Skip to content

Commit 76a3dc6

Browse files
authored
Merge pull request RustPython#3463 from youknowone/tp-init
Add PyTypSlot::init
2 parents 07ab973 + 32aab6b commit 76a3dc6

File tree

17 files changed

+579
-438
lines changed

17 files changed

+579
-438
lines changed

derive/src/pyclass.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,10 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
381381
#slot_new_impl
382382
}
383383

384+
#[pyslot]
384385
#[pymethod(magic)]
385386
pub(crate) fn init(
386-
zelf: ::rustpython_vm::PyRef<::rustpython_vm::builtins::PyBaseException>,
387+
zelf: PyObjectRef,
387388
args: ::rustpython_vm::function::FuncArgs,
388389
vm: &::rustpython_vm::VirtualMachine,
389390
) -> ::rustpython_vm::PyResult<()> {

stdlib/src/contextvars.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod _contextvars {
55
use crate::vm::{
66
builtins::{PyFunction, PyStrRef, PyTypeRef},
77
function::{ArgCallable, FuncArgs, OptionalArg},
8+
types::Initializer,
89
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
910
};
1011

@@ -13,13 +14,8 @@ mod _contextvars {
1314
#[derive(Debug, PyPayload)]
1415
struct PyContext {} // not to confuse with vm::Context
1516

16-
#[pyimpl]
17+
#[pyimpl(with(Initializer))]
1718
impl PyContext {
18-
#[pymethod(magic)]
19-
fn init(&self, _vm: &VirtualMachine) -> PyResult<()> {
20-
unimplemented!("Context.__init__ is currently under construction")
21-
}
22-
2319
#[pymethod]
2420
fn run(
2521
&self,
@@ -75,6 +71,14 @@ mod _contextvars {
7571
}
7672
}
7773

74+
impl Initializer for PyContext {
75+
type Args = FuncArgs;
76+
77+
fn init(_obj: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
78+
unimplemented!("Context.__init__ is currently under construction")
79+
}
80+
}
81+
7882
#[pyattr]
7983
#[pyclass(name)]
8084
#[derive(Debug, PyPayload)]
@@ -95,13 +99,8 @@ mod _contextvars {
9599
default: OptionalArg<PyObjectRef>,
96100
}
97101

98-
#[pyimpl]
102+
#[pyimpl(with(Initializer))]
99103
impl ContextVar {
100-
#[pymethod(magic)]
101-
fn init(&self, _args: ContextVarOptions, _vm: &VirtualMachine) -> PyResult<()> {
102-
unimplemented!("ContextVar.__init__() is currently under construction")
103-
}
104-
105104
#[pyproperty]
106105
fn name(&self) -> String {
107106
self.name.clone()
@@ -147,6 +146,14 @@ mod _contextvars {
147146
}
148147
}
149148

149+
impl Initializer for ContextVar {
150+
type Args = ContextVarOptions;
151+
152+
fn init(_obj: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
153+
unimplemented!("ContextVar.__init__() is currently under construction")
154+
}
155+
}
156+
150157
#[pyattr]
151158
#[pyclass(name = "Token")]
152159
#[derive(Debug, PyPayload)]
@@ -165,13 +172,8 @@ mod _contextvars {
165172
old_value: PyObjectRef,
166173
}
167174

168-
#[pyimpl]
175+
#[pyimpl(with(Initializer))]
169176
impl ContextToken {
170-
#[pymethod(magic)]
171-
fn init(&self, _args: ContextTokenOptions, _vm: &VirtualMachine) -> PyResult<()> {
172-
unimplemented!("Token.__init__() is currently under construction")
173-
}
174-
175177
#[pyproperty]
176178
fn var(&self, _vm: &VirtualMachine) -> PyObjectRef {
177179
unimplemented!("Token.var() is currently under construction")
@@ -188,6 +190,14 @@ mod _contextvars {
188190
}
189191
}
190192

193+
impl Initializer for ContextToken {
194+
type Args = ContextTokenOptions;
195+
196+
fn init(_obj: PyRef<Self>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<()> {
197+
unimplemented!("Token.__init__() is currently under construction")
198+
}
199+
}
200+
191201
#[pyfunction]
192202
fn copy_context() {}
193203
}

stdlib/src/socket.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ 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},
17+
function::{ArgBytesLike, ArgMemoryBuffer, Either, OptionalArg, OptionalOption},
18+
types::{DefaultConstructor, Initializer},
1819
utils::ToCString,
19-
AsObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
20+
AsObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
2021
};
2122
use crossbeam_utils::atomic::AtomicCell;
2223
use num_traits::ToPrimitive;
@@ -541,20 +542,19 @@ mod _socket {
541542
}
542543
}
543544

544-
#[pyimpl(flags(BASETYPE))]
545-
impl PySocket {
546-
#[pyslot]
547-
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
548-
Self::default().into_ref_with_type(vm, cls).map(Into::into)
549-
}
545+
impl DefaultConstructor for PySocket {}
546+
547+
impl Initializer for PySocket {
548+
type Args = (
549+
OptionalArg<i32>,
550+
OptionalArg<i32>,
551+
OptionalArg<i32>,
552+
OptionalOption<PyObjectRef>,
553+
);
550554

551-
#[pymethod(magic)]
552555
fn init(
553-
&self,
554-
family: OptionalArg<i32>,
555-
socket_kind: OptionalArg<i32>,
556-
proto: OptionalArg<i32>,
557-
fileno: OptionalOption<PyObjectRef>,
556+
zelf: PyRef<Self>,
557+
(family, socket_kind, proto, fileno): Self::Args,
558558
vm: &VirtualMachine,
559559
) -> PyResult<()> {
560560
let mut family = family.unwrap_or(-1);
@@ -628,9 +628,12 @@ mod _socket {
628628
)
629629
.map_err(|err| err.to_pyexception(vm))?;
630630
};
631-
self.init_inner(family, socket_kind, proto, sock, vm)
631+
zelf.init_inner(family, socket_kind, proto, sock, vm)
632632
}
633+
}
633634

635+
#[pyimpl(with(DefaultConstructor, Initializer), flags(BASETYPE))]
636+
impl PySocket {
634637
#[pymethod]
635638
fn connect(&self, address: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
636639
self.connect_inner(address, "connect", vm)

vm/src/builtins/bytearray.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::{
3030
},
3131
sliceable::{SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp},
3232
types::{
33-
AsBuffer, AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext,
34-
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
33+
AsBuffer, AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, Initializer,
34+
IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
3535
},
3636
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
3737
TryFromBorrowedObject, TryFromObject, VirtualMachine,
@@ -95,24 +95,18 @@ pub(crate) fn init(context: &Context) {
9595

9696
#[pyimpl(
9797
flags(BASETYPE),
98-
with(Hashable, Comparable, AsBuffer, AsMapping, AsSequence, Iterable)
98+
with(
99+
Constructor,
100+
Initializer,
101+
Hashable,
102+
Comparable,
103+
AsBuffer,
104+
AsMapping,
105+
AsSequence,
106+
Iterable
107+
)
99108
)]
100109
impl PyByteArray {
101-
#[pyslot]
102-
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
103-
PyByteArray::default()
104-
.into_ref_with_type(vm, cls)
105-
.map(Into::into)
106-
}
107-
108-
#[pymethod(magic)]
109-
fn init(&self, options: ByteInnerNewOptions, vm: &VirtualMachine) -> PyResult<()> {
110-
// First unpack bytearray and *then* get a lock to set it.
111-
let mut inner = options.get_bytearray_inner(vm)?;
112-
std::mem::swap(&mut *self.inner_mut(), &mut inner);
113-
Ok(())
114-
}
115-
116110
#[cfg(debug_assertions)]
117111
#[pyproperty]
118112
fn exports(&self) -> usize {
@@ -714,6 +708,27 @@ impl PyByteArray {
714708
};
715709
}
716710

711+
impl Constructor for PyByteArray {
712+
type Args = FuncArgs;
713+
714+
fn py_new(cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
715+
PyByteArray::default()
716+
.into_ref_with_type(vm, cls)
717+
.map(Into::into)
718+
}
719+
}
720+
721+
impl Initializer for PyByteArray {
722+
type Args = ByteInnerNewOptions;
723+
724+
fn init(zelf: PyRef<Self>, options: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
725+
// First unpack bytearray and *then* get a lock to set it.
726+
let mut inner = options.get_bytearray_inner(vm)?;
727+
std::mem::swap(&mut *zelf.inner_mut(), &mut inner);
728+
Ok(())
729+
}
730+
}
731+
717732
impl Comparable for PyByteArray {
718733
fn cmp(
719734
zelf: &crate::Py<Self>,

0 commit comments

Comments
 (0)