pyo3_macros/
lib.rs

1//! This crate declares only the proc macro attributes, as a crate defining proc macro attributes
2//! must not contain any other public items.
3
4#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
5use proc_macro::TokenStream;
6use proc_macro2::TokenStream as TokenStream2;
7use pyo3_macros_backend::{
8    build_derive_from_pyobject, build_derive_into_pyobject, build_py_class, build_py_enum,
9    build_py_function, build_py_methods, pymodule_function_impl, pymodule_module_impl, PyClassArgs,
10    PyClassMethodsType, PyFunctionOptions, PyModuleOptions,
11};
12use quote::quote;
13use syn::{parse_macro_input, Item};
14
15/// A proc macro used to implement Python modules.
16///
17/// The name of the module will be taken from the function name, unless `#[pyo3(name = "my_name")]`
18/// is also annotated on the function to override the name. **Important**: the module name should
19/// match the `lib.name` setting in `Cargo.toml`, so that Python is able to import the module
20/// without needing a custom import loader.
21///
22/// Functions annotated with `#[pymodule]` can also be annotated with the following:
23///
24/// |  Annotation  |  Description |
25/// | :-  | :- |
26/// | `#[pyo3(name = "...")]` | Defines the name of the module in Python. |
27/// | `#[pyo3(submodule)]`    | Skips adding a `PyInit_` FFI symbol to the compiled binary. |
28/// | `#[pyo3(module = "...")]` | Defines the Python `dotted.path` to the parent module for use in introspection. |
29/// | `#[pyo3(crate = "pyo3")]` | Defines the path to PyO3 to use code generated by the macro. |
30///
31/// For more on creating Python modules see the [module section of the guide][1].
32///
33/// Due to technical limitations on how `#[pymodule]` is implemented, a function marked
34/// `#[pymodule]` cannot have a module with the same name in the same scope. (The
35/// `#[pymodule]` implementation generates a hidden module with the same name containing
36/// metadata about the module, which is used by `wrap_pymodule!`).
37///
38#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/module.html")]
39#[proc_macro_attribute]
40pub fn pymodule(args: TokenStream, input: TokenStream) -> TokenStream {
41    let options = parse_macro_input!(args as PyModuleOptions);
42
43    let mut ast = parse_macro_input!(input as Item);
44    let expanded = match &mut ast {
45        Item::Mod(module) => {
46            match pymodule_module_impl(module, options) {
47                // #[pymodule] on a module will rebuild the original ast, so we don't emit it here
48                Ok(expanded) => return expanded.into(),
49                Err(e) => Err(e),
50            }
51        }
52        Item::Fn(function) => pymodule_function_impl(function, options),
53        unsupported => Err(syn::Error::new_spanned(
54            unsupported,
55            "#[pymodule] only supports modules and functions.",
56        )),
57    }
58    .unwrap_or_compile_error();
59
60    quote!(
61        #ast
62        #expanded
63    )
64    .into()
65}
66
67#[proc_macro_attribute]
68pub fn pyclass(attr: TokenStream, input: TokenStream) -> TokenStream {
69    let item = parse_macro_input!(input as Item);
70    match item {
71        Item::Struct(struct_) => pyclass_impl(attr, struct_, methods_type()),
72        Item::Enum(enum_) => pyclass_enum_impl(attr, enum_, methods_type()),
73        unsupported => {
74            syn::Error::new_spanned(unsupported, "#[pyclass] only supports structs and enums.")
75                .into_compile_error()
76                .into()
77        }
78    }
79}
80
81/// A proc macro used to expose methods to Python.
82///
83/// Methods within a `#[pymethods]` block can be annotated with  as well as the following:
84///
85/// |  Annotation  |  Description |
86/// | :-  | :- |
87/// | [`#[new]`][4]  | Defines the class constructor, like Python's `__new__` method. |
88/// | [`#[getter]`][5] and [`#[setter]`][5] | These define getters and setters, similar to Python's `@property` decorator. This is useful for getters/setters that require computation or side effects; if that is not the case consider using [`#[pyo3(get, set)]`][12] on the struct's field(s).|
89/// | [`#[staticmethod]`][6]| Defines the method as a staticmethod, like Python's `@staticmethod` decorator.|
90/// | [`#[classmethod]`][7]  | Defines the method as a classmethod, like Python's `@classmethod` decorator.|
91/// | [`#[classattr]`][9]  | Defines a class variable. |
92/// | [`#[args]`][10]  | Deprecated way to define a method's default arguments and allows the function to receive `*args` and `**kwargs`. Use `#[pyo3(signature = (...))]` instead. |
93/// | <nobr>[`#[pyo3(<option> = <value>)`][11]</nobr> | Any of the `#[pyo3]` options supported on [`macro@pyfunction`]. |
94///
95/// For more on creating class methods,
96/// see the [class section of the guide][1].
97///
98/// If the [`multiple-pymethods`][2] feature is enabled, it is possible to implement
99/// multiple `#[pymethods]` blocks for a single `#[pyclass]`.
100/// This will add a transitive dependency on the [`inventory`][3] crate.
101///
102#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#instance-methods")]
103#[doc = concat!("[2]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/features.html#multiple-pymethods")]
104/// [3]: https://docs.rs/inventory/
105#[doc = concat!("[4]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#constructor")]
106#[doc = concat!("[5]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#object-properties-using-getter-and-setter")]
107#[doc = concat!("[6]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#static-methods")]
108#[doc = concat!("[7]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#class-methods")]
109#[doc = concat!("[8]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#callable-objects")]
110#[doc = concat!("[9]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#class-attributes")]
111#[doc = concat!("[10]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#method-arguments")]
112#[doc = concat!("[11]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/function.html#function-options")]
113#[doc = concat!("[12]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/class.html#object-properties-using-pyo3get-set")]
114#[proc_macro_attribute]
115pub fn pymethods(attr: TokenStream, input: TokenStream) -> TokenStream {
116    let methods_type = if cfg!(feature = "multiple-pymethods") {
117        PyClassMethodsType::Inventory
118    } else {
119        PyClassMethodsType::Specialization
120    };
121    pymethods_impl(attr, input, methods_type)
122}
123
124/// A proc macro used to expose Rust functions to Python.
125///
126/// Functions annotated with `#[pyfunction]` can also be annotated with the following `#[pyo3]`
127/// options:
128///
129/// |  Annotation  |  Description |
130/// | :-  | :- |
131/// | `#[pyo3(name = "...")]` | Defines the name of the function in Python. |
132/// | `#[pyo3(text_signature = "...")]` | Defines the `__text_signature__` attribute of the function in Python. |
133/// | `#[pyo3(pass_module)]` | Passes the module containing the function as a `&PyModule` first argument to the function. |
134/// | `#[pyo3(warn(message = "...", category = ...))]` | Generate warning given a message and a category |
135///
136/// For more on exposing functions see the [function section of the guide][1].
137///
138/// Due to technical limitations on how `#[pyfunction]` is implemented, a function marked
139/// `#[pyfunction]` cannot have a module with the same name in the same scope. (The
140/// `#[pyfunction]` implementation generates a hidden module with the same name containing
141/// metadata about the function, which is used by `wrap_pyfunction!`).
142///
143#[doc = concat!("[1]: https://pyo3.rs/v", env!("CARGO_PKG_VERSION"), "/function.html")]
144#[proc_macro_attribute]
145pub fn pyfunction(attr: TokenStream, input: TokenStream) -> TokenStream {
146    let mut ast = parse_macro_input!(input as syn::ItemFn);
147    let options = parse_macro_input!(attr as PyFunctionOptions);
148
149    let expanded = build_py_function(&mut ast, options).unwrap_or_compile_error();
150
151    quote!(
152        #ast
153        #expanded
154    )
155    .into()
156}
157
158#[proc_macro_derive(IntoPyObject, attributes(pyo3))]
159pub fn derive_into_py_object(item: TokenStream) -> TokenStream {
160    let ast = parse_macro_input!(item as syn::DeriveInput);
161    let expanded = build_derive_into_pyobject::<false>(&ast).unwrap_or_compile_error();
162    quote!(
163        #expanded
164    )
165    .into()
166}
167
168#[proc_macro_derive(IntoPyObjectRef, attributes(pyo3))]
169pub fn derive_into_py_object_ref(item: TokenStream) -> TokenStream {
170    let ast = parse_macro_input!(item as syn::DeriveInput);
171    let expanded =
172        pyo3_macros_backend::build_derive_into_pyobject::<true>(&ast).unwrap_or_compile_error();
173    quote!(
174        #expanded
175    )
176    .into()
177}
178
179#[proc_macro_derive(FromPyObject, attributes(pyo3))]
180pub fn derive_from_py_object(item: TokenStream) -> TokenStream {
181    let ast = parse_macro_input!(item as syn::DeriveInput);
182    let expanded = build_derive_from_pyobject(&ast).unwrap_or_compile_error();
183    quote!(
184        #expanded
185    )
186    .into()
187}
188
189fn pyclass_impl(
190    attrs: TokenStream,
191    mut ast: syn::ItemStruct,
192    methods_type: PyClassMethodsType,
193) -> TokenStream {
194    let args = parse_macro_input!(attrs with PyClassArgs::parse_struct_args);
195    let expanded = build_py_class(&mut ast, args, methods_type).unwrap_or_compile_error();
196
197    quote!(
198        #ast
199        #expanded
200    )
201    .into()
202}
203
204fn pyclass_enum_impl(
205    attrs: TokenStream,
206    mut ast: syn::ItemEnum,
207    methods_type: PyClassMethodsType,
208) -> TokenStream {
209    let args = parse_macro_input!(attrs with PyClassArgs::parse_enum_args);
210    let expanded = build_py_enum(&mut ast, args, methods_type).unwrap_or_compile_error();
211
212    quote!(
213        #ast
214        #expanded
215    )
216    .into()
217}
218
219fn pymethods_impl(
220    attr: TokenStream,
221    input: TokenStream,
222    methods_type: PyClassMethodsType,
223) -> TokenStream {
224    let mut ast = parse_macro_input!(input as syn::ItemImpl);
225    // Apply all options as a #[pyo3] attribute on the ItemImpl
226    // e.g. #[pymethods(crate = "crate")] impl Foo { }
227    // -> #[pyo3(crate = "crate")] impl Foo { }
228    let attr: TokenStream2 = attr.into();
229    ast.attrs.push(syn::parse_quote!( #[pyo3(#attr)] ));
230    let expanded = build_py_methods(&mut ast, methods_type).unwrap_or_compile_error();
231
232    quote!(
233        #ast
234        #expanded
235    )
236    .into()
237}
238
239fn methods_type() -> PyClassMethodsType {
240    if cfg!(feature = "multiple-pymethods") {
241        PyClassMethodsType::Inventory
242    } else {
243        PyClassMethodsType::Specialization
244    }
245}
246
247trait UnwrapOrCompileError {
248    fn unwrap_or_compile_error(self) -> TokenStream2;
249}
250
251impl UnwrapOrCompileError for syn::Result<TokenStream2> {
252    fn unwrap_or_compile_error(self) -> TokenStream2 {
253        self.unwrap_or_else(|e| e.into_compile_error())
254    }
255}