Skip to content

Commit 86ed4b2

Browse files
committed
PyType::py_new to return Self
1 parent 4828fb3 commit 86ed4b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+551
-439
lines changed

crates/stdlib/src/array.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ mod array {
4545
atomic_func,
4646
builtins::{
4747
PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat,
48-
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyTypeRef,
48+
PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyType,
49+
PyTypeRef,
4950
},
5051
class_or_notimplemented,
5152
convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
@@ -651,10 +652,10 @@ mod array {
651652
type Args = (ArrayNewArgs, KwArgs);
652653

653654
fn py_new(
654-
cls: PyTypeRef,
655+
cls: &Py<PyType>,
655656
(ArrayNewArgs { spec, init }, kwargs): Self::Args,
656657
vm: &VirtualMachine,
657-
) -> PyResult {
658+
) -> PyResult<Self> {
658659
let spec = spec.as_str().chars().exactly_one().map_err(|_| {
659660
vm.new_type_error("array() argument 1 must be a unicode character, not str")
660661
})?;
@@ -701,8 +702,7 @@ mod array {
701702
}
702703
}
703704

704-
let zelf = Self::from(array).into_ref_with_type(vm, cls)?;
705-
Ok(zelf.into())
705+
Ok(Self::from(array))
706706
}
707707
}
708708

crates/stdlib/src/bz2.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ mod _bz2 {
88
DecompressArgs, DecompressError, DecompressState, DecompressStatus, Decompressor,
99
};
1010
use crate::vm::{
11-
VirtualMachine,
12-
builtins::{PyBytesRef, PyTypeRef},
11+
Py, VirtualMachine,
12+
builtins::{PyBytesRef, PyType},
1313
common::lock::PyMutex,
1414
function::{ArgBytesLike, OptionalArg},
15-
object::{PyPayload, PyResult},
15+
object::PyResult,
1616
types::Constructor,
1717
};
1818
use bzip2::{Decompress, Status, write::BzEncoder};
@@ -61,12 +61,10 @@ mod _bz2 {
6161
impl Constructor for BZ2Decompressor {
6262
type Args = ();
6363

64-
fn py_new(cls: PyTypeRef, _: Self::Args, vm: &VirtualMachine) -> PyResult {
65-
Self {
64+
fn py_new(_cls: &Py<PyType>, _args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
65+
Ok(Self {
6666
state: PyMutex::new(DecompressState::new(Decompress::new(false), vm)),
67-
}
68-
.into_ref_with_type(vm, cls)
69-
.map(Into::into)
67+
})
7068
}
7169
}
7270

@@ -132,8 +130,11 @@ mod _bz2 {
132130
impl Constructor for BZ2Compressor {
133131
type Args = (OptionalArg<i32>,);
134132

135-
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
136-
let (compresslevel,) = args;
133+
fn py_new(
134+
_cls: &Py<PyType>,
135+
(compresslevel,): Self::Args,
136+
vm: &VirtualMachine,
137+
) -> PyResult<Self> {
137138
// TODO: seriously?
138139
// compresslevel.unwrap_or(bzip2::Compression::best().level().try_into().unwrap());
139140
let compresslevel = compresslevel.unwrap_or(9);
@@ -144,14 +145,12 @@ mod _bz2 {
144145
}
145146
};
146147

147-
Self {
148+
Ok(Self {
148149
state: PyMutex::new(CompressorState {
149150
flushed: false,
150151
encoder: Some(BzEncoder::new(Vec::new(), level)),
151152
}),
152-
}
153-
.into_ref_with_type(vm, cls)
154-
.map(Into::into)
153+
})
155154
}
156155
}
157156

crates/stdlib/src/contextvars.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ thread_local! {
2424
mod _contextvars {
2525
use crate::vm::{
2626
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, atomic_func,
27-
builtins::{PyGenericAlias, PyStrRef, PyTypeRef},
27+
builtins::{PyGenericAlias, PyStrRef, PyType, PyTypeRef},
2828
class::StaticType,
2929
common::hash::PyHash,
3030
function::{ArgCallable, FuncArgs, OptionalArg},
@@ -244,8 +244,8 @@ mod _contextvars {
244244

245245
impl Constructor for PyContext {
246246
type Args = ();
247-
fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
248-
Ok(Self::empty(vm).into_pyobject(vm))
247+
fn py_new(_cls: &Py<PyType>, _args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
248+
Ok(Self::empty(vm))
249249
}
250250
}
251251

@@ -488,22 +488,28 @@ mod _contextvars {
488488

489489
impl Constructor for ContextVar {
490490
type Args = ContextVarOptions;
491-
fn py_new(_cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
491+
492+
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
493+
let args: Self::Args = args.bind(vm)?;
492494
let var = Self {
493495
name: args.name.to_string(),
494496
default: args.default.into_option(),
495497
cached_id: 0.into(),
496498
cached: AtomicCell::new(None),
497499
hash: UnsafeCell::new(0),
498500
};
499-
let py_var = var.into_ref(&vm.ctx);
501+
let py_var = var.into_ref_with_type(vm, cls)?;
500502

501503
unsafe {
502504
// SAFETY: py_var is not exposed to python memory model yet
503505
*py_var.hash.get() = Self::generate_hash(&py_var, vm)
504506
};
505507
Ok(py_var.into())
506508
}
509+
510+
fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
511+
unreachable!("use slot_new")
512+
}
507513
}
508514

509515
impl std::hash::Hash for ContextVar {
@@ -578,8 +584,8 @@ mod _contextvars {
578584
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
579585
Err(vm.new_runtime_error("Tokens can only be created by ContextVars"))
580586
}
581-
fn py_new(_cls: PyTypeRef, _args: Self::Args, _vm: &VirtualMachine) -> PyResult {
582-
unreachable!()
587+
fn py_new(_cls: &Py<PyType>, _args: Self::Args, _vm: &VirtualMachine) -> PyResult<Self> {
588+
unreachable!("use slot_new")
583589
}
584590
}
585591

crates/stdlib/src/csv.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ mod _csv {
6868
impl Constructor for PyDialect {
6969
type Args = PyObjectRef;
7070

71-
fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
72-
Self::try_from_object(vm, ctx)?
73-
.into_ref_with_type(vm, cls)
74-
.map(Into::into)
71+
fn py_new(_cls: &Py<PyType>, ctx: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
72+
Self::try_from_object(vm, ctx)
7573
}
7674
}
7775
#[pyclass(with(Constructor))]

crates/stdlib/src/json.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod _json {
66
use super::machinery;
77
use crate::vm::{
88
AsObject, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
9-
builtins::{PyBaseExceptionRef, PyStrRef, PyType, PyTypeRef},
9+
builtins::{PyBaseExceptionRef, PyStrRef, PyType},
1010
convert::{ToPyObject, ToPyResult},
1111
function::{IntoFuncArgs, OptionalArg},
1212
protocol::PyIterReturn,
@@ -33,7 +33,7 @@ mod _json {
3333
impl Constructor for JsonScanner {
3434
type Args = PyObjectRef;
3535

36-
fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
36+
fn py_new(_cls: &Py<PyType>, ctx: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
3737
let strict = ctx.get_attr("strict", vm)?.try_to_bool(vm)?;
3838
let object_hook = vm.option_if_none(ctx.get_attr("object_hook", vm)?);
3939
let object_pairs_hook = vm.option_if_none(ctx.get_attr("object_pairs_hook", vm)?);
@@ -52,17 +52,15 @@ mod _json {
5252
};
5353
let parse_constant = ctx.get_attr("parse_constant", vm)?;
5454

55-
Self {
55+
Ok(Self {
5656
strict,
5757
object_hook,
5858
object_pairs_hook,
5959
parse_float,
6060
parse_int,
6161
parse_constant,
6262
ctx,
63-
}
64-
.into_ref_with_type(vm, cls)
65-
.map(Into::into)
63+
})
6664
}
6765
}
6866

crates/stdlib/src/lzma.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ mod _lzma {
3333
LZMA_PRESET_LEVEL_MASK as PRESET_LEVEL_MASK,
3434
};
3535
use rustpython_common::lock::PyMutex;
36-
use rustpython_vm::builtins::{PyBaseExceptionRef, PyBytesRef, PyTypeRef};
36+
use rustpython_vm::builtins::{PyBaseExceptionRef, PyBytesRef, PyType, PyTypeRef};
3737
use rustpython_vm::convert::ToPyException;
3838
use rustpython_vm::function::ArgBytesLike;
3939
use rustpython_vm::types::Constructor;
40-
use rustpython_vm::{PyObjectRef, PyPayload, PyResult, VirtualMachine};
40+
use rustpython_vm::{Py, PyObjectRef, PyPayload, PyResult, VirtualMachine};
4141
use std::fmt;
4242
use xz2::stream::{Action, Check, Error, Filters, LzmaOptions, Status, Stream};
4343

@@ -148,7 +148,7 @@ mod _lzma {
148148
impl Constructor for LZMADecompressor {
149149
type Args = LZMADecompressorConstructorArgs;
150150

151-
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
151+
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
152152
if args.format == FORMAT_RAW && args.mem_limit.is_some() {
153153
return Err(vm.new_value_error("Cannot specify memory limit with FORMAT_RAW"));
154154
}
@@ -161,15 +161,13 @@ mod _lzma {
161161
// TODO: FORMAT_RAW
162162
_ => return Err(new_lzma_error("Invalid format", vm)),
163163
};
164-
Self {
164+
Ok(Self {
165165
state: PyMutex::new(DecompressState::new(
166166
stream_result
167167
.map_err(|_| new_lzma_error("Failed to initialize decoder", vm))?,
168168
vm,
169169
)),
170-
}
171-
.into_ref_with_type(vm, cls)
172-
.map(Into::into)
170+
})
173171
}
174172
}
175173

@@ -366,7 +364,7 @@ mod _lzma {
366364
impl Constructor for LZMACompressor {
367365
type Args = LZMACompressorConstructorArgs;
368366

369-
fn py_new(_cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
367+
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
370368
let preset = args.preset.unwrap_or(PRESET_DEFAULT);
371369
#[allow(clippy::unnecessary_cast)]
372370
if args.format != FORMAT_XZ as i32
@@ -392,8 +390,7 @@ mod _lzma {
392390
};
393391
Ok(Self {
394392
state: PyMutex::new(CompressState::new(CompressorInner::new(stream))),
395-
}
396-
.into_pyobject(vm))
393+
})
397394
}
398395
}
399396

crates/stdlib/src/mmap.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod mmap {
1111
use crate::vm::{
1212
AsObject, FromArgs, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
1313
TryFromBorrowedObject, VirtualMachine, atomic_func,
14-
builtins::{PyBytes, PyBytesRef, PyInt, PyIntRef, PyTypeRef},
14+
builtins::{PyBytes, PyBytesRef, PyInt, PyIntRef, PyType, PyTypeRef},
1515
byte::{bytes_from_object, value_from_object},
1616
convert::ToPyException,
1717
function::{ArgBytesLike, FuncArgs, OptionalArg},
@@ -388,7 +388,7 @@ mod mmap {
388388
type Args = MmapNewArgs;
389389

390390
#[cfg(unix)]
391-
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
391+
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
392392
use libc::{MAP_PRIVATE, MAP_SHARED, PROT_READ, PROT_WRITE};
393393

394394
let mut map_size = args.validate_new_args(vm)?;
@@ -477,7 +477,7 @@ mod mmap {
477477
}()
478478
.map_err(|e| e.to_pyexception(vm))?;
479479

480-
let m_obj = Self {
480+
Ok(Self {
481481
closed: AtomicCell::new(false),
482482
mmap: PyMutex::new(Some(mmap)),
483483
fd: AtomicCell::new(fd.map_or(-1, |fd| fd.into_raw())),
@@ -486,13 +486,11 @@ mod mmap {
486486
pos: AtomicCell::new(0),
487487
exports: AtomicCell::new(0),
488488
access,
489-
};
490-
491-
m_obj.into_ref_with_type(vm, cls).map(Into::into)
489+
})
492490
}
493491

494492
#[cfg(windows)]
495-
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
493+
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
496494
let mut map_size = args.validate_new_args(vm)?;
497495
let MmapNewArgs {
498496
fileno,
@@ -627,7 +625,7 @@ mod mmap {
627625
(INVALID_HANDLE_VALUE as isize, MmapObj::Write(mmap))
628626
};
629627

630-
let m_obj = Self {
628+
Ok(Self {
631629
closed: AtomicCell::new(false),
632630
mmap: PyMutex::new(Some(mmap)),
633631
handle: AtomicCell::new(handle),
@@ -636,9 +634,7 @@ mod mmap {
636634
pos: AtomicCell::new(0),
637635
exports: AtomicCell::new(0),
638636
access,
639-
};
640-
641-
m_obj.into_ref_with_type(vm, cls).map(Into::into)
637+
})
642638
}
643639
}
644640

crates/stdlib/src/overlapped.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod _overlapped {
99

1010
use crate::vm::{
1111
Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
12-
builtins::{PyBaseExceptionRef, PyBytesRef, PyTypeRef},
12+
builtins::{PyBaseExceptionRef, PyBytesRef, PyType},
1313
common::lock::PyMutex,
1414
convert::{ToPyException, ToPyObject},
1515
protocol::PyBuffer,
@@ -264,7 +264,11 @@ mod _overlapped {
264264
impl Constructor for Overlapped {
265265
type Args = (isize,);
266266

267-
fn py_new(cls: PyTypeRef, (mut event,): Self::Args, vm: &VirtualMachine) -> PyResult {
267+
fn py_new(
268+
_cls: &Py<PyType>,
269+
(mut event,): Self::Args,
270+
vm: &VirtualMachine,
271+
) -> PyResult<Self> {
268272
if event == INVALID_HANDLE_VALUE {
269273
event = unsafe {
270274
windows_sys::Win32::System::Threading::CreateEventA(
@@ -289,10 +293,9 @@ mod _overlapped {
289293
error: 0,
290294
data: OverlappedData::None,
291295
};
292-
let overlapped = Overlapped {
296+
Ok(Overlapped {
293297
inner: PyMutex::new(inner),
294-
};
295-
overlapped.into_ref_with_type(vm, cls).map(Into::into)
298+
})
296299
}
297300
}
298301

crates/stdlib/src/pystruct.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) mod _struct {
1212
use crate::vm::{
1313
AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine,
1414
buffer::{FormatSpec, new_struct_error, struct_error_type},
15-
builtins::{PyBytes, PyStr, PyStrRef, PyTupleRef, PyTypeRef},
15+
builtins::{PyBytes, PyStr, PyStrRef, PyTupleRef, PyType, PyTypeRef},
1616
function::{ArgBytesLike, ArgMemoryBuffer, PosArgs},
1717
match_class,
1818
protocol::PyIterReturn,
@@ -241,12 +241,10 @@ pub(crate) mod _struct {
241241
impl Constructor for PyStruct {
242242
type Args = IntoStructFormatBytes;
243243

244-
fn py_new(cls: PyTypeRef, fmt: Self::Args, vm: &VirtualMachine) -> PyResult {
244+
fn py_new(_cls: &Py<PyType>, fmt: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
245245
let spec = fmt.format_spec(vm)?;
246246
let format = fmt.0;
247-
Self { spec, format }
248-
.into_ref_with_type(vm, cls)
249-
.map(Into::into)
247+
Ok(Self { spec, format })
250248
}
251249
}
252250

0 commit comments

Comments
 (0)