rustpython_derive/
lib.rs

1#![recursion_limit = "128"]
2#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
3#![doc(html_root_url = "https://docs.rs/rustpython-derive/")]
4
5use proc_macro::TokenStream;
6use rustpython_derive_impl as derive_impl;
7use syn::parse_macro_input;
8
9#[proc_macro_derive(FromArgs, attributes(pyarg))]
10pub fn derive_from_args(input: TokenStream) -> TokenStream {
11    let input = parse_macro_input!(input);
12    derive_impl::derive_from_args(input).into()
13}
14
15#[proc_macro_attribute]
16pub fn pyclass(attr: TokenStream, item: TokenStream) -> TokenStream {
17    let attr = parse_macro_input!(attr);
18    let item = parse_macro_input!(item);
19    derive_impl::pyclass(attr, item).into()
20}
21
22/// Helper macro to define `Exception` types.
23/// More-or-less is an alias to `pyclass` macro.
24///
25/// This macro serves a goal of generating multiple
26/// `BaseException` / `Exception`
27/// subtypes in a uniform and convenient manner.
28/// It looks like `SimpleExtendsException` in `CPython`.
29/// <https://github.com/python/cpython/blob/main/Objects/exceptions.c>
30#[proc_macro_attribute]
31pub fn pyexception(attr: TokenStream, item: TokenStream) -> TokenStream {
32    let attr = parse_macro_input!(attr);
33    let item = parse_macro_input!(item);
34    derive_impl::pyexception(attr, item).into()
35}
36
37#[proc_macro_attribute]
38pub fn pymodule(attr: TokenStream, item: TokenStream) -> TokenStream {
39    let attr = parse_macro_input!(attr);
40    let item = parse_macro_input!(item);
41    derive_impl::pymodule(attr, item).into()
42}
43
44#[proc_macro_derive(PyStructSequence)]
45pub fn pystruct_sequence(input: TokenStream) -> TokenStream {
46    let input = parse_macro_input!(input);
47    derive_impl::pystruct_sequence(input).into()
48}
49
50#[proc_macro_derive(TryIntoPyStructSequence)]
51pub fn pystruct_sequence_try_from_object(input: TokenStream) -> TokenStream {
52    let input = parse_macro_input!(input);
53    derive_impl::pystruct_sequence_try_from_object(input).into()
54}
55
56struct Compiler;
57impl derive_impl::Compiler for Compiler {
58    fn compile(
59        &self,
60        source: &str,
61        mode: rustpython_compiler::Mode,
62        module_name: String,
63    ) -> Result<rustpython_compiler::CodeObject, Box<dyn std::error::Error>> {
64        use rustpython_compiler::{compile, CompileOpts};
65        Ok(compile(source, mode, module_name, CompileOpts::default())?)
66    }
67}
68
69#[proc_macro]
70pub fn py_compile(input: TokenStream) -> TokenStream {
71    derive_impl::py_compile(input.into(), &Compiler).into()
72}
73
74#[proc_macro]
75pub fn py_freeze(input: TokenStream) -> TokenStream {
76    derive_impl::py_freeze(input.into(), &Compiler).into()
77}
78
79#[proc_macro_derive(PyPayload)]
80pub fn pypayload(input: TokenStream) -> TokenStream {
81    let input = parse_macro_input!(input);
82    derive_impl::pypayload(input).into()
83}
84
85/// use on struct with named fields like `struct A{x:PyRef<B>, y:PyRef<C>}` to impl `Traverse` for datatype.
86///
87/// use `#[pytraverse(skip)]` on fields you wish not to trace
88///
89/// add `trace` attr to `#[pyclass]` to make it impl `MaybeTraverse` that will call `Traverse`'s `traverse` method so make it
90/// traceable(Even from type-erased PyObject)(i.e. write `#[pyclass(trace)]`).
91/// # Example
92/// ```rust, ignore
93/// #[pyclass(module = false, traverse)]
94/// #[derive(Default, Traverse)]
95/// pub struct PyList {
96///     elements: PyRwLock<Vec<PyObjectRef>>,
97///     #[pytraverse(skip)]
98///     len: AtomicCell<usize>,
99/// }
100/// ```
101/// This create both `MaybeTraverse` that call `Traverse`'s `traverse` method and `Traverse` that impl `Traverse`
102/// for `PyList` which call elements' `traverse` method and ignore `len` field.
103#[proc_macro_derive(Traverse, attributes(pytraverse))]
104pub fn pytraverse(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
105    let item = parse_macro_input!(item);
106    derive_impl::pytraverse(item).into()
107}