diff --git a/stdlib/src/socket.rs b/stdlib/src/socket.rs index 20dff4355..fa3b84d8e 100644 --- a/stdlib/src/socket.rs +++ b/stdlib/src/socket.rs @@ -14,8 +14,8 @@ mod _socket { use crate::vm::{ builtins::{PyBaseExceptionRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef}, convert::{ToPyException, ToPyObject, TryFromBorrowedObject, TryFromObject}, - function::{ArgBytesLike, ArgMemoryBuffer, Either, FuncArgs, OptionalArg, OptionalOption}, - types::{Constructor, Initializer}, + function::{ArgBytesLike, ArgMemoryBuffer, Either, OptionalArg, OptionalOption}, + types::{DefaultConstructor, Initializer}, utils::ToCString, AsObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, }; @@ -542,13 +542,7 @@ mod _socket { } } - impl Constructor for PySocket { - type Args = FuncArgs; - - fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - Self::default().into_ref_with_type(vm, cls).map(Into::into) - } - } + impl DefaultConstructor for PySocket {} impl Initializer for PySocket { type Args = ( @@ -638,7 +632,7 @@ mod _socket { } } - #[pyimpl(with(Constructor, Initializer), flags(BASETYPE))] + #[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE))] impl PySocket { #[pymethod] fn connect(&self, address: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> { diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs index 7da8f9dbd..d32a5bb5c 100644 --- a/vm/src/stdlib/io.rs +++ b/vm/src/stdlib/io.rs @@ -113,7 +113,7 @@ mod _io { BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, VecBuffer, }, recursion::ReprGuard, - types::{Constructor, Destructor, Initializer, IterNext, Iterable}, + types::{Constructor, DefaultConstructor, Destructor, Initializer, IterNext, Iterable}, vm::VirtualMachine, AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromBorrowedObject, TryFromObject, @@ -1670,13 +1670,13 @@ mod _io { } } - #[pyimpl(with(BufferedMixin, BufferedReadable), flags(BASETYPE, HAS_DICT))] - impl BufferedReader { - #[pyslot] - fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { - Self::default().into_ref_with_type(vm, cls).map(Into::into) - } - } + #[pyimpl( + with(DefaultConstructor, BufferedMixin, BufferedReadable), + flags(BASETYPE, HAS_DICT) + )] + impl BufferedReader {} + + impl DefaultConstructor for BufferedReader {} #[pyimpl] trait BufferedWritable: PyPayload { @@ -1719,13 +1719,13 @@ mod _io { } } - #[pyimpl(with(BufferedMixin, BufferedWritable), flags(BASETYPE, HAS_DICT))] - impl BufferedWriter { - #[pyslot] - fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { - Self::default().into_ref_with_type(vm, cls).map(Into::into) - } - } + #[pyimpl( + with(DefaultConstructor, BufferedMixin, BufferedWritable), + flags(BASETYPE, HAS_DICT) + )] + impl BufferedWriter {} + + impl DefaultConstructor for BufferedWriter {} #[pyattr] #[pyclass(name = "BufferedRandom", base = "_BufferedIOBase")] @@ -1755,15 +1755,12 @@ mod _io { } #[pyimpl( - with(BufferedMixin, BufferedReadable, BufferedWritable), + with(DefaultConstructor, BufferedMixin, BufferedReadable, BufferedWritable), flags(BASETYPE, HAS_DICT) )] - impl BufferedRandom { - #[pyslot] - fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { - Self::default().into_ref_with_type(vm, cls).map(Into::into) - } - } + impl BufferedRandom {} + + impl DefaultConstructor for BufferedRandom {} #[pyattr] #[pyclass(name = "BufferedRWPair", base = "_BufferedIOBase")] @@ -1785,13 +1782,7 @@ mod _io { } } - impl Constructor for BufferedRWPair { - type Args = FuncArgs; - - fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - Self::default().into_ref_with_type(vm, cls).map(Into::into) - } - } + impl DefaultConstructor for BufferedRWPair {} impl Initializer for BufferedRWPair { type Args = (PyObjectRef, PyObjectRef, BufferSize); @@ -1808,7 +1799,7 @@ mod _io { } #[pyimpl( - with(Constructor, Initializer, BufferedReadable, BufferedWritable), + with(DefaultConstructor, Initializer, BufferedReadable, BufferedWritable), flags(BASETYPE, HAS_DICT) )] impl BufferedRWPair { @@ -2188,13 +2179,7 @@ mod _io { data: PyThreadMutex>, } - impl Constructor for TextIOWrapper { - type Args = FuncArgs; - - fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - Self::default().into_ref_with_type(vm, cls).map(Into::into) - } - } + impl DefaultConstructor for TextIOWrapper {} impl Initializer for TextIOWrapper { type Args = TextIOWrapperArgs; @@ -2289,7 +2274,7 @@ mod _io { } } - #[pyimpl(with(Constructor, Initializer), flags(BASETYPE))] + #[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE))] impl TextIOWrapper { #[pymethod] fn seekable(&self, vm: &VirtualMachine) -> PyResult { @@ -3709,12 +3694,12 @@ mod _io { mod fileio { use super::{Offset, _io::*}; use crate::{ - builtins::{PyStr, PyStrRef, PyTypeRef}, + builtins::{PyStr, PyStrRef}, convert::ToPyException, crt_fd::Fd, - function::{ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption}, + function::{ArgBytesLike, ArgMemoryBuffer, OptionalArg, OptionalOption}, stdlib::os, - types::{Constructor, Initializer}, + types::{DefaultConstructor, Initializer}, AsObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine, }; use crossbeam_utils::atomic::AtomicCell; @@ -3841,21 +3826,19 @@ mod fileio { opener: Option, } - impl Constructor for FileIO { - type Args = FuncArgs; - - fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult { - FileIO { + impl Default for FileIO { + fn default() -> Self { + Self { fd: AtomicCell::new(-1), closefd: AtomicCell::new(false), mode: AtomicCell::new(Mode::empty()), seekable: AtomicCell::new(None), } - .into_ref_with_type(vm, cls) - .map(Into::into) } } + impl DefaultConstructor for FileIO {} + impl Initializer for FileIO { type Args = FileIOArgs; @@ -3901,7 +3884,7 @@ mod fileio { } } - #[pyimpl(with(Constructor, Initializer), flags(BASETYPE, HAS_DICT))] + #[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE, HAS_DICT))] impl FileIO { #[pyproperty] fn closed(&self) -> bool { diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 5c7b0e1a6..1a0dc9ad5 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -416,6 +416,15 @@ pub trait Constructor: PyPayload { fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult; } +#[pyimpl] +pub trait DefaultConstructor: PyPayload + Default { + #[inline] + #[pyslot] + fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult { + Self::default().into_ref_with_type(vm, cls).map(Into::into) + } +} + /// For types that cannot be instantiated through Python code. pub trait Unconstructible: PyPayload {}