diff --git a/derive/src/lib.rs b/derive/src/lib.rs index a847f01e6..8952d018b 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -17,6 +17,7 @@ mod from_args; mod pyclass; mod pymodule; mod pystructseq; +mod pyvalue; use error::{extract_spans, Diagnostic}; use proc_macro::TokenStream; @@ -95,3 +96,9 @@ pub fn py_compile(input: TokenStream) -> TokenStream { pub fn py_freeze(input: TokenStream) -> TokenStream { result_to_tokens(compile_bytecode::impl_py_freeze(input.into())) } + +#[proc_macro_derive(PyValue)] +pub fn pyvalue(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + result_to_tokens(pyvalue::impl_pyvalue(input)) +} diff --git a/derive/src/pyvalue.rs b/derive/src/pyvalue.rs new file mode 100644 index 000000000..a3bb0b3b0 --- /dev/null +++ b/derive/src/pyvalue.rs @@ -0,0 +1,17 @@ +use super::Diagnostic; +use proc_macro2::TokenStream; +use quote::quote; +use syn::DeriveInput; + +pub(crate) fn impl_pyvalue(input: DeriveInput) -> std::result::Result { + let ty = &input.ident; + + let ret = quote! { + impl ::rustpython_vm::PyValue for #ty { + fn class(_vm: &VirtualMachine) -> &PyTypeRef { + Self::static_type() + } + } + }; + Ok(ret) +} diff --git a/vm/src/stdlib/array.rs b/vm/src/stdlib/array.rs index a9a20b7e7..ac0f9fd58 100644 --- a/vm/src/stdlib/array.rs +++ b/vm/src/stdlib/array.rs @@ -589,7 +589,7 @@ mod array { #[pyattr] #[pyclass(name = "array")] - #[derive(Debug)] + #[derive(Debug, PyValue)] pub struct PyArray { array: PyRwLock, exports: AtomicCell, @@ -597,12 +597,6 @@ mod array { pub type PyArrayRef = PyRef; - impl PyValue for PyArray { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl From for PyArray { fn from(array: ArrayContentType) -> Self { PyArray { @@ -1198,18 +1192,12 @@ mod array { #[pyattr] #[pyclass(name = "array_iterator")] - #[derive(Debug)] + #[derive(Debug, PyValue)] pub struct PyArrayIter { position: AtomicCell, array: PyArrayRef, } - impl PyValue for PyArrayIter { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(PyIter))] impl PyArrayIter {} diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs index f867c4b9a..9c7037665 100644 --- a/vm/src/stdlib/ast.rs +++ b/vm/src/stdlib/ast.rs @@ -44,7 +44,7 @@ fn get_node_field_opt( } #[pyclass(module = "_ast", name = "AST")] -#[derive(Debug)] +#[derive(Debug, PyValue)] pub(crate) struct AstNode; #[pyimpl(flags(HAS_DICT))] @@ -84,12 +84,6 @@ impl AstNode { const MODULE_NAME: &str = "_ast"; pub const PY_COMPILE_FLAG_AST_ONLY: i32 = 0x0400; -impl PyValue for AstNode { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - trait Node: Sized { fn ast_to_object(self, vm: &VirtualMachine) -> PyObjectRef; fn ast_from_object(vm: &VirtualMachine, object: PyObjectRef) -> PyResult; diff --git a/vm/src/stdlib/collections.rs b/vm/src/stdlib/collections.rs index a48843744..0f726c7c4 100644 --- a/vm/src/stdlib/collections.rs +++ b/vm/src/stdlib/collections.rs @@ -26,7 +26,7 @@ mod _collections { #[pyattr] #[pyclass(name = "deque")] - #[derive(Debug, Default)] + #[derive(Debug, Default, PyValue)] struct PyDeque { deque: PyRwLock>, maxlen: Option, @@ -35,12 +35,6 @@ mod _collections { type PyDequeRef = PyRef; - impl PyValue for PyDeque { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct PyDequeOptions { #[pyarg(any, optional)] @@ -577,7 +571,7 @@ mod _collections { #[pyattr] #[pyclass(name = "_deque_iterator")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyDequeIterator { position: AtomicCell, status: AtomicCell, @@ -585,12 +579,6 @@ mod _collections { deque: PyDequeRef, } - impl PyValue for PyDequeIterator { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct DequeIterArgs { #[pyarg(positional)] @@ -680,7 +668,7 @@ mod _collections { #[pyattr] #[pyclass(name = "_deque_reverse_iterator")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyReverseDequeIterator { position: AtomicCell, status: AtomicCell, @@ -688,12 +676,6 @@ mod _collections { deque: PyDequeRef, } - impl PyValue for PyReverseDequeIterator { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyReverseDequeIterator { type Args = (DequeIterArgs, KwArgs); diff --git a/vm/src/stdlib/csv.rs b/vm/src/stdlib/csv.rs index a5058d1b0..38971ebb2 100644 --- a/vm/src/stdlib/csv.rs +++ b/vm/src/stdlib/csv.rs @@ -86,6 +86,7 @@ struct ReadState { } #[pyclass(module = "_csv", name = "reader")] +#[derive(PyValue)] struct Reader { iter: PyObjectRef, state: PyMutex, @@ -97,12 +98,6 @@ impl fmt::Debug for Reader { } } -impl PyValue for Reader { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl(with(PyIter))] impl Reader {} impl PyIter for Reader { @@ -193,6 +188,7 @@ struct WriteState { } #[pyclass(module = "_csv", name = "writer")] +#[derive(PyValue)] struct Writer { write: PyObjectRef, state: PyMutex, @@ -204,12 +200,6 @@ impl fmt::Debug for Writer { } } -impl PyValue for Writer { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl Writer { #[pymethod] diff --git a/vm/src/stdlib/hashlib.rs b/vm/src/stdlib/hashlib.rs index 91be45fd9..def57e0e4 100644 --- a/vm/src/stdlib/hashlib.rs +++ b/vm/src/stdlib/hashlib.rs @@ -19,6 +19,7 @@ mod hashlib { #[pyattr] #[pyclass(module = "hashlib", name = "hasher")] + #[derive(PyValue)] struct PyHasher { name: String, buffer: PyRwLock, @@ -30,12 +31,6 @@ mod hashlib { } } - impl PyValue for PyHasher { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl] impl PyHasher { fn new(name: &str, d: HashWrapper) -> Self { diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 9340a26eb..c90c5bad8 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -1638,15 +1638,10 @@ mod _io { #[pyattr] #[pyclass(name = "BufferedReader", base = "_BufferedIOBase")] - #[derive(Debug, Default)] + #[derive(Debug, Default, PyValue)] struct BufferedReader { data: PyThreadMutex, } - impl PyValue for BufferedReader { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } impl BufferedMixin for BufferedReader { const READABLE: bool = true; const WRITABLE: bool = false; @@ -1692,15 +1687,10 @@ mod _io { #[pyattr] #[pyclass(name = "BufferedWriter", base = "_BufferedIOBase")] - #[derive(Debug, Default)] + #[derive(Debug, Default, PyValue)] struct BufferedWriter { data: PyThreadMutex, } - impl PyValue for BufferedWriter { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } impl BufferedMixin for BufferedWriter { const READABLE: bool = false; const WRITABLE: bool = true; @@ -1725,15 +1715,10 @@ mod _io { #[pyattr] #[pyclass(name = "BufferedRandom", base = "_BufferedIOBase")] - #[derive(Debug, Default)] + #[derive(Debug, Default, PyValue)] struct BufferedRandom { data: PyThreadMutex, } - impl PyValue for BufferedRandom { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } impl BufferedMixin for BufferedRandom { const READABLE: bool = true; const WRITABLE: bool = true; @@ -1768,16 +1753,11 @@ mod _io { #[pyattr] #[pyclass(name = "BufferedRWPair", base = "_BufferedIOBase")] - #[derive(Debug, Default)] + #[derive(Debug, Default, PyValue)] struct BufferedRWPair { read: BufferedReader, write: BufferedWriter, } - impl PyValue for BufferedRWPair { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } impl BufferedReadable for BufferedRWPair { type Reader = BufferedReader; fn reader(&self) -> &Self::Reader { @@ -2182,15 +2162,10 @@ mod _io { #[pyattr] #[pyclass(name = "TextIOWrapper", base = "_TextIOBase")] - #[derive(Debug, Default)] + #[derive(Debug, Default, PyValue)] struct TextIOWrapper { data: PyThreadMutex>, } - impl PyValue for TextIOWrapper { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } #[pyimpl(flags(BASETYPE))] impl TextIOWrapper { @@ -3043,7 +3018,7 @@ mod _io { #[pyattr] #[pyclass(name = "StringIO", base = "_TextIOBase")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct StringIO { buffer: PyRwLock, closed: AtomicCell, @@ -3051,12 +3026,6 @@ mod _io { type StringIORef = PyRef; - impl PyValue for StringIO { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct StringIONewArgs { #[pyarg(positional, optional)] @@ -3198,7 +3167,7 @@ mod _io { #[pyattr] #[pyclass(name = "BytesIO", base = "_BufferedIOBase")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct BytesIO { buffer: PyRwLock, closed: AtomicCell, @@ -3207,12 +3176,6 @@ mod _io { type BytesIORef = PyRef; - impl PyValue for BytesIO { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for BytesIO { type Args = OptionalArg>; @@ -3823,7 +3786,7 @@ mod fileio { #[pyattr] #[pyclass(module = "io", name, base = "_RawIOBase")] - #[derive(Debug)] + #[derive(Debug, PyValue)] pub(super) struct FileIO { fd: AtomicCell, closefd: AtomicCell, @@ -3831,12 +3794,6 @@ mod fileio { seekable: AtomicCell>, } - impl PyValue for FileIO { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct FileIOArgs { #[pyarg(positional)] diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 6f50d0fac..3a7d01d9c 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -24,19 +24,13 @@ mod decl { #[pyattr] #[pyclass(name = "chain")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsChain { iterables: Vec, cur_idx: AtomicCell, cached_iter: PyRwLock>, } - impl PyValue for PyItertoolsChain { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(PyIter))] impl PyItertoolsChain { #[pyslot] @@ -102,18 +96,12 @@ mod decl { #[pyattr] #[pyclass(name = "compress")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsCompress { data: PyObjectRef, selector: PyObjectRef, } - impl PyValue for PyItertoolsCompress { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct CompressNewArgs { #[pyarg(positional)] @@ -160,18 +148,12 @@ mod decl { #[pyattr] #[pyclass(name = "count")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsCount { cur: PyRwLock, step: BigInt, } - impl PyValue for PyItertoolsCount { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct CountNewArgs { #[pyarg(positional, optional)] @@ -219,19 +201,13 @@ mod decl { #[pyattr] #[pyclass(name = "cycle")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsCycle { iter: PyObjectRef, saved: PyRwLock>, index: AtomicCell, } - impl PyValue for PyItertoolsCycle { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyItertoolsCycle { type Args = PyObjectRef; @@ -275,18 +251,12 @@ mod decl { #[pyattr] #[pyclass(name = "repeat")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsRepeat { object: PyObjectRef, times: Option>, } - impl PyValue for PyItertoolsRepeat { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct PyRepeatNewArgs { #[pyarg(any)] @@ -371,18 +341,12 @@ mod decl { #[pyattr] #[pyclass(name = "starmap")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsStarmap { function: PyObjectRef, iter: PyObjectRef, } - impl PyValue for PyItertoolsStarmap { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct StarmapNewArgs { #[pyarg(positional)] @@ -418,19 +382,13 @@ mod decl { #[pyattr] #[pyclass(name = "takewhile")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsTakewhile { predicate: PyObjectRef, iterable: PyObjectRef, stop_flag: AtomicCell, } - impl PyValue for PyItertoolsTakewhile { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct TakewhileNewArgs { #[pyarg(positional)] @@ -486,19 +444,13 @@ mod decl { #[pyattr] #[pyclass(name = "dropwhile")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsDropwhile { predicate: PyCallable, iterable: PyObjectRef, start_flag: AtomicCell, } - impl PyValue for PyItertoolsDropwhile { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct DropwhileNewArgs { #[pyarg(positional)] @@ -579,18 +531,13 @@ mod decl { #[pyattr] #[pyclass(name = "groupby")] + #[derive(PyValue)] struct PyItertoolsGroupBy { iterable: PyObjectRef, key_func: Option, state: PyMutex, } - impl PyValue for PyItertoolsGroupBy { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl fmt::Debug for PyItertoolsGroupBy { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("PyItertoolsGroupBy") @@ -680,19 +627,13 @@ mod decl { #[pyattr] #[pyclass(name = "_grouper")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsGrouper { groupby: PyRef, } type PyItertoolsGrouperRef = PyRef; - impl PyValue for PyItertoolsGrouper { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(PyIter))] impl PyItertoolsGrouper {} impl PyIter for PyItertoolsGrouper { @@ -727,7 +668,7 @@ mod decl { #[pyattr] #[pyclass(name = "islice")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsIslice { iterable: PyObjectRef, cur: AtomicCell, @@ -736,12 +677,6 @@ mod decl { step: usize, } - impl PyValue for PyItertoolsIslice { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - // Restrict obj to ints with value 0 <= val <= sys.maxsize (isize::MAX). // On failure (out of range, non-int object) a ValueError is raised. fn pyobject_to_opt_usize( @@ -856,18 +791,12 @@ mod decl { #[pyattr] #[pyclass(name = "filterfalse")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsFilterFalse { predicate: PyObjectRef, iterable: PyObjectRef, } - impl PyValue for PyItertoolsFilterFalse { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct FilterFalseNewArgs { #[pyarg(positional)] @@ -921,7 +850,7 @@ mod decl { #[pyattr] #[pyclass(name = "accumulate")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsAccumulate { iterable: PyObjectRef, binop: Option, @@ -938,12 +867,6 @@ mod decl { initial: OptionalOption, } - impl PyValue for PyItertoolsAccumulate { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyItertoolsAccumulate { type Args = AccumulateArgs; @@ -1013,18 +936,12 @@ mod decl { #[pyattr] #[pyclass(name = "tee")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsTee { tee_data: PyRc, index: AtomicCell, } - impl PyValue for PyItertoolsTee { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct TeeNewArgs { #[pyarg(positional)] @@ -1097,7 +1014,7 @@ mod decl { #[pyattr] #[pyclass(name = "product")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsProduct { pools: Vec>, idxs: PyRwLock>, @@ -1105,12 +1022,6 @@ mod decl { stop: AtomicCell, } - impl PyValue for PyItertoolsProduct { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct ProductArgs { #[pyarg(named, optional)] @@ -1200,7 +1111,7 @@ mod decl { #[pyattr] #[pyclass(name = "combinations")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsCombinations { pool: Vec, indices: PyRwLock>, @@ -1208,12 +1119,6 @@ mod decl { exhausted: AtomicCell, } - impl PyValue for PyItertoolsCombinations { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct CombinationsNewArgs { #[pyarg(positional)] @@ -1304,7 +1209,7 @@ mod decl { #[pyattr] #[pyclass(name = "combinations_with_replacement")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsCombinationsWithReplacement { pool: Vec, indices: PyRwLock>, @@ -1312,12 +1217,6 @@ mod decl { exhausted: AtomicCell, } - impl PyValue for PyItertoolsCombinationsWithReplacement { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyItertoolsCombinationsWithReplacement { type Args = CombinationsNewArgs; @@ -1395,7 +1294,7 @@ mod decl { #[pyattr] #[pyclass(name = "permutations")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsPermutations { pool: Vec, // Collected input iterable indices: PyRwLock>, // One index per element in pool @@ -1405,12 +1304,6 @@ mod decl { exhausted: AtomicCell, // Set when the iterator is exhausted } - impl PyValue for PyItertoolsPermutations { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[derive(FromArgs)] struct PermutationsNewArgs { #[pyarg(positional)] @@ -1554,18 +1447,12 @@ mod decl { #[pyattr] #[pyclass(name = "zip_longest")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsZipLongest { iterators: Vec, fillvalue: PyObjectRef, } - impl PyValue for PyItertoolsZipLongest { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(PyIter, SlotConstructor))] impl PyItertoolsZipLongest {} impl PyIter for PyItertoolsZipLongest { @@ -1599,18 +1486,12 @@ mod decl { #[pyattr] #[pyclass(name = "pairwise")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItertoolsPairwise { iterator: PyObjectRef, old: PyRwLock>, } - impl PyValue for PyItertoolsPairwise { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyItertoolsPairwise { type Args = PyObjectRef; diff --git a/vm/src/stdlib/json.rs b/vm/src/stdlib/json.rs index d0f821d04..16ab99c8d 100644 --- a/vm/src/stdlib/json.rs +++ b/vm/src/stdlib/json.rs @@ -20,7 +20,7 @@ mod _json { #[pyattr(name = "make_scanner")] #[pyclass(name = "Scanner")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct JsonScanner { strict: bool, object_hook: Option, @@ -31,12 +31,6 @@ mod _json { ctx: PyObjectRef, } - impl PyValue for JsonScanner { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for JsonScanner { type Args = PyObjectRef; diff --git a/vm/src/stdlib/operator.rs b/vm/src/stdlib/operator.rs index 032737231..8fd62ed54 100644 --- a/vm/src/stdlib/operator.rs +++ b/vm/src/stdlib/operator.rs @@ -436,17 +436,11 @@ mod _operator { /// (r.name.first, r.name.last). #[pyattr] #[pyclass(name = "attrgetter")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyAttrGetter { attrs: Vec, } - impl PyValue for PyAttrGetter { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(Callable))] impl PyAttrGetter { #[pyslot] @@ -539,17 +533,11 @@ mod _operator { /// After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3]) #[pyattr] #[pyclass(name = "itemgetter")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyItemGetter { items: Vec, } - impl PyValue for PyItemGetter { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(Callable))] impl PyItemGetter { #[pyslot] @@ -614,18 +602,12 @@ mod _operator { /// r.name('date', foo=1). #[pyattr] #[pyclass(name = "methodcaller")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyMethodCaller { name: PyStrRef, args: FuncArgs, } - impl PyValue for PyMethodCaller { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyMethodCaller { type Args = (PyObjectRef, FuncArgs); diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 009f3c765..1f001604e 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -787,7 +787,7 @@ mod _os { #[pyattr] #[pyclass(name)] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct DirEntry { entry: fs::DirEntry, mode: OutputMode, @@ -797,12 +797,6 @@ mod _os { ino: AtomicCell>, } - impl PyValue for DirEntry { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl] impl DirEntry { #[pyproperty] @@ -959,19 +953,13 @@ mod _os { #[pyattr] #[pyclass(name = "ScandirIter")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct ScandirIterator { entries: PyRwLock, exhausted: AtomicCell, mode: OutputMode, } - impl PyValue for ScandirIterator { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(PyIter))] impl ScandirIterator { #[pymethod] @@ -2486,17 +2474,11 @@ mod posix { #[pyattr] #[pyclass(name = "sched_param")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct SchedParam { sched_priority: PyObjectRef, } - impl PyValue for SchedParam { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl TryFromObject for SchedParam { fn try_from_object(_vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { Ok(SchedParam { diff --git a/vm/src/stdlib/pyexpat.rs b/vm/src/stdlib/pyexpat.rs index ecc73ddf7..d08d37933 100644 --- a/vm/src/stdlib/pyexpat.rs +++ b/vm/src/stdlib/pyexpat.rs @@ -48,7 +48,7 @@ mod _pyexpat { #[pyattr] #[pyclass(name = "xmlparser", module = false)] - #[derive(Debug)] + #[derive(Debug, PyValue)] pub struct PyExpatLikeXmlParser { start_element: MutableObject, end_element: MutableObject, @@ -58,12 +58,6 @@ mod _pyexpat { } type PyExpatLikeXmlParserRef = PyRef; - impl PyValue for PyExpatLikeXmlParser { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[inline] fn invoke_handler(vm: &VirtualMachine, handler: &MutableObject, args: T) where diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs index 3f1964880..b71f72162 100644 --- a/vm/src/stdlib/pystruct.rs +++ b/vm/src/stdlib/pystruct.rs @@ -789,7 +789,7 @@ pub(crate) mod _struct { #[pyattr] #[pyclass(name = "unpack_iterator")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct UnpackIterator { format_spec: FormatSpec, buffer: ArgBytesLike, @@ -825,12 +825,6 @@ pub(crate) mod _struct { } } - impl PyValue for UnpackIterator { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(PyIter))] impl UnpackIterator { #[pymethod(magic)] @@ -870,18 +864,12 @@ pub(crate) mod _struct { #[pyattr] #[pyclass(name = "Struct")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyStruct { spec: FormatSpec, fmt_str: PyStrRef, } - impl PyValue for PyStruct { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyStruct { type Args = Either; diff --git a/vm/src/stdlib/random.rs b/vm/src/stdlib/random.rs index 4f86456db..e9b18f837 100644 --- a/vm/src/stdlib/random.rs +++ b/vm/src/stdlib/random.rs @@ -56,17 +56,11 @@ mod _random { #[pyattr] #[pyclass(name = "Random")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyRandom { rng: PyMutex, } - impl PyValue for PyRandom { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - impl SlotConstructor for PyRandom { type Args = OptionalOption; diff --git a/vm/src/stdlib/re.rs b/vm/src/stdlib/re.rs index bbda297dc..94a7522fa 100644 --- a/vm/src/stdlib/re.rs +++ b/vm/src/stdlib/re.rs @@ -17,7 +17,7 @@ use crate::vm::VirtualMachine; use crate::{IntoPyObject, PyClassImpl, PyObjectRef, PyResult, PyValue, StaticType, TryFromObject}; #[pyclass(module = "re", name = "Pattern")] -#[derive(Debug)] +#[derive(Debug, PyValue)] struct PyPattern { regex: Regex, pattern: String, @@ -62,14 +62,9 @@ impl PyRegexFlags { } } -impl PyValue for PyPattern { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - /// Inner data for a match object. #[pyclass(module = "re", name = "Match")] +#[derive(PyValue)] struct PyMatch { haystack: PyStrRef, captures: Vec>>, @@ -81,12 +76,6 @@ impl fmt::Debug for PyMatch { } } -impl PyValue for PyMatch { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - // type PyPatternRef = PyRef; // type PyMatchRef = PyRef; @@ -246,7 +235,9 @@ fn do_split( let split = output .into_iter() .map(|v| { - vm.unwrap_or_none(v.map(|v| vm.ctx.new_utf8_str(String::from_utf8_lossy(v).into_owned()))) + vm.unwrap_or_none( + v.map(|v| vm.ctx.new_utf8_str(String::from_utf8_lossy(v).into_owned())), + ) }) .collect(); Ok(vm.ctx.new_list(split)) diff --git a/vm/src/stdlib/select.rs b/vm/src/stdlib/select.rs index 59ba10beb..8fcdb491e 100644 --- a/vm/src/stdlib/select.rs +++ b/vm/src/stdlib/select.rs @@ -264,18 +264,12 @@ mod decl { use std::time; #[pyclass(module = "select", name = "poll")] - #[derive(Default, Debug)] + #[derive(Default, Debug, PyValue)] pub struct PyPoll { // keep sorted fds: PyMutex>, } - impl PyValue for PyPoll { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[inline] fn search(fds: &[pollfd], fd: i32) -> Result { fds.binary_search_by_key(&fd, |pfd| pfd.fd) diff --git a/vm/src/stdlib/socket.rs b/vm/src/stdlib/socket.rs index 4845658cf..57f4994cc 100644 --- a/vm/src/stdlib/socket.rs +++ b/vm/src/stdlib/socket.rs @@ -142,7 +142,7 @@ impl Default for NullableSocket { } #[pyclass(module = "socket", name = "socket")] -#[derive(Debug)] +#[derive(Debug, PyValue)] pub struct PySocket { kind: AtomicCell, family: AtomicCell, @@ -163,12 +163,6 @@ impl Default for PySocket { } } -impl PyValue for PySocket { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - pub type PySocketRef = PyRef; #[cfg(windows)] diff --git a/vm/src/stdlib/sre.rs b/vm/src/stdlib/sre.rs index af6c5f7e4..868317451 100644 --- a/vm/src/stdlib/sre.rs +++ b/vm/src/stdlib/sre.rs @@ -122,7 +122,7 @@ mod _sre { #[pyattr] #[pyclass(name = "Pattern")] - #[derive(Debug)] + #[derive(Debug, PyValue)] pub(crate) struct Pattern { pub pattern: PyObjectRef, pub flags: SreFlag, @@ -133,12 +133,6 @@ mod _sre { pub isbytes: bool, } - impl PyValue for Pattern { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl(with(Hashable, Comparable))] impl Pattern { fn with_str_drive PyResult>( @@ -561,7 +555,7 @@ mod _sre { #[pyattr] #[pyclass(name = "Match")] - #[derive(Debug)] + #[derive(Debug, PyValue)] pub(crate) struct Match { string: PyObjectRef, pattern: PyRef, @@ -570,11 +564,6 @@ mod _sre { lastindex: isize, regs: Vec<(isize, isize)>, } - impl PyValue for Match { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } #[pyimpl] impl Match { @@ -804,7 +793,7 @@ mod _sre { #[pyattr] #[pyclass(name = "SRE_Scanner")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct SreScanner { pattern: PyRef, string: PyObjectRef, @@ -812,11 +801,6 @@ mod _sre { end: usize, must_advance: AtomicCell, } - impl PyValue for SreScanner { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } #[pyimpl] impl SreScanner { diff --git a/vm/src/stdlib/ssl.rs b/vm/src/stdlib/ssl.rs index 195071c90..fd48002ee 100644 --- a/vm/src/stdlib/ssl.rs +++ b/vm/src/stdlib/ssl.rs @@ -248,6 +248,7 @@ fn _ssl_rand_pseudo_bytes(n: i32, vm: &VirtualMachine) -> PyResult<(Vec, boo } #[pyclass(module = "ssl", name = "_SSLContext")] +#[derive(PyValue)] struct PySslContext { ctx: PyRwLock, check_hostname: AtomicCell, @@ -260,12 +261,6 @@ impl fmt::Debug for PySslContext { } } -impl PyValue for PySslContext { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - fn builder_as_ctx(x: &SslContextBuilder) -> &ssl::SslContextRef { unsafe { ssl::SslContextRef::from_ptr(x.as_ptr()) } } @@ -716,6 +711,7 @@ fn socket_closed_error(vm: &VirtualMachine) -> PyBaseExceptionRef { } #[pyclass(module = "ssl", name = "_SSLSocket")] +#[derive(PyValue)] struct PySslSocket { ctx: PyRef, stream: PyRwLock>, @@ -730,12 +726,6 @@ impl fmt::Debug for PySslSocket { } } -impl PyValue for PySslSocket { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl PySslSocket { #[pyproperty] diff --git a/vm/src/stdlib/symtable.rs b/vm/src/stdlib/symtable.rs index 2a77058f1..2a9cf0d21 100644 --- a/vm/src/stdlib/symtable.rs +++ b/vm/src/stdlib/symtable.rs @@ -40,6 +40,7 @@ mod decl { #[pyattr] #[pyclass(name = "SymbolTable")] + #[derive(PyValue)] struct PySymbolTable { symtable: SymbolTable, } @@ -50,12 +51,6 @@ mod decl { } } - impl PyValue for PySymbolTable { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl] impl PySymbolTable { #[pymethod] @@ -157,6 +152,7 @@ mod decl { #[pyattr] #[pyclass(name = "Symbol")] + #[derive(PyValue)] struct PySymbol { symbol: Symbol, namespaces: Vec, @@ -168,12 +164,6 @@ mod decl { } } - impl PyValue for PySymbol { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl] impl PySymbol { #[pymethod] diff --git a/vm/src/stdlib/thread.rs b/vm/src/stdlib/thread.rs index f96162d92..24dc47514 100644 --- a/vm/src/stdlib/thread.rs +++ b/vm/src/stdlib/thread.rs @@ -96,17 +96,12 @@ macro_rules! repr_lock_impl { } #[pyclass(module = "thread", name = "lock")] +#[derive(PyValue)] struct PyLock { mu: RawMutex, } type PyLockRef = PyRef; -impl PyValue for PyLock { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - impl fmt::Debug for PyLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad("PyLock") @@ -150,16 +145,11 @@ impl PyLock { pub type RawRMutex = RawReentrantMutex; #[pyclass(module = "thread", name = "RLock")] +#[derive(PyValue)] struct PyRLock { mu: RawRMutex, } -impl PyValue for PyRLock { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - impl fmt::Debug for PyRLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.pad("PyRLock") @@ -303,17 +293,11 @@ fn _thread_count(vm: &VirtualMachine) -> usize { } #[pyclass(module = "thread", name = "_local")] -#[derive(Debug)] +#[derive(Debug, PyValue)] struct PyLocal { data: ThreadLocal, } -impl PyValue for PyLocal { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl(with(SlotGetattro, SlotSetattro), flags(BASETYPE))] impl PyLocal { fn ldict(&self, vm: &VirtualMachine) -> PyDictRef { diff --git a/vm/src/stdlib/unicodedata.rs b/vm/src/stdlib/unicodedata.rs index bc675d99b..964e276f0 100644 --- a/vm/src/stdlib/unicodedata.rs +++ b/vm/src/stdlib/unicodedata.rs @@ -55,17 +55,11 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { } #[pyclass(module = "unicodedata", name = "UCD")] -#[derive(Debug)] +#[derive(Debug, PyValue)] struct PyUCD { unic_version: UnicodeVersion, } -impl PyValue for PyUCD { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - impl Default for PyUCD { #[inline(always)] fn default() -> Self { diff --git a/vm/src/stdlib/winreg.rs b/vm/src/stdlib/winreg.rs index 7954c8990..4e6addf90 100644 --- a/vm/src/stdlib/winreg.rs +++ b/vm/src/stdlib/winreg.rs @@ -13,7 +13,7 @@ use winapi::shared::winerror; use winreg::{enums::RegType, RegKey, RegValue}; #[pyclass(module = "winreg", name = "HKEYType")] -#[derive(Debug)] +#[derive(Debug, PyValue)] struct PyHKEY { key: PyRwLock, } @@ -22,12 +22,6 @@ type PyHKEYRef = PyRef; // TODO: fix this unsafe impl Sync for PyHKEY {} -impl PyValue for PyHKEY { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl PyHKEY { fn new(key: RegKey) -> Self { diff --git a/vm/src/stdlib/zlib.rs b/vm/src/stdlib/zlib.rs index 07834b311..8a6c2d75b 100644 --- a/vm/src/stdlib/zlib.rs +++ b/vm/src/stdlib/zlib.rs @@ -270,18 +270,13 @@ mod decl { } #[pyattr] #[pyclass(name = "Decompress")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyDecompress { decompress: PyMutex, eof: AtomicCell, unused_data: PyMutex, unconsumed_tail: PyMutex, } - impl PyValue for PyDecompress { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } #[pyimpl] impl PyDecompress { #[pyproperty] @@ -441,17 +436,11 @@ mod decl { #[pyattr] #[pyclass(name = "Compress")] - #[derive(Debug)] + #[derive(Debug, PyValue)] struct PyCompress { inner: PyMutex, } - impl PyValue for PyCompress { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } - } - #[pyimpl] impl PyCompress { #[pymethod] diff --git a/wasm/lib/src/browser_module.rs b/wasm/lib/src/browser_module.rs index 84ccdec82..47ff84537 100644 --- a/wasm/lib/src/browser_module.rs +++ b/wasm/lib/src/browser_module.rs @@ -153,17 +153,11 @@ fn browser_cancel_animation_frame(id: i32, vm: &VirtualMachine) -> PyResult<()> } #[pyclass(module = "browser", name)] -#[derive(Debug)] +#[derive(Debug, PyValue)] struct Document { doc: web_sys::Document, } -impl PyValue for Document { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl Document { #[pymethod] @@ -179,17 +173,11 @@ impl Document { } #[pyclass(module = "browser", name)] -#[derive(Debug)] +#[derive(Debug, PyValue)] struct Element { elem: web_sys::Element, } -impl PyValue for Element { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl Element { #[pymethod] diff --git a/wasm/lib/src/js_module.rs b/wasm/lib/src/js_module.rs index 34f7724db..55f9b99ba 100644 --- a/wasm/lib/src/js_module.rs +++ b/wasm/lib/src/js_module.rs @@ -51,18 +51,12 @@ extern "C" { } #[pyclass(module = "_js", name = "JSValue")] -#[derive(Debug)] +#[derive(Debug, PyValue)] pub struct PyJsValue { pub(crate) value: JsValue, } type PyJsValueRef = PyRef; -impl PyValue for PyJsValue { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - impl AsRef for PyJsValue { fn as_ref(&self) -> &JsValue { &self.value @@ -281,6 +275,7 @@ struct NewObjectOptions { type ClosureType = Closure) -> Result>; #[pyclass(module = "_js", name = "JSClosure")] +#[derive(PyValue)] struct JsClosure { closure: cell::RefCell>, destroyed: cell::Cell, @@ -293,12 +288,6 @@ impl fmt::Debug for JsClosure { } } -impl PyValue for JsClosure { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl JsClosure { fn new(obj: PyObjectRef, once: bool, vm: &VirtualMachine) -> Self { @@ -380,7 +369,7 @@ impl JsClosure { } #[pyclass(module = "_js", name = "Promise")] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PyValue)] pub struct PyPromise { value: PromiseKind, } @@ -394,12 +383,6 @@ enum PromiseKind { PyRejected(PyBaseExceptionRef), } -impl PyValue for PyPromise { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl] impl PyPromise { pub fn new(value: Promise) -> PyPromise { @@ -548,6 +531,7 @@ impl PyPromise { } #[pyclass(module = "_js", name = "AwaitPromise")] +#[derive(PyValue)] struct AwaitPromise { obj: cell::Cell>, } @@ -558,12 +542,6 @@ impl fmt::Debug for AwaitPromise { } } -impl PyValue for AwaitPromise { - fn class(_vm: &VirtualMachine) -> &PyTypeRef { - Self::static_type() - } -} - #[pyimpl(with(PyIter))] impl AwaitPromise { #[pymethod]