pyo3_macros_backend/
pymethod.rs

1use std::borrow::Cow;
2use std::ffi::CString;
3
4use crate::attributes::{FromPyWithAttribute, NameAttribute, RenamingRule};
5use crate::method::{CallingConvention, ExtractErrorMode, PyArg};
6use crate::params::{impl_regular_arg_param, Holders};
7use crate::utils::{Ctx, LitCStr};
8use crate::utils::{PythonDoc, TypeExt as _};
9use crate::{
10    method::{FnArg, FnSpec, FnType, SelfType},
11    pyfunction::PyFunctionOptions,
12};
13use crate::{quotes, utils};
14use proc_macro2::{Span, TokenStream};
15use quote::{format_ident, quote, quote_spanned, ToTokens};
16use syn::{ext::IdentExt, spanned::Spanned, Result};
17
18/// Generated code for a single pymethod item.
19pub struct MethodAndMethodDef {
20    /// The implementation of the Python wrapper for the pymethod
21    pub associated_method: TokenStream,
22    /// The method def which will be used to register this pymethod
23    pub method_def: TokenStream,
24}
25
26/// Generated code for a single pymethod item which is registered by a slot.
27pub struct MethodAndSlotDef {
28    /// The implementation of the Python wrapper for the pymethod
29    pub associated_method: TokenStream,
30    /// The slot def which will be used to register this pymethod
31    pub slot_def: TokenStream,
32}
33
34pub enum GeneratedPyMethod {
35    Method(MethodAndMethodDef),
36    Proto(MethodAndSlotDef),
37    SlotTraitImpl(String, TokenStream),
38}
39
40pub struct PyMethod<'a> {
41    kind: PyMethodKind,
42    method_name: String,
43    spec: FnSpec<'a>,
44}
45
46enum PyMethodKind {
47    Fn,
48    Proto(PyMethodProtoKind),
49}
50
51impl PyMethodKind {
52    fn from_name(name: &str) -> Self {
53        match name {
54            // Protocol implemented through slots
55            "__str__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__STR__)),
56            "__repr__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__REPR__)),
57            "__hash__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__HASH__)),
58            "__richcmp__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__RICHCMP__)),
59            "__get__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__GET__)),
60            "__iter__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ITER__)),
61            "__next__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__NEXT__)),
62            "__await__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__AWAIT__)),
63            "__aiter__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__AITER__)),
64            "__anext__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ANEXT__)),
65            "__len__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__LEN__)),
66            "__contains__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__CONTAINS__)),
67            "__concat__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__CONCAT__)),
68            "__repeat__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__REPEAT__)),
69            "__inplace_concat__" => {
70                PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INPLACE_CONCAT__))
71            }
72            "__inplace_repeat__" => {
73                PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INPLACE_REPEAT__))
74            }
75            "__getitem__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__GETITEM__)),
76            "__pos__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__POS__)),
77            "__neg__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__NEG__)),
78            "__abs__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ABS__)),
79            "__invert__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INVERT__)),
80            "__index__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INDEX__)),
81            "__int__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__INT__)),
82            "__float__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__FLOAT__)),
83            "__bool__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__BOOL__)),
84            "__iadd__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IADD__)),
85            "__isub__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ISUB__)),
86            "__imul__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IMUL__)),
87            "__imatmul__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IMATMUL__)),
88            "__itruediv__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ITRUEDIV__)),
89            "__ifloordiv__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IFLOORDIV__)),
90            "__imod__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IMOD__)),
91            "__ipow__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IPOW__)),
92            "__ilshift__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__ILSHIFT__)),
93            "__irshift__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IRSHIFT__)),
94            "__iand__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IAND__)),
95            "__ixor__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IXOR__)),
96            "__ior__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__IOR__)),
97            "__getbuffer__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__GETBUFFER__)),
98            "__releasebuffer__" => PyMethodKind::Proto(PyMethodProtoKind::Slot(&__RELEASEBUFFER__)),
99            // Protocols implemented through traits
100            "__getattribute__" => {
101                PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GETATTRIBUTE__))
102            }
103            "__getattr__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GETATTR__)),
104            "__setattr__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SETATTR__)),
105            "__delattr__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DELATTR__)),
106            "__set__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SET__)),
107            "__delete__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DELETE__)),
108            "__setitem__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SETITEM__)),
109            "__delitem__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DELITEM__)),
110            "__add__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__ADD__)),
111            "__radd__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RADD__)),
112            "__sub__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__SUB__)),
113            "__rsub__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RSUB__)),
114            "__mul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__MUL__)),
115            "__rmul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RMUL__)),
116            "__matmul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__MATMUL__)),
117            "__rmatmul__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RMATMUL__)),
118            "__floordiv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__FLOORDIV__)),
119            "__rfloordiv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RFLOORDIV__)),
120            "__truediv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__TRUEDIV__)),
121            "__rtruediv__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RTRUEDIV__)),
122            "__divmod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__DIVMOD__)),
123            "__rdivmod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RDIVMOD__)),
124            "__mod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__MOD__)),
125            "__rmod__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RMOD__)),
126            "__lshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__LSHIFT__)),
127            "__rlshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RLSHIFT__)),
128            "__rshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RSHIFT__)),
129            "__rrshift__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RRSHIFT__)),
130            "__and__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__AND__)),
131            "__rand__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RAND__)),
132            "__xor__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__XOR__)),
133            "__rxor__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RXOR__)),
134            "__or__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__OR__)),
135            "__ror__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__ROR__)),
136            "__pow__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__POW__)),
137            "__rpow__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__RPOW__)),
138            "__lt__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__LT__)),
139            "__le__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__LE__)),
140            "__eq__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__EQ__)),
141            "__ne__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__NE__)),
142            "__gt__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GT__)),
143            "__ge__" => PyMethodKind::Proto(PyMethodProtoKind::SlotFragment(&__GE__)),
144            // Some tricky protocols which don't fit the pattern of the rest
145            "__call__" => PyMethodKind::Proto(PyMethodProtoKind::Call),
146            "__traverse__" => PyMethodKind::Proto(PyMethodProtoKind::Traverse),
147            "__clear__" => PyMethodKind::Proto(PyMethodProtoKind::Clear),
148            // Not a proto
149            _ => PyMethodKind::Fn,
150        }
151    }
152}
153
154enum PyMethodProtoKind {
155    Slot(&'static SlotDef),
156    Call,
157    Traverse,
158    Clear,
159    SlotFragment(&'static SlotFragmentDef),
160}
161
162impl<'a> PyMethod<'a> {
163    fn parse(
164        sig: &'a mut syn::Signature,
165        meth_attrs: &mut Vec<syn::Attribute>,
166        options: PyFunctionOptions,
167    ) -> Result<Self> {
168        let spec = FnSpec::parse(sig, meth_attrs, options)?;
169
170        let method_name = spec.python_name.to_string();
171        let kind = PyMethodKind::from_name(&method_name);
172
173        Ok(Self {
174            kind,
175            method_name,
176            spec,
177        })
178    }
179}
180
181pub fn is_proto_method(name: &str) -> bool {
182    match PyMethodKind::from_name(name) {
183        PyMethodKind::Fn => false,
184        PyMethodKind::Proto(_) => true,
185    }
186}
187
188pub fn gen_py_method(
189    cls: &syn::Type,
190    sig: &mut syn::Signature,
191    meth_attrs: &mut Vec<syn::Attribute>,
192    options: PyFunctionOptions,
193    ctx: &Ctx,
194) -> Result<GeneratedPyMethod> {
195    check_generic(sig)?;
196    ensure_function_options_valid(&options)?;
197    let method = PyMethod::parse(sig, meth_attrs, options)?;
198    let spec = &method.spec;
199    let Ctx { pyo3_path, .. } = ctx;
200
201    Ok(match (method.kind, &spec.tp) {
202        // Class attributes go before protos so that class attributes can be used to set proto
203        // method to None.
204        (_, FnType::ClassAttribute) => {
205            GeneratedPyMethod::Method(impl_py_class_attribute(cls, spec, ctx)?)
206        }
207        (PyMethodKind::Proto(proto_kind), _) => {
208            ensure_no_forbidden_protocol_attributes(&proto_kind, spec, &method.method_name)?;
209            match proto_kind {
210                PyMethodProtoKind::Slot(slot_def) => {
211                    let slot = slot_def.generate_type_slot(cls, spec, &method.method_name, ctx)?;
212                    GeneratedPyMethod::Proto(slot)
213                }
214                PyMethodProtoKind::Call => {
215                    GeneratedPyMethod::Proto(impl_call_slot(cls, method.spec, ctx)?)
216                }
217                PyMethodProtoKind::Traverse => {
218                    GeneratedPyMethod::Proto(impl_traverse_slot(cls, spec, ctx)?)
219                }
220                PyMethodProtoKind::Clear => {
221                    GeneratedPyMethod::Proto(impl_clear_slot(cls, spec, ctx)?)
222                }
223                PyMethodProtoKind::SlotFragment(slot_fragment_def) => {
224                    let proto = slot_fragment_def.generate_pyproto_fragment(cls, spec, ctx)?;
225                    GeneratedPyMethod::SlotTraitImpl(method.method_name, proto)
226                }
227            }
228        }
229        // ordinary functions (with some specialties)
230        (_, FnType::Fn(_)) => GeneratedPyMethod::Method(impl_py_method_def(
231            cls,
232            spec,
233            &spec.get_doc(meth_attrs, ctx),
234            None,
235            ctx,
236        )?),
237        (_, FnType::FnClass(_)) => GeneratedPyMethod::Method(impl_py_method_def(
238            cls,
239            spec,
240            &spec.get_doc(meth_attrs, ctx),
241            Some(quote!(#pyo3_path::ffi::METH_CLASS)),
242            ctx,
243        )?),
244        (_, FnType::FnStatic) => GeneratedPyMethod::Method(impl_py_method_def(
245            cls,
246            spec,
247            &spec.get_doc(meth_attrs, ctx),
248            Some(quote!(#pyo3_path::ffi::METH_STATIC)),
249            ctx,
250        )?),
251        // special prototypes
252        (_, FnType::FnNew) | (_, FnType::FnNewClass(_)) => {
253            GeneratedPyMethod::Proto(impl_py_method_def_new(cls, spec, ctx)?)
254        }
255
256        (_, FnType::Getter(self_type)) => GeneratedPyMethod::Method(impl_py_getter_def(
257            cls,
258            PropertyType::Function {
259                self_type,
260                spec,
261                doc: spec.get_doc(meth_attrs, ctx),
262            },
263            ctx,
264        )?),
265        (_, FnType::Setter(self_type)) => GeneratedPyMethod::Method(impl_py_setter_def(
266            cls,
267            PropertyType::Function {
268                self_type,
269                spec,
270                doc: spec.get_doc(meth_attrs, ctx),
271            },
272            ctx,
273        )?),
274        (_, FnType::FnModule(_)) => {
275            unreachable!("methods cannot be FnModule")
276        }
277    })
278}
279
280pub fn check_generic(sig: &syn::Signature) -> syn::Result<()> {
281    let err_msg = |typ| format!("Python functions cannot have generic {} parameters", typ);
282    for param in &sig.generics.params {
283        match param {
284            syn::GenericParam::Lifetime(_) => {}
285            syn::GenericParam::Type(_) => bail_spanned!(param.span() => err_msg("type")),
286            syn::GenericParam::Const(_) => bail_spanned!(param.span() => err_msg("const")),
287        }
288    }
289    Ok(())
290}
291
292fn ensure_function_options_valid(options: &PyFunctionOptions) -> syn::Result<()> {
293    if let Some(pass_module) = &options.pass_module {
294        bail_spanned!(pass_module.span() => "`pass_module` cannot be used on Python methods");
295    }
296    Ok(())
297}
298
299fn ensure_no_forbidden_protocol_attributes(
300    proto_kind: &PyMethodProtoKind,
301    spec: &FnSpec<'_>,
302    method_name: &str,
303) -> syn::Result<()> {
304    if let Some(signature) = &spec.signature.attribute {
305        // __call__ is allowed to have a signature, but nothing else is.
306        if !matches!(proto_kind, PyMethodProtoKind::Call) {
307            bail_spanned!(signature.kw.span() => format!("`signature` cannot be used with magic method `{}`", method_name));
308        }
309    }
310    if let Some(text_signature) = &spec.text_signature {
311        bail_spanned!(text_signature.kw.span() => format!("`text_signature` cannot be used with magic method `{}`", method_name));
312    }
313    Ok(())
314}
315
316/// Also used by pyfunction.
317pub fn impl_py_method_def(
318    cls: &syn::Type,
319    spec: &FnSpec<'_>,
320    doc: &PythonDoc,
321    flags: Option<TokenStream>,
322    ctx: &Ctx,
323) -> Result<MethodAndMethodDef> {
324    let Ctx { pyo3_path, .. } = ctx;
325    let wrapper_ident = format_ident!("__pymethod_{}__", spec.python_name);
326    let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?;
327    let add_flags = flags.map(|flags| quote!(.flags(#flags)));
328    let methoddef_type = match spec.tp {
329        FnType::FnStatic => quote!(Static),
330        FnType::FnClass(_) => quote!(Class),
331        _ => quote!(Method),
332    };
333    let methoddef = spec.get_methoddef(quote! { #cls::#wrapper_ident }, doc, ctx);
334    let method_def = quote! {
335        #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static(
336            #pyo3_path::impl_::pymethods::PyMethodDefType::#methoddef_type(#methoddef #add_flags)
337        )
338    };
339    Ok(MethodAndMethodDef {
340        associated_method,
341        method_def,
342    })
343}
344
345/// Also used by pyclass.
346pub fn impl_py_method_def_new(
347    cls: &syn::Type,
348    spec: &FnSpec<'_>,
349    ctx: &Ctx,
350) -> Result<MethodAndSlotDef> {
351    let Ctx { pyo3_path, .. } = ctx;
352    let wrapper_ident = syn::Ident::new("__pymethod___new____", Span::call_site());
353    let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?;
354    // Use just the text_signature_call_signature() because the class' Python name
355    // isn't known to `#[pymethods]` - that has to be attached at runtime from the PyClassImpl
356    // trait implementation created by `#[pyclass]`.
357    let text_signature_body = spec.text_signature_call_signature().map_or_else(
358        || quote!(::std::option::Option::None),
359        |text_signature| quote!(::std::option::Option::Some(#text_signature)),
360    );
361    let slot_def = quote! {
362        #pyo3_path::ffi::PyType_Slot {
363            slot: #pyo3_path::ffi::Py_tp_new,
364            pfunc: {
365                unsafe extern "C" fn trampoline(
366                    subtype: *mut #pyo3_path::ffi::PyTypeObject,
367                    args: *mut #pyo3_path::ffi::PyObject,
368                    kwargs: *mut #pyo3_path::ffi::PyObject,
369                ) -> *mut #pyo3_path::ffi::PyObject {
370                    #[allow(unknown_lints, non_local_definitions)]
371                    impl #pyo3_path::impl_::pyclass::PyClassNewTextSignature<#cls> for #pyo3_path::impl_::pyclass::PyClassImplCollector<#cls> {
372                        #[inline]
373                        fn new_text_signature(self) -> ::std::option::Option<&'static str> {
374                            #text_signature_body
375                        }
376                    }
377
378                    #pyo3_path::impl_::trampoline::newfunc(
379                        subtype,
380                        args,
381                        kwargs,
382                        #cls::#wrapper_ident
383                    )
384                }
385                trampoline
386            } as #pyo3_path::ffi::newfunc as _
387        }
388    };
389    Ok(MethodAndSlotDef {
390        associated_method,
391        slot_def,
392    })
393}
394
395fn impl_call_slot(cls: &syn::Type, mut spec: FnSpec<'_>, ctx: &Ctx) -> Result<MethodAndSlotDef> {
396    let Ctx { pyo3_path, .. } = ctx;
397
398    // HACK: __call__ proto slot must always use varargs calling convention, so change the spec.
399    // Probably indicates there's a refactoring opportunity somewhere.
400    spec.convention = CallingConvention::Varargs;
401
402    let wrapper_ident = syn::Ident::new("__pymethod___call____", Span::call_site());
403    let associated_method = spec.get_wrapper_function(&wrapper_ident, Some(cls), ctx)?;
404    let slot_def = quote! {
405        #pyo3_path::ffi::PyType_Slot {
406            slot: #pyo3_path::ffi::Py_tp_call,
407            pfunc: {
408                unsafe extern "C" fn trampoline(
409                    slf: *mut #pyo3_path::ffi::PyObject,
410                    args: *mut #pyo3_path::ffi::PyObject,
411                    kwargs: *mut #pyo3_path::ffi::PyObject,
412                ) -> *mut #pyo3_path::ffi::PyObject
413                {
414                    #pyo3_path::impl_::trampoline::ternaryfunc(
415                        slf,
416                        args,
417                        kwargs,
418                        #cls::#wrapper_ident
419                    )
420                }
421                trampoline
422            } as #pyo3_path::ffi::ternaryfunc as _
423        }
424    };
425    Ok(MethodAndSlotDef {
426        associated_method,
427        slot_def,
428    })
429}
430
431fn impl_traverse_slot(
432    cls: &syn::Type,
433    spec: &FnSpec<'_>,
434    ctx: &Ctx,
435) -> syn::Result<MethodAndSlotDef> {
436    let Ctx { pyo3_path, .. } = ctx;
437    if let (Some(py_arg), _) = split_off_python_arg(&spec.signature.arguments) {
438        return Err(syn::Error::new_spanned(py_arg.ty, "__traverse__ may not take `Python`. \
439            Usually, an implementation of `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` \
440            should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited \
441            inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic."));
442    }
443
444    // check that the receiver does not try to smuggle an (implicit) `Python` token into here
445    if let FnType::Fn(SelfType::TryFromBoundRef(span))
446    | FnType::Fn(SelfType::Receiver {
447        mutable: true,
448        span,
449    }) = spec.tp
450    {
451        bail_spanned! { span =>
452            "__traverse__ may not take a receiver other than `&self`. Usually, an implementation of \
453            `__traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError>` \
454            should do nothing but calls to `visit.call`. Most importantly, safe access to the GIL is prohibited \
455            inside implementations of `__traverse__`, i.e. `Python::with_gil` will panic."
456        }
457    }
458
459    let rust_fn_ident = spec.name;
460
461    let associated_method = quote! {
462        pub unsafe extern "C" fn __pymethod_traverse__(
463            slf: *mut #pyo3_path::ffi::PyObject,
464            visit: #pyo3_path::ffi::visitproc,
465            arg: *mut ::std::os::raw::c_void,
466        ) -> ::std::os::raw::c_int {
467            #pyo3_path::impl_::pymethods::_call_traverse::<#cls>(slf, #cls::#rust_fn_ident, visit, arg, #cls::__pymethod_traverse__)
468        }
469    };
470    let slot_def = quote! {
471        #pyo3_path::ffi::PyType_Slot {
472            slot: #pyo3_path::ffi::Py_tp_traverse,
473            pfunc: #cls::__pymethod_traverse__ as #pyo3_path::ffi::traverseproc as _
474        }
475    };
476    Ok(MethodAndSlotDef {
477        associated_method,
478        slot_def,
479    })
480}
481
482fn impl_clear_slot(cls: &syn::Type, spec: &FnSpec<'_>, ctx: &Ctx) -> syn::Result<MethodAndSlotDef> {
483    let Ctx { pyo3_path, .. } = ctx;
484    let (py_arg, args) = split_off_python_arg(&spec.signature.arguments);
485    let self_type = match &spec.tp {
486        FnType::Fn(self_type) => self_type,
487        _ => bail_spanned!(spec.name.span() => "expected instance method for `__clear__` function"),
488    };
489    let mut holders = Holders::new();
490    let slf = self_type.receiver(cls, ExtractErrorMode::Raise, &mut holders, ctx);
491
492    if let [arg, ..] = args {
493        bail_spanned!(arg.ty().span() => "`__clear__` function expected to have no arguments");
494    }
495
496    let name = &spec.name;
497    let holders = holders.init_holders(ctx);
498    let fncall = if py_arg.is_some() {
499        quote!(#cls::#name(#slf, py))
500    } else {
501        quote!(#cls::#name(#slf))
502    };
503
504    let associated_method = quote! {
505        pub unsafe extern "C" fn __pymethod___clear____(
506            _slf: *mut #pyo3_path::ffi::PyObject,
507        ) -> ::std::os::raw::c_int {
508            #pyo3_path::impl_::pymethods::_call_clear(_slf, |py, _slf| {
509                #holders
510                let result = #fncall;
511                let result = #pyo3_path::impl_::wrap::converter(&result).wrap(result)?;
512                ::std::result::Result::Ok(result)
513            }, #cls::__pymethod___clear____)
514        }
515    };
516    let slot_def = quote! {
517        #pyo3_path::ffi::PyType_Slot {
518            slot: #pyo3_path::ffi::Py_tp_clear,
519            pfunc: #cls::__pymethod___clear____ as #pyo3_path::ffi::inquiry as _
520        }
521    };
522    Ok(MethodAndSlotDef {
523        associated_method,
524        slot_def,
525    })
526}
527
528pub(crate) fn impl_py_class_attribute(
529    cls: &syn::Type,
530    spec: &FnSpec<'_>,
531    ctx: &Ctx,
532) -> syn::Result<MethodAndMethodDef> {
533    let Ctx { pyo3_path, .. } = ctx;
534    let (py_arg, args) = split_off_python_arg(&spec.signature.arguments);
535    ensure_spanned!(
536        args.is_empty(),
537        args[0].ty().span() => "#[classattr] can only have one argument (of type pyo3::Python)"
538    );
539
540    let name = &spec.name;
541    let fncall = if py_arg.is_some() {
542        quote!(function(py))
543    } else {
544        quote!(function())
545    };
546
547    let wrapper_ident = format_ident!("__pymethod_{}__", name);
548    let python_name = spec.null_terminated_python_name(ctx);
549    let body = quotes::ok_wrap(fncall, ctx);
550
551    let associated_method = quote! {
552        fn #wrapper_ident(py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> {
553            let function = #cls::#name; // Shadow the method name to avoid #3017
554            let result = #body;
555            #pyo3_path::impl_::wrap::converter(&result).map_into_pyobject(py, result)
556        }
557    };
558
559    let method_def = quote! {
560        #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static(
561            #pyo3_path::impl_::pymethods::PyMethodDefType::ClassAttribute({
562                #pyo3_path::impl_::pymethods::PyClassAttributeDef::new(
563                    #python_name,
564                    #cls::#wrapper_ident
565                )
566            })
567        )
568    };
569
570    Ok(MethodAndMethodDef {
571        associated_method,
572        method_def,
573    })
574}
575
576fn impl_call_setter(
577    cls: &syn::Type,
578    spec: &FnSpec<'_>,
579    self_type: &SelfType,
580    holders: &mut Holders,
581    ctx: &Ctx,
582) -> syn::Result<TokenStream> {
583    let (py_arg, args) = split_off_python_arg(&spec.signature.arguments);
584    let slf = self_type.receiver(cls, ExtractErrorMode::Raise, holders, ctx);
585
586    if args.is_empty() {
587        bail_spanned!(spec.name.span() => "setter function expected to have one argument");
588    } else if args.len() > 1 {
589        bail_spanned!(
590            args[1].ty().span() =>
591            "setter function can have at most two arguments ([pyo3::Python,] and value)"
592        );
593    }
594
595    let name = &spec.name;
596    let fncall = if py_arg.is_some() {
597        quote!(#cls::#name(#slf, py, _val))
598    } else {
599        quote!(#cls::#name(#slf, _val))
600    };
601
602    Ok(fncall)
603}
604
605// Used here for PropertyType::Function, used in pyclass for descriptors.
606pub fn impl_py_setter_def(
607    cls: &syn::Type,
608    property_type: PropertyType<'_>,
609    ctx: &Ctx,
610) -> Result<MethodAndMethodDef> {
611    let Ctx { pyo3_path, .. } = ctx;
612    let python_name = property_type.null_terminated_python_name(ctx)?;
613    let doc = property_type.doc(ctx);
614    let mut holders = Holders::new();
615    let setter_impl = match property_type {
616        PropertyType::Descriptor {
617            field_index, field, ..
618        } => {
619            let slf = SelfType::Receiver {
620                mutable: true,
621                span: Span::call_site(),
622            }
623            .receiver(cls, ExtractErrorMode::Raise, &mut holders, ctx);
624            if let Some(ident) = &field.ident {
625                // named struct field
626                quote!({ #slf.#ident = _val; })
627            } else {
628                // tuple struct field
629                let index = syn::Index::from(field_index);
630                quote!({ #slf.#index = _val; })
631            }
632        }
633        PropertyType::Function {
634            spec, self_type, ..
635        } => impl_call_setter(cls, spec, self_type, &mut holders, ctx)?,
636    };
637
638    let wrapper_ident = match property_type {
639        PropertyType::Descriptor {
640            field: syn::Field {
641                ident: Some(ident), ..
642            },
643            ..
644        } => {
645            format_ident!("__pymethod_set_{}__", ident)
646        }
647        PropertyType::Descriptor { field_index, .. } => {
648            format_ident!("__pymethod_set_field_{}__", field_index)
649        }
650        PropertyType::Function { spec, .. } => {
651            format_ident!("__pymethod_set_{}__", spec.name)
652        }
653    };
654
655    let extract = match &property_type {
656        PropertyType::Function { spec, .. } => {
657            let (_, args) = split_off_python_arg(&spec.signature.arguments);
658            let value_arg = &args[0];
659            let (from_py_with, ident) =
660                if let Some(from_py_with) = &value_arg.from_py_with().as_ref().map(|f| &f.value) {
661                    let ident = syn::Ident::new("from_py_with", from_py_with.span());
662                    (
663                        quote_spanned! { from_py_with.span() =>
664                            let #ident = #from_py_with;
665                        },
666                        ident,
667                    )
668                } else {
669                    (quote!(), syn::Ident::new("dummy", Span::call_site()))
670                };
671
672            let arg = if let FnArg::Regular(arg) = &value_arg {
673                arg
674            } else {
675                bail_spanned!(value_arg.name().span() => "The #[setter] value argument can't be *args, **kwargs or `cancel_handle`.");
676            };
677
678            let extract = impl_regular_arg_param(
679                arg,
680                ident,
681                quote!(::std::option::Option::Some(_value.into())),
682                &mut holders,
683                ctx,
684            );
685
686            quote! {
687                #from_py_with
688                let _val = #extract;
689            }
690        }
691        PropertyType::Descriptor { field, .. } => {
692            let span = field.ty.span();
693            let name = field
694                .ident
695                .as_ref()
696                .map(|i| i.to_string())
697                .unwrap_or_default();
698
699            let holder = holders.push_holder(span);
700            let ty = field.ty.clone().elide_lifetimes();
701            quote! {
702                #[allow(unused_imports)]
703                use #pyo3_path::impl_::pyclass::Probe as _;
704                let _val = #pyo3_path::impl_::extract_argument::extract_argument::<
705                    _,
706                    { #pyo3_path::impl_::pyclass::IsOption::<#ty>::VALUE }
707                >(_value.into(), &mut #holder, #name)?;
708            }
709        }
710    };
711
712    let mut cfg_attrs = TokenStream::new();
713    if let PropertyType::Descriptor { field, .. } = &property_type {
714        for attr in field
715            .attrs
716            .iter()
717            .filter(|attr| attr.path().is_ident("cfg"))
718        {
719            attr.to_tokens(&mut cfg_attrs);
720        }
721    }
722
723    let init_holders = holders.init_holders(ctx);
724    let associated_method = quote! {
725        #cfg_attrs
726        unsafe fn #wrapper_ident(
727            py: #pyo3_path::Python<'_>,
728            _slf: *mut #pyo3_path::ffi::PyObject,
729            _value: *mut #pyo3_path::ffi::PyObject,
730        ) -> #pyo3_path::PyResult<::std::os::raw::c_int> {
731            use ::std::convert::Into;
732            let _value = #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr_or_opt(py, &_value)
733                .ok_or_else(|| {
734                    #pyo3_path::exceptions::PyAttributeError::new_err("can't delete attribute")
735                })?;
736            #init_holders
737            #extract
738            let result = #setter_impl;
739            #pyo3_path::impl_::callback::convert(py, result)
740        }
741    };
742
743    let method_def = quote! {
744        #cfg_attrs
745        #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static(
746            #pyo3_path::impl_::pymethods::PyMethodDefType::Setter(
747                #pyo3_path::impl_::pymethods::PySetterDef::new(
748                    #python_name,
749                    #cls::#wrapper_ident,
750                    #doc
751                )
752            )
753        )
754    };
755
756    Ok(MethodAndMethodDef {
757        associated_method,
758        method_def,
759    })
760}
761
762fn impl_call_getter(
763    cls: &syn::Type,
764    spec: &FnSpec<'_>,
765    self_type: &SelfType,
766    holders: &mut Holders,
767    ctx: &Ctx,
768) -> syn::Result<TokenStream> {
769    let (py_arg, args) = split_off_python_arg(&spec.signature.arguments);
770    let slf = self_type.receiver(cls, ExtractErrorMode::Raise, holders, ctx);
771    ensure_spanned!(
772        args.is_empty(),
773        args[0].ty().span() => "getter function can only have one argument (of type pyo3::Python)"
774    );
775
776    let name = &spec.name;
777    let fncall = if py_arg.is_some() {
778        quote!(#cls::#name(#slf, py))
779    } else {
780        quote!(#cls::#name(#slf))
781    };
782
783    Ok(fncall)
784}
785
786// Used here for PropertyType::Function, used in pyclass for descriptors.
787pub fn impl_py_getter_def(
788    cls: &syn::Type,
789    property_type: PropertyType<'_>,
790    ctx: &Ctx,
791) -> Result<MethodAndMethodDef> {
792    let Ctx { pyo3_path, .. } = ctx;
793    let python_name = property_type.null_terminated_python_name(ctx)?;
794    let doc = property_type.doc(ctx);
795
796    let mut cfg_attrs = TokenStream::new();
797    if let PropertyType::Descriptor { field, .. } = &property_type {
798        for attr in field
799            .attrs
800            .iter()
801            .filter(|attr| attr.path().is_ident("cfg"))
802        {
803            attr.to_tokens(&mut cfg_attrs);
804        }
805    }
806
807    let mut holders = Holders::new();
808    match property_type {
809        PropertyType::Descriptor {
810            field_index, field, ..
811        } => {
812            let ty = &field.ty;
813            let field = if let Some(ident) = &field.ident {
814                ident.to_token_stream()
815            } else {
816                syn::Index::from(field_index).to_token_stream()
817            };
818
819            // TODO: on MSRV 1.77+, we can use `::std::mem::offset_of!` here, and it should
820            // make it possible for the `MaybeRuntimePyMethodDef` to be a `Static` variant.
821            let generator = quote_spanned! { ty.span() =>
822                #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Runtime(
823                    || GENERATOR.generate(#python_name, #doc)
824                )
825            };
826            // This is separate so that the unsafe below does not inherit the span and thus does not
827            // trigger the `unsafe_code` lint
828            let method_def = quote! {
829                #cfg_attrs
830                {
831                    #[allow(unused_imports)]  // might not be used if all probes are positve
832                    use #pyo3_path::impl_::pyclass::Probe;
833
834                    struct Offset;
835                    unsafe impl #pyo3_path::impl_::pyclass::OffsetCalculator<#cls, #ty> for Offset {
836                        fn offset() -> usize {
837                            #pyo3_path::impl_::pyclass::class_offset::<#cls>() +
838                            #pyo3_path::impl_::pyclass::offset_of!(#cls, #field)
839                        }
840                    }
841
842                    const GENERATOR: #pyo3_path::impl_::pyclass::PyClassGetterGenerator::<
843                        #cls,
844                        #ty,
845                        Offset,
846                        { #pyo3_path::impl_::pyclass::IsPyT::<#ty>::VALUE },
847                        { #pyo3_path::impl_::pyclass::IsIntoPyObjectRef::<#ty>::VALUE },
848                        { #pyo3_path::impl_::pyclass::IsIntoPyObject::<#ty>::VALUE },
849                    > = unsafe { #pyo3_path::impl_::pyclass::PyClassGetterGenerator::new() };
850                    #generator
851                }
852            };
853
854            Ok(MethodAndMethodDef {
855                associated_method: quote! {},
856                method_def,
857            })
858        }
859        // Forward to `IntoPyCallbackOutput`, to handle `#[getter]`s returning results.
860        PropertyType::Function {
861            spec, self_type, ..
862        } => {
863            let wrapper_ident = format_ident!("__pymethod_get_{}__", spec.name);
864            let call = impl_call_getter(cls, spec, self_type, &mut holders, ctx)?;
865            let body = quote! {
866                #pyo3_path::impl_::callback::convert(py, #call)
867            };
868
869            let init_holders = holders.init_holders(ctx);
870            let associated_method = quote! {
871                #cfg_attrs
872                unsafe fn #wrapper_ident(
873                    py: #pyo3_path::Python<'_>,
874                    _slf: *mut #pyo3_path::ffi::PyObject
875                ) -> #pyo3_path::PyResult<*mut #pyo3_path::ffi::PyObject> {
876                    #init_holders
877                    let result = #body;
878                    result
879                }
880            };
881
882            let method_def = quote! {
883                #cfg_attrs
884                #pyo3_path::impl_::pyclass::MaybeRuntimePyMethodDef::Static(
885                    #pyo3_path::impl_::pymethods::PyMethodDefType::Getter(
886                        #pyo3_path::impl_::pymethods::PyGetterDef::new(
887                            #python_name,
888                            #cls::#wrapper_ident,
889                            #doc
890                        )
891                    )
892                )
893            };
894
895            Ok(MethodAndMethodDef {
896                associated_method,
897                method_def,
898            })
899        }
900    }
901}
902
903/// Split an argument of pyo3::Python from the front of the arg list, if present
904fn split_off_python_arg<'a, 'b>(args: &'a [FnArg<'b>]) -> (Option<&'a PyArg<'b>>, &'a [FnArg<'b>]) {
905    match args {
906        [FnArg::Py(py), args @ ..] => (Some(py), args),
907        args => (None, args),
908    }
909}
910
911pub enum PropertyType<'a> {
912    Descriptor {
913        field_index: usize,
914        field: &'a syn::Field,
915        python_name: Option<&'a NameAttribute>,
916        renaming_rule: Option<RenamingRule>,
917    },
918    Function {
919        self_type: &'a SelfType,
920        spec: &'a FnSpec<'a>,
921        doc: PythonDoc,
922    },
923}
924
925impl PropertyType<'_> {
926    fn null_terminated_python_name(&self, ctx: &Ctx) -> Result<LitCStr> {
927        match self {
928            PropertyType::Descriptor {
929                field,
930                python_name,
931                renaming_rule,
932                ..
933            } => {
934                let name = match (python_name, &field.ident) {
935                    (Some(name), _) => name.value.0.to_string(),
936                    (None, Some(field_name)) => {
937                        let mut name = field_name.unraw().to_string();
938                        if let Some(rule) = renaming_rule {
939                            name = utils::apply_renaming_rule(*rule, &name);
940                        }
941                        name
942                    }
943                    (None, None) => {
944                        bail_spanned!(field.span() => "`get` and `set` with tuple struct fields require `name`");
945                    }
946                };
947                let name = CString::new(name).unwrap();
948                Ok(LitCStr::new(name, field.span(), ctx))
949            }
950            PropertyType::Function { spec, .. } => Ok(spec.null_terminated_python_name(ctx)),
951        }
952    }
953
954    fn doc(&self, ctx: &Ctx) -> Cow<'_, PythonDoc> {
955        match self {
956            PropertyType::Descriptor { field, .. } => {
957                Cow::Owned(utils::get_doc(&field.attrs, None, ctx))
958            }
959            PropertyType::Function { doc, .. } => Cow::Borrowed(doc),
960        }
961    }
962}
963
964pub const __STR__: SlotDef = SlotDef::new("Py_tp_str", "reprfunc");
965pub const __REPR__: SlotDef = SlotDef::new("Py_tp_repr", "reprfunc");
966pub const __HASH__: SlotDef = SlotDef::new("Py_tp_hash", "hashfunc")
967    .ret_ty(Ty::PyHashT)
968    .return_conversion(TokenGenerator(
969        |Ctx { pyo3_path, .. }: &Ctx| quote! { #pyo3_path::impl_::callback::HashCallbackOutput },
970    ));
971pub const __RICHCMP__: SlotDef = SlotDef::new("Py_tp_richcompare", "richcmpfunc")
972    .extract_error_mode(ExtractErrorMode::NotImplemented)
973    .arguments(&[Ty::Object, Ty::CompareOp]);
974const __GET__: SlotDef = SlotDef::new("Py_tp_descr_get", "descrgetfunc")
975    .arguments(&[Ty::MaybeNullObject, Ty::MaybeNullObject]);
976const __ITER__: SlotDef = SlotDef::new("Py_tp_iter", "getiterfunc");
977const __NEXT__: SlotDef = SlotDef::new("Py_tp_iternext", "iternextfunc")
978    .return_specialized_conversion(
979        TokenGenerator(|_| quote! { IterBaseKind, IterOptionKind, IterResultOptionKind }),
980        TokenGenerator(|_| quote! { iter_tag }),
981    );
982const __AWAIT__: SlotDef = SlotDef::new("Py_am_await", "unaryfunc");
983const __AITER__: SlotDef = SlotDef::new("Py_am_aiter", "unaryfunc");
984const __ANEXT__: SlotDef = SlotDef::new("Py_am_anext", "unaryfunc").return_specialized_conversion(
985    TokenGenerator(
986        |_| quote! { AsyncIterBaseKind, AsyncIterOptionKind, AsyncIterResultOptionKind },
987    ),
988    TokenGenerator(|_| quote! { async_iter_tag }),
989);
990pub const __LEN__: SlotDef = SlotDef::new("Py_mp_length", "lenfunc").ret_ty(Ty::PySsizeT);
991const __CONTAINS__: SlotDef = SlotDef::new("Py_sq_contains", "objobjproc")
992    .arguments(&[Ty::Object])
993    .ret_ty(Ty::Int);
994const __CONCAT__: SlotDef = SlotDef::new("Py_sq_concat", "binaryfunc").arguments(&[Ty::Object]);
995const __REPEAT__: SlotDef = SlotDef::new("Py_sq_repeat", "ssizeargfunc").arguments(&[Ty::PySsizeT]);
996const __INPLACE_CONCAT__: SlotDef =
997    SlotDef::new("Py_sq_concat", "binaryfunc").arguments(&[Ty::Object]);
998const __INPLACE_REPEAT__: SlotDef =
999    SlotDef::new("Py_sq_repeat", "ssizeargfunc").arguments(&[Ty::PySsizeT]);
1000pub const __GETITEM__: SlotDef =
1001    SlotDef::new("Py_mp_subscript", "binaryfunc").arguments(&[Ty::Object]);
1002
1003const __POS__: SlotDef = SlotDef::new("Py_nb_positive", "unaryfunc");
1004const __NEG__: SlotDef = SlotDef::new("Py_nb_negative", "unaryfunc");
1005const __ABS__: SlotDef = SlotDef::new("Py_nb_absolute", "unaryfunc");
1006const __INVERT__: SlotDef = SlotDef::new("Py_nb_invert", "unaryfunc");
1007const __INDEX__: SlotDef = SlotDef::new("Py_nb_index", "unaryfunc");
1008pub const __INT__: SlotDef = SlotDef::new("Py_nb_int", "unaryfunc");
1009const __FLOAT__: SlotDef = SlotDef::new("Py_nb_float", "unaryfunc");
1010const __BOOL__: SlotDef = SlotDef::new("Py_nb_bool", "inquiry").ret_ty(Ty::Int);
1011
1012const __IADD__: SlotDef = SlotDef::new("Py_nb_inplace_add", "binaryfunc")
1013    .arguments(&[Ty::Object])
1014    .extract_error_mode(ExtractErrorMode::NotImplemented)
1015    .return_self();
1016const __ISUB__: SlotDef = SlotDef::new("Py_nb_inplace_subtract", "binaryfunc")
1017    .arguments(&[Ty::Object])
1018    .extract_error_mode(ExtractErrorMode::NotImplemented)
1019    .return_self();
1020const __IMUL__: SlotDef = SlotDef::new("Py_nb_inplace_multiply", "binaryfunc")
1021    .arguments(&[Ty::Object])
1022    .extract_error_mode(ExtractErrorMode::NotImplemented)
1023    .return_self();
1024const __IMATMUL__: SlotDef = SlotDef::new("Py_nb_inplace_matrix_multiply", "binaryfunc")
1025    .arguments(&[Ty::Object])
1026    .extract_error_mode(ExtractErrorMode::NotImplemented)
1027    .return_self();
1028const __ITRUEDIV__: SlotDef = SlotDef::new("Py_nb_inplace_true_divide", "binaryfunc")
1029    .arguments(&[Ty::Object])
1030    .extract_error_mode(ExtractErrorMode::NotImplemented)
1031    .return_self();
1032const __IFLOORDIV__: SlotDef = SlotDef::new("Py_nb_inplace_floor_divide", "binaryfunc")
1033    .arguments(&[Ty::Object])
1034    .extract_error_mode(ExtractErrorMode::NotImplemented)
1035    .return_self();
1036const __IMOD__: SlotDef = SlotDef::new("Py_nb_inplace_remainder", "binaryfunc")
1037    .arguments(&[Ty::Object])
1038    .extract_error_mode(ExtractErrorMode::NotImplemented)
1039    .return_self();
1040const __IPOW__: SlotDef = SlotDef::new("Py_nb_inplace_power", "ipowfunc")
1041    .arguments(&[Ty::Object, Ty::IPowModulo])
1042    .extract_error_mode(ExtractErrorMode::NotImplemented)
1043    .return_self();
1044const __ILSHIFT__: SlotDef = SlotDef::new("Py_nb_inplace_lshift", "binaryfunc")
1045    .arguments(&[Ty::Object])
1046    .extract_error_mode(ExtractErrorMode::NotImplemented)
1047    .return_self();
1048const __IRSHIFT__: SlotDef = SlotDef::new("Py_nb_inplace_rshift", "binaryfunc")
1049    .arguments(&[Ty::Object])
1050    .extract_error_mode(ExtractErrorMode::NotImplemented)
1051    .return_self();
1052const __IAND__: SlotDef = SlotDef::new("Py_nb_inplace_and", "binaryfunc")
1053    .arguments(&[Ty::Object])
1054    .extract_error_mode(ExtractErrorMode::NotImplemented)
1055    .return_self();
1056const __IXOR__: SlotDef = SlotDef::new("Py_nb_inplace_xor", "binaryfunc")
1057    .arguments(&[Ty::Object])
1058    .extract_error_mode(ExtractErrorMode::NotImplemented)
1059    .return_self();
1060const __IOR__: SlotDef = SlotDef::new("Py_nb_inplace_or", "binaryfunc")
1061    .arguments(&[Ty::Object])
1062    .extract_error_mode(ExtractErrorMode::NotImplemented)
1063    .return_self();
1064const __GETBUFFER__: SlotDef = SlotDef::new("Py_bf_getbuffer", "getbufferproc")
1065    .arguments(&[Ty::PyBuffer, Ty::Int])
1066    .ret_ty(Ty::Int)
1067    .require_unsafe();
1068const __RELEASEBUFFER__: SlotDef = SlotDef::new("Py_bf_releasebuffer", "releasebufferproc")
1069    .arguments(&[Ty::PyBuffer])
1070    .ret_ty(Ty::Void)
1071    .require_unsafe();
1072const __CLEAR__: SlotDef = SlotDef::new("Py_tp_clear", "inquiry")
1073    .arguments(&[])
1074    .ret_ty(Ty::Int);
1075
1076#[derive(Clone, Copy)]
1077enum Ty {
1078    Object,
1079    MaybeNullObject,
1080    NonNullObject,
1081    IPowModulo,
1082    CompareOp,
1083    Int,
1084    PyHashT,
1085    PySsizeT,
1086    Void,
1087    PyBuffer,
1088}
1089
1090impl Ty {
1091    fn ffi_type(self, ctx: &Ctx) -> TokenStream {
1092        let Ctx {
1093            pyo3_path,
1094            output_span,
1095        } = ctx;
1096        let pyo3_path = pyo3_path.to_tokens_spanned(*output_span);
1097        match self {
1098            Ty::Object | Ty::MaybeNullObject => quote! { *mut #pyo3_path::ffi::PyObject },
1099            Ty::NonNullObject => quote! { ::std::ptr::NonNull<#pyo3_path::ffi::PyObject> },
1100            Ty::IPowModulo => quote! { #pyo3_path::impl_::pymethods::IPowModulo },
1101            Ty::Int | Ty::CompareOp => quote! { ::std::os::raw::c_int },
1102            Ty::PyHashT => quote! { #pyo3_path::ffi::Py_hash_t },
1103            Ty::PySsizeT => quote! { #pyo3_path::ffi::Py_ssize_t },
1104            Ty::Void => quote! { () },
1105            Ty::PyBuffer => quote! { *mut #pyo3_path::ffi::Py_buffer },
1106        }
1107    }
1108
1109    fn extract(
1110        self,
1111        ident: &syn::Ident,
1112        arg: &FnArg<'_>,
1113        extract_error_mode: ExtractErrorMode,
1114        holders: &mut Holders,
1115        ctx: &Ctx,
1116    ) -> TokenStream {
1117        let Ctx { pyo3_path, .. } = ctx;
1118        match self {
1119            Ty::Object => extract_object(
1120                extract_error_mode,
1121                holders,
1122                arg,
1123                quote! { #ident },
1124                ctx
1125            ),
1126            Ty::MaybeNullObject => extract_object(
1127                extract_error_mode,
1128                holders,
1129                arg,
1130                quote! {
1131                    if #ident.is_null() {
1132                        #pyo3_path::ffi::Py_None()
1133                    } else {
1134                        #ident
1135                    }
1136                },
1137                ctx
1138            ),
1139            Ty::NonNullObject => extract_object(
1140                extract_error_mode,
1141                holders,
1142                arg,
1143                quote! { #ident.as_ptr() },
1144                ctx
1145            ),
1146            Ty::IPowModulo => extract_object(
1147                extract_error_mode,
1148                holders,
1149                arg,
1150                quote! { #ident.as_ptr() },
1151                ctx
1152            ),
1153            Ty::CompareOp => extract_error_mode.handle_error(
1154                quote! {
1155                    #pyo3_path::class::basic::CompareOp::from_raw(#ident)
1156                        .ok_or_else(|| #pyo3_path::exceptions::PyValueError::new_err("invalid comparison operator"))
1157                },
1158                ctx
1159            ),
1160            Ty::PySsizeT => {
1161                let ty = arg.ty();
1162                extract_error_mode.handle_error(
1163                    quote! {
1164                            ::std::convert::TryInto::<#ty>::try_into(#ident).map_err(|e| #pyo3_path::exceptions::PyValueError::new_err(e.to_string()))
1165                    },
1166                    ctx
1167                )
1168            }
1169            // Just pass other types through unmodified
1170            Ty::PyBuffer | Ty::Int | Ty::PyHashT | Ty::Void => quote! { #ident },
1171        }
1172    }
1173}
1174
1175fn extract_object(
1176    extract_error_mode: ExtractErrorMode,
1177    holders: &mut Holders,
1178    arg: &FnArg<'_>,
1179    source_ptr: TokenStream,
1180    ctx: &Ctx,
1181) -> TokenStream {
1182    let Ctx { pyo3_path, .. } = ctx;
1183    let name = arg.name().unraw().to_string();
1184
1185    let extract = if let Some(FromPyWithAttribute {
1186        kw,
1187        value: extractor,
1188    }) = arg.from_py_with()
1189    {
1190        let extractor = quote_spanned! { kw.span =>
1191            { let from_py_with: fn(_) -> _ = #extractor; from_py_with }
1192        };
1193
1194        quote! {
1195            #pyo3_path::impl_::extract_argument::from_py_with(
1196                unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &#source_ptr).0 },
1197                #name,
1198                #extractor,
1199            )
1200        }
1201    } else {
1202        let holder = holders.push_holder(Span::call_site());
1203        let ty = arg.ty().clone().elide_lifetimes();
1204        quote! {{
1205            #[allow(unused_imports)]
1206            use #pyo3_path::impl_::pyclass::Probe as _;
1207            #pyo3_path::impl_::extract_argument::extract_argument::<
1208                _,
1209                { #pyo3_path::impl_::pyclass::IsOption::<#ty>::VALUE }
1210            >(
1211                unsafe { #pyo3_path::impl_::pymethods::BoundRef::ref_from_ptr(py, &#source_ptr).0 },
1212                &mut #holder,
1213                #name
1214            )
1215        }}
1216    };
1217
1218    let extracted = extract_error_mode.handle_error(extract, ctx);
1219    quote!(#extracted)
1220}
1221
1222enum ReturnMode {
1223    ReturnSelf,
1224    Conversion(TokenGenerator),
1225    SpecializedConversion(TokenGenerator, TokenGenerator),
1226}
1227
1228impl ReturnMode {
1229    fn return_call_output(&self, call: TokenStream, ctx: &Ctx) -> TokenStream {
1230        let Ctx { pyo3_path, .. } = ctx;
1231        match self {
1232            ReturnMode::Conversion(conversion) => {
1233                let conversion = TokenGeneratorCtx(*conversion, ctx);
1234                quote! {
1235                    let _result: #pyo3_path::PyResult<#conversion> = #pyo3_path::impl_::callback::convert(py, #call);
1236                    #pyo3_path::impl_::callback::convert(py, _result)
1237                }
1238            }
1239            ReturnMode::SpecializedConversion(traits, tag) => {
1240                let traits = TokenGeneratorCtx(*traits, ctx);
1241                let tag = TokenGeneratorCtx(*tag, ctx);
1242                quote! {
1243                    let _result = #call;
1244                    use #pyo3_path::impl_::pymethods::{#traits};
1245                    (&_result).#tag().convert(py, _result)
1246                }
1247            }
1248            ReturnMode::ReturnSelf => quote! {
1249                let _result: #pyo3_path::PyResult<()> = #pyo3_path::impl_::callback::convert(py, #call);
1250                _result?;
1251                #pyo3_path::ffi::Py_XINCREF(_raw_slf);
1252                ::std::result::Result::Ok(_raw_slf)
1253            },
1254        }
1255    }
1256}
1257
1258pub struct SlotDef {
1259    slot: StaticIdent,
1260    func_ty: StaticIdent,
1261    arguments: &'static [Ty],
1262    ret_ty: Ty,
1263    extract_error_mode: ExtractErrorMode,
1264    return_mode: Option<ReturnMode>,
1265    require_unsafe: bool,
1266}
1267
1268const NO_ARGUMENTS: &[Ty] = &[];
1269
1270impl SlotDef {
1271    const fn new(slot: &'static str, func_ty: &'static str) -> Self {
1272        SlotDef {
1273            slot: StaticIdent(slot),
1274            func_ty: StaticIdent(func_ty),
1275            arguments: NO_ARGUMENTS,
1276            ret_ty: Ty::Object,
1277            extract_error_mode: ExtractErrorMode::Raise,
1278            return_mode: None,
1279            require_unsafe: false,
1280        }
1281    }
1282
1283    const fn arguments(mut self, arguments: &'static [Ty]) -> Self {
1284        self.arguments = arguments;
1285        self
1286    }
1287
1288    const fn ret_ty(mut self, ret_ty: Ty) -> Self {
1289        self.ret_ty = ret_ty;
1290        self
1291    }
1292
1293    const fn return_conversion(mut self, return_conversion: TokenGenerator) -> Self {
1294        self.return_mode = Some(ReturnMode::Conversion(return_conversion));
1295        self
1296    }
1297
1298    const fn return_specialized_conversion(
1299        mut self,
1300        traits: TokenGenerator,
1301        tag: TokenGenerator,
1302    ) -> Self {
1303        self.return_mode = Some(ReturnMode::SpecializedConversion(traits, tag));
1304        self
1305    }
1306
1307    const fn extract_error_mode(mut self, extract_error_mode: ExtractErrorMode) -> Self {
1308        self.extract_error_mode = extract_error_mode;
1309        self
1310    }
1311
1312    const fn return_self(mut self) -> Self {
1313        self.return_mode = Some(ReturnMode::ReturnSelf);
1314        self
1315    }
1316
1317    const fn require_unsafe(mut self) -> Self {
1318        self.require_unsafe = true;
1319        self
1320    }
1321
1322    pub fn generate_type_slot(
1323        &self,
1324        cls: &syn::Type,
1325        spec: &FnSpec<'_>,
1326        method_name: &str,
1327        ctx: &Ctx,
1328    ) -> Result<MethodAndSlotDef> {
1329        let Ctx { pyo3_path, .. } = ctx;
1330        let SlotDef {
1331            slot,
1332            func_ty,
1333            arguments,
1334            extract_error_mode,
1335            ret_ty,
1336            return_mode,
1337            require_unsafe,
1338        } = self;
1339        if *require_unsafe {
1340            ensure_spanned!(
1341                spec.unsafety.is_some(),
1342                spec.name.span() => format!("`{}` must be `unsafe fn`", method_name)
1343            );
1344        }
1345        let arg_types: &Vec<_> = &arguments.iter().map(|arg| arg.ffi_type(ctx)).collect();
1346        let arg_idents: &Vec<_> = &(0..arguments.len())
1347            .map(|i| format_ident!("arg{}", i))
1348            .collect();
1349        let wrapper_ident = format_ident!("__pymethod_{}__", method_name);
1350        let ret_ty = ret_ty.ffi_type(ctx);
1351        let mut holders = Holders::new();
1352        let body = generate_method_body(
1353            cls,
1354            spec,
1355            arguments,
1356            *extract_error_mode,
1357            &mut holders,
1358            return_mode.as_ref(),
1359            ctx,
1360        )?;
1361        let name = spec.name;
1362        let holders = holders.init_holders(ctx);
1363        let associated_method = quote! {
1364            #[allow(non_snake_case)]
1365            unsafe fn #wrapper_ident(
1366                py: #pyo3_path::Python<'_>,
1367                _raw_slf: *mut #pyo3_path::ffi::PyObject,
1368                #(#arg_idents: #arg_types),*
1369            ) -> #pyo3_path::PyResult<#ret_ty> {
1370                let function = #cls::#name; // Shadow the method name to avoid #3017
1371                let _slf = _raw_slf;
1372                #holders
1373                #body
1374            }
1375        };
1376        let slot_def = quote! {{
1377            unsafe extern "C" fn trampoline(
1378                _slf: *mut #pyo3_path::ffi::PyObject,
1379                #(#arg_idents: #arg_types),*
1380            ) -> #ret_ty
1381            {
1382                #pyo3_path::impl_::trampoline:: #func_ty (
1383                    _slf,
1384                    #(#arg_idents,)*
1385                    #cls::#wrapper_ident
1386                )
1387            }
1388
1389            #pyo3_path::ffi::PyType_Slot {
1390                slot: #pyo3_path::ffi::#slot,
1391                pfunc: trampoline as #pyo3_path::ffi::#func_ty as _
1392            }
1393        }};
1394        Ok(MethodAndSlotDef {
1395            associated_method,
1396            slot_def,
1397        })
1398    }
1399}
1400
1401fn generate_method_body(
1402    cls: &syn::Type,
1403    spec: &FnSpec<'_>,
1404    arguments: &[Ty],
1405    extract_error_mode: ExtractErrorMode,
1406    holders: &mut Holders,
1407    return_mode: Option<&ReturnMode>,
1408    ctx: &Ctx,
1409) -> Result<TokenStream> {
1410    let Ctx { pyo3_path, .. } = ctx;
1411    let self_arg = spec
1412        .tp
1413        .self_arg(Some(cls), extract_error_mode, holders, ctx);
1414    let rust_name = spec.name;
1415    let args = extract_proto_arguments(spec, arguments, extract_error_mode, holders, ctx)?;
1416    let call = quote! { #cls::#rust_name(#self_arg #(#args),*) };
1417    Ok(if let Some(return_mode) = return_mode {
1418        return_mode.return_call_output(call, ctx)
1419    } else {
1420        quote! {
1421            let result = #call;
1422            #pyo3_path::impl_::callback::convert(py, result)
1423        }
1424    })
1425}
1426
1427struct SlotFragmentDef {
1428    fragment: &'static str,
1429    arguments: &'static [Ty],
1430    extract_error_mode: ExtractErrorMode,
1431    ret_ty: Ty,
1432}
1433
1434impl SlotFragmentDef {
1435    const fn new(fragment: &'static str, arguments: &'static [Ty]) -> Self {
1436        SlotFragmentDef {
1437            fragment,
1438            arguments,
1439            extract_error_mode: ExtractErrorMode::Raise,
1440            ret_ty: Ty::Void,
1441        }
1442    }
1443
1444    const fn extract_error_mode(mut self, extract_error_mode: ExtractErrorMode) -> Self {
1445        self.extract_error_mode = extract_error_mode;
1446        self
1447    }
1448
1449    const fn ret_ty(mut self, ret_ty: Ty) -> Self {
1450        self.ret_ty = ret_ty;
1451        self
1452    }
1453
1454    fn generate_pyproto_fragment(
1455        &self,
1456        cls: &syn::Type,
1457        spec: &FnSpec<'_>,
1458        ctx: &Ctx,
1459    ) -> Result<TokenStream> {
1460        let Ctx { pyo3_path, .. } = ctx;
1461        let SlotFragmentDef {
1462            fragment,
1463            arguments,
1464            extract_error_mode,
1465            ret_ty,
1466        } = self;
1467        let fragment_trait = format_ident!("PyClass{}SlotFragment", fragment);
1468        let method = syn::Ident::new(fragment, Span::call_site());
1469        let wrapper_ident = format_ident!("__pymethod_{}__", fragment);
1470        let arg_types: &Vec<_> = &arguments.iter().map(|arg| arg.ffi_type(ctx)).collect();
1471        let arg_idents: &Vec<_> = &(0..arguments.len())
1472            .map(|i| format_ident!("arg{}", i))
1473            .collect();
1474        let mut holders = Holders::new();
1475        let body = generate_method_body(
1476            cls,
1477            spec,
1478            arguments,
1479            *extract_error_mode,
1480            &mut holders,
1481            None,
1482            ctx,
1483        )?;
1484        let ret_ty = ret_ty.ffi_type(ctx);
1485        let holders = holders.init_holders(ctx);
1486        Ok(quote! {
1487            impl #cls {
1488                #[allow(non_snake_case)]
1489                unsafe fn #wrapper_ident(
1490                    py: #pyo3_path::Python,
1491                    _raw_slf: *mut #pyo3_path::ffi::PyObject,
1492                    #(#arg_idents: #arg_types),*
1493                ) -> #pyo3_path::PyResult<#ret_ty> {
1494                    let _slf = _raw_slf;
1495                    #holders
1496                    #body
1497                }
1498            }
1499
1500            impl #pyo3_path::impl_::pyclass::#fragment_trait<#cls> for #pyo3_path::impl_::pyclass::PyClassImplCollector<#cls> {
1501
1502                #[inline]
1503                unsafe fn #method(
1504                    self,
1505                    py: #pyo3_path::Python,
1506                    _raw_slf: *mut #pyo3_path::ffi::PyObject,
1507                    #(#arg_idents: #arg_types),*
1508                ) -> #pyo3_path::PyResult<#ret_ty> {
1509                    #cls::#wrapper_ident(py, _raw_slf, #(#arg_idents),*)
1510                }
1511            }
1512        })
1513    }
1514}
1515
1516const __GETATTRIBUTE__: SlotFragmentDef =
1517    SlotFragmentDef::new("__getattribute__", &[Ty::Object]).ret_ty(Ty::Object);
1518const __GETATTR__: SlotFragmentDef =
1519    SlotFragmentDef::new("__getattr__", &[Ty::Object]).ret_ty(Ty::Object);
1520const __SETATTR__: SlotFragmentDef =
1521    SlotFragmentDef::new("__setattr__", &[Ty::Object, Ty::NonNullObject]);
1522const __DELATTR__: SlotFragmentDef = SlotFragmentDef::new("__delattr__", &[Ty::Object]);
1523const __SET__: SlotFragmentDef = SlotFragmentDef::new("__set__", &[Ty::Object, Ty::NonNullObject]);
1524const __DELETE__: SlotFragmentDef = SlotFragmentDef::new("__delete__", &[Ty::Object]);
1525const __SETITEM__: SlotFragmentDef =
1526    SlotFragmentDef::new("__setitem__", &[Ty::Object, Ty::NonNullObject]);
1527const __DELITEM__: SlotFragmentDef = SlotFragmentDef::new("__delitem__", &[Ty::Object]);
1528
1529macro_rules! binary_num_slot_fragment_def {
1530    ($ident:ident, $name:literal) => {
1531        const $ident: SlotFragmentDef = SlotFragmentDef::new($name, &[Ty::Object])
1532            .extract_error_mode(ExtractErrorMode::NotImplemented)
1533            .ret_ty(Ty::Object);
1534    };
1535}
1536
1537binary_num_slot_fragment_def!(__ADD__, "__add__");
1538binary_num_slot_fragment_def!(__RADD__, "__radd__");
1539binary_num_slot_fragment_def!(__SUB__, "__sub__");
1540binary_num_slot_fragment_def!(__RSUB__, "__rsub__");
1541binary_num_slot_fragment_def!(__MUL__, "__mul__");
1542binary_num_slot_fragment_def!(__RMUL__, "__rmul__");
1543binary_num_slot_fragment_def!(__MATMUL__, "__matmul__");
1544binary_num_slot_fragment_def!(__RMATMUL__, "__rmatmul__");
1545binary_num_slot_fragment_def!(__FLOORDIV__, "__floordiv__");
1546binary_num_slot_fragment_def!(__RFLOORDIV__, "__rfloordiv__");
1547binary_num_slot_fragment_def!(__TRUEDIV__, "__truediv__");
1548binary_num_slot_fragment_def!(__RTRUEDIV__, "__rtruediv__");
1549binary_num_slot_fragment_def!(__DIVMOD__, "__divmod__");
1550binary_num_slot_fragment_def!(__RDIVMOD__, "__rdivmod__");
1551binary_num_slot_fragment_def!(__MOD__, "__mod__");
1552binary_num_slot_fragment_def!(__RMOD__, "__rmod__");
1553binary_num_slot_fragment_def!(__LSHIFT__, "__lshift__");
1554binary_num_slot_fragment_def!(__RLSHIFT__, "__rlshift__");
1555binary_num_slot_fragment_def!(__RSHIFT__, "__rshift__");
1556binary_num_slot_fragment_def!(__RRSHIFT__, "__rrshift__");
1557binary_num_slot_fragment_def!(__AND__, "__and__");
1558binary_num_slot_fragment_def!(__RAND__, "__rand__");
1559binary_num_slot_fragment_def!(__XOR__, "__xor__");
1560binary_num_slot_fragment_def!(__RXOR__, "__rxor__");
1561binary_num_slot_fragment_def!(__OR__, "__or__");
1562binary_num_slot_fragment_def!(__ROR__, "__ror__");
1563
1564const __POW__: SlotFragmentDef = SlotFragmentDef::new("__pow__", &[Ty::Object, Ty::Object])
1565    .extract_error_mode(ExtractErrorMode::NotImplemented)
1566    .ret_ty(Ty::Object);
1567const __RPOW__: SlotFragmentDef = SlotFragmentDef::new("__rpow__", &[Ty::Object, Ty::Object])
1568    .extract_error_mode(ExtractErrorMode::NotImplemented)
1569    .ret_ty(Ty::Object);
1570
1571const __LT__: SlotFragmentDef = SlotFragmentDef::new("__lt__", &[Ty::Object])
1572    .extract_error_mode(ExtractErrorMode::NotImplemented)
1573    .ret_ty(Ty::Object);
1574const __LE__: SlotFragmentDef = SlotFragmentDef::new("__le__", &[Ty::Object])
1575    .extract_error_mode(ExtractErrorMode::NotImplemented)
1576    .ret_ty(Ty::Object);
1577const __EQ__: SlotFragmentDef = SlotFragmentDef::new("__eq__", &[Ty::Object])
1578    .extract_error_mode(ExtractErrorMode::NotImplemented)
1579    .ret_ty(Ty::Object);
1580const __NE__: SlotFragmentDef = SlotFragmentDef::new("__ne__", &[Ty::Object])
1581    .extract_error_mode(ExtractErrorMode::NotImplemented)
1582    .ret_ty(Ty::Object);
1583const __GT__: SlotFragmentDef = SlotFragmentDef::new("__gt__", &[Ty::Object])
1584    .extract_error_mode(ExtractErrorMode::NotImplemented)
1585    .ret_ty(Ty::Object);
1586const __GE__: SlotFragmentDef = SlotFragmentDef::new("__ge__", &[Ty::Object])
1587    .extract_error_mode(ExtractErrorMode::NotImplemented)
1588    .ret_ty(Ty::Object);
1589
1590fn extract_proto_arguments(
1591    spec: &FnSpec<'_>,
1592    proto_args: &[Ty],
1593    extract_error_mode: ExtractErrorMode,
1594    holders: &mut Holders,
1595    ctx: &Ctx,
1596) -> Result<Vec<TokenStream>> {
1597    let mut args = Vec::with_capacity(spec.signature.arguments.len());
1598    let mut non_python_args = 0;
1599
1600    for arg in &spec.signature.arguments {
1601        if let FnArg::Py(..) = arg {
1602            args.push(quote! { py });
1603        } else {
1604            let ident = syn::Ident::new(&format!("arg{}", non_python_args), Span::call_site());
1605            let conversions = proto_args.get(non_python_args)
1606                .ok_or_else(|| err_spanned!(arg.ty().span() => format!("Expected at most {} non-python arguments", proto_args.len())))?
1607                .extract(&ident, arg, extract_error_mode, holders, ctx);
1608            non_python_args += 1;
1609            args.push(conversions);
1610        }
1611    }
1612
1613    if non_python_args != proto_args.len() {
1614        bail_spanned!(spec.name.span() => format!("Expected {} arguments, got {}", proto_args.len(), non_python_args));
1615    }
1616    Ok(args)
1617}
1618
1619struct StaticIdent(&'static str);
1620
1621impl ToTokens for StaticIdent {
1622    fn to_tokens(&self, tokens: &mut TokenStream) {
1623        syn::Ident::new(self.0, Span::call_site()).to_tokens(tokens)
1624    }
1625}
1626
1627#[derive(Clone, Copy)]
1628struct TokenGenerator(fn(&Ctx) -> TokenStream);
1629
1630struct TokenGeneratorCtx<'ctx>(TokenGenerator, &'ctx Ctx);
1631
1632impl ToTokens for TokenGeneratorCtx<'_> {
1633    fn to_tokens(&self, tokens: &mut TokenStream) {
1634        let Self(TokenGenerator(gen), ctx) = self;
1635        (gen)(ctx).to_tokens(tokens)
1636    }
1637}
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here