forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
93 lines (81 loc) · 3.08 KB
/
lib.rs
File metadata and controls
93 lines (81 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![recursion_limit = "128"]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
#![doc(html_root_url = "https://docs.rs/rustpython-derive/")]
use proc_macro::TokenStream;
use rustpython_derive_impl as derive_impl;
use syn::parse_macro_input;
#[proc_macro_derive(FromArgs, attributes(pyarg))]
pub fn derive_from_args(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input);
derive_impl::derive_from_args(input).into()
}
#[proc_macro_attribute]
pub fn pyclass(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = parse_macro_input!(attr);
let item = parse_macro_input!(item);
derive_impl::pyclass(attr, item).into()
}
/// This macro serves a goal of generating multiple
/// `BaseException` / `Exception`
/// subtypes in a uniform and convenient manner.
/// It looks like `SimpleExtendsException` in `CPython`.
/// <https://github.com/python/cpython/blob/main/Objects/exceptions.c>
///
/// We need `ctx` to be ready to add
/// `properties` / `custom` constructors / slots / methods etc.
/// So, we use `extend_class!` macro as the second
/// step in exception type definition.
#[proc_macro]
pub fn define_exception(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input);
derive_impl::define_exception(input).into()
}
/// Helper macro to define `Exception` types.
/// More-or-less is an alias to `pyclass` macro.
#[proc_macro_attribute]
pub fn pyexception(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = parse_macro_input!(attr);
let item = parse_macro_input!(item);
derive_impl::pyexception(attr, item).into()
}
#[proc_macro_attribute]
pub fn pymodule(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr = parse_macro_input!(attr);
let item = parse_macro_input!(item);
derive_impl::pymodule(attr, item).into()
}
#[proc_macro_derive(PyStructSequence)]
pub fn pystruct_sequence(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input);
derive_impl::pystruct_sequence(input).into()
}
#[proc_macro_derive(TryIntoPyStructSequence)]
pub fn pystruct_sequence_try_from_object(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input);
derive_impl::pystruct_sequence_try_from_object(input).into()
}
struct Compiler;
impl derive_impl::Compiler for Compiler {
fn compile(
&self,
source: &str,
mode: rustpython_compiler::Mode,
module_name: String,
) -> Result<rustpython_compiler::CodeObject, Box<dyn std::error::Error>> {
use rustpython_compiler::{compile, CompileOpts};
Ok(compile(source, mode, module_name, CompileOpts::default())?)
}
}
#[proc_macro]
pub fn py_compile(input: TokenStream) -> TokenStream {
derive_impl::py_compile(input.into(), &Compiler).into()
}
#[proc_macro]
pub fn py_freeze(input: TokenStream) -> TokenStream {
derive_impl::py_freeze(input.into(), &Compiler).into()
}
#[proc_macro_derive(PyPayload)]
pub fn pypayload(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input);
derive_impl::pypayload(input).into()
}