Skip to main content

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