Skip to content

Commit 32aab6b

Browse files
committed
DefaultConstructor
1 parent 0325cca commit 32aab6b

File tree

3 files changed

+45
-59
lines changed

3 files changed

+45
-59
lines changed

stdlib/src/socket.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ mod _socket {
1414
use crate::vm::{
1515
builtins::{PyBaseExceptionRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
1616
convert::{ToPyException, ToPyObject, TryFromBorrowedObject, TryFromObject},
17-
function::{ArgBytesLike, ArgMemoryBuffer, Either, FuncArgs, OptionalArg, OptionalOption},
18-
types::{Constructor, Initializer},
17+
function::{ArgBytesLike, ArgMemoryBuffer, Either, OptionalArg, OptionalOption},
18+
types::{DefaultConstructor, Initializer},
1919
utils::ToCString,
2020
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
2121
};
@@ -542,13 +542,7 @@ mod _socket {
542542
}
543543
}
544544

545-
impl Constructor for PySocket {
546-
type Args = FuncArgs;
547-
548-
fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
549-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
550-
}
551-
}
545+
impl DefaultConstructor for PySocket {}
552546

553547
impl Initializer for PySocket {
554548
type Args = (
@@ -638,7 +632,7 @@ mod _socket {
638632
}
639633
}
640634

641-
#[pyimpl(with(Constructor, Initializer), flags(BASETYPE))]
635+
#[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE))]
642636
impl PySocket {
643637
#[pymethod]
644638
fn connect(&self, address: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {

vm/src/stdlib/io.rs

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ mod _io {
113113
BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn, VecBuffer,
114114
},
115115
recursion::ReprGuard,
116-
types::{Constructor, Destructor, Initializer, IterNext, Iterable},
116+
types::{Constructor, DefaultConstructor, Destructor, Initializer, IterNext, Iterable},
117117
vm::VirtualMachine,
118118
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
119119
TryFromBorrowedObject, TryFromObject,
@@ -1670,13 +1670,13 @@ mod _io {
16701670
}
16711671
}
16721672

1673-
#[pyimpl(with(BufferedMixin, BufferedReadable), flags(BASETYPE, HAS_DICT))]
1674-
impl BufferedReader {
1675-
#[pyslot]
1676-
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
1677-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
1678-
}
1679-
}
1673+
#[pyimpl(
1674+
with(DefaultConstructor, BufferedMixin, BufferedReadable),
1675+
flags(BASETYPE, HAS_DICT)
1676+
)]
1677+
impl BufferedReader {}
1678+
1679+
impl DefaultConstructor for BufferedReader {}
16801680

16811681
#[pyimpl]
16821682
trait BufferedWritable: PyPayload {
@@ -1719,13 +1719,13 @@ mod _io {
17191719
}
17201720
}
17211721

1722-
#[pyimpl(with(BufferedMixin, BufferedWritable), flags(BASETYPE, HAS_DICT))]
1723-
impl BufferedWriter {
1724-
#[pyslot]
1725-
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
1726-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
1727-
}
1728-
}
1722+
#[pyimpl(
1723+
with(DefaultConstructor, BufferedMixin, BufferedWritable),
1724+
flags(BASETYPE, HAS_DICT)
1725+
)]
1726+
impl BufferedWriter {}
1727+
1728+
impl DefaultConstructor for BufferedWriter {}
17291729

17301730
#[pyattr]
17311731
#[pyclass(name = "BufferedRandom", base = "_BufferedIOBase")]
@@ -1755,15 +1755,12 @@ mod _io {
17551755
}
17561756

17571757
#[pyimpl(
1758-
with(BufferedMixin, BufferedReadable, BufferedWritable),
1758+
with(DefaultConstructor, BufferedMixin, BufferedReadable, BufferedWritable),
17591759
flags(BASETYPE, HAS_DICT)
17601760
)]
1761-
impl BufferedRandom {
1762-
#[pyslot]
1763-
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
1764-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
1765-
}
1766-
}
1761+
impl BufferedRandom {}
1762+
1763+
impl DefaultConstructor for BufferedRandom {}
17671764

17681765
#[pyattr]
17691766
#[pyclass(name = "BufferedRWPair", base = "_BufferedIOBase")]
@@ -1785,13 +1782,7 @@ mod _io {
17851782
}
17861783
}
17871784

1788-
impl Constructor for BufferedRWPair {
1789-
type Args = FuncArgs;
1790-
1791-
fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
1792-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
1793-
}
1794-
}
1785+
impl DefaultConstructor for BufferedRWPair {}
17951786

17961787
impl Initializer for BufferedRWPair {
17971788
type Args = (PyObjectRef, PyObjectRef, BufferSize);
@@ -1808,7 +1799,7 @@ mod _io {
18081799
}
18091800

18101801
#[pyimpl(
1811-
with(Constructor, Initializer, BufferedReadable, BufferedWritable),
1802+
with(DefaultConstructor, Initializer, BufferedReadable, BufferedWritable),
18121803
flags(BASETYPE, HAS_DICT)
18131804
)]
18141805
impl BufferedRWPair {
@@ -2188,13 +2179,7 @@ mod _io {
21882179
data: PyThreadMutex<Option<TextIOData>>,
21892180
}
21902181

2191-
impl Constructor for TextIOWrapper {
2192-
type Args = FuncArgs;
2193-
2194-
fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
2195-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
2196-
}
2197-
}
2182+
impl DefaultConstructor for TextIOWrapper {}
21982183

21992184
impl Initializer for TextIOWrapper {
22002185
type Args = TextIOWrapperArgs;
@@ -2289,7 +2274,7 @@ mod _io {
22892274
}
22902275
}
22912276

2292-
#[pyimpl(with(Constructor, Initializer), flags(BASETYPE))]
2277+
#[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE))]
22932278
impl TextIOWrapper {
22942279
#[pymethod]
22952280
fn seekable(&self, vm: &VirtualMachine) -> PyResult {
@@ -3709,12 +3694,12 @@ mod _io {
37093694
mod fileio {
37103695
use super::{Offset, _io::*};
37113696
use crate::{
3712-
builtins::{PyStr, PyStrRef, PyTypeRef},
3697+
builtins::{PyStr, PyStrRef},
37133698
convert::ToPyException,
37143699
crt_fd::Fd,
3715-
function::{ArgBytesLike, ArgMemoryBuffer, FuncArgs, OptionalArg, OptionalOption},
3700+
function::{ArgBytesLike, ArgMemoryBuffer, OptionalArg, OptionalOption},
37163701
stdlib::os,
3717-
types::{Constructor, Initializer},
3702+
types::{DefaultConstructor, Initializer},
37183703
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
37193704
};
37203705
use crossbeam_utils::atomic::AtomicCell;
@@ -3841,21 +3826,19 @@ mod fileio {
38413826
opener: Option<PyObjectRef>,
38423827
}
38433828

3844-
impl Constructor for FileIO {
3845-
type Args = FuncArgs;
3846-
3847-
fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
3848-
FileIO {
3829+
impl Default for FileIO {
3830+
fn default() -> Self {
3831+
Self {
38493832
fd: AtomicCell::new(-1),
38503833
closefd: AtomicCell::new(false),
38513834
mode: AtomicCell::new(Mode::empty()),
38523835
seekable: AtomicCell::new(None),
38533836
}
3854-
.into_ref_with_type(vm, cls)
3855-
.map(Into::into)
38563837
}
38573838
}
38583839

3840+
impl DefaultConstructor for FileIO {}
3841+
38593842
impl Initializer for FileIO {
38603843
type Args = FileIOArgs;
38613844

@@ -3901,7 +3884,7 @@ mod fileio {
39013884
}
39023885
}
39033886

3904-
#[pyimpl(with(Constructor, Initializer), flags(BASETYPE, HAS_DICT))]
3887+
#[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE, HAS_DICT))]
39053888
impl FileIO {
39063889
#[pyproperty]
39073890
fn closed(&self) -> bool {

vm/src/types/slot.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,15 @@ pub trait Constructor: PyPayload {
416416
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult;
417417
}
418418

419+
#[pyimpl]
420+
pub trait DefaultConstructor: PyPayload + Default {
421+
#[inline]
422+
#[pyslot]
423+
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
424+
Self::default().into_ref_with_type(vm, cls).map(Into::into)
425+
}
426+
}
427+
419428
/// For types that cannot be instantiated through Python code.
420429
pub trait Unconstructible: PyPayload {}
421430

0 commit comments

Comments
 (0)