Skip to content

Commit c4b80b3

Browse files
committed
IntoObject::into_object() and PyValue::into_pyobject(vm)
1 parent e0ce577 commit c4b80b3

Some content is hidden

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

41 files changed

+148
-153
lines changed

benches/microbenchmarks.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use criterion::{
33
Criterion, Throughput,
44
};
55
use rustpython_compiler::Mode;
6-
use rustpython_vm::{
7-
common::ascii, InitParameter, Interpreter, PyObjectWrap, PyResult, PySettings,
8-
};
6+
use rustpython_vm::{common::ascii, InitParameter, Interpreter, PyResult, PySettings};
97
use std::{
108
ffi, fs, io,
119
path::{Path, PathBuf},

stdlib/src/array.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ mod array {
480480
$f_swap(self)
481481
}
482482
fn to_object(self, vm: &VirtualMachine) -> PyObjectRef {
483-
$f_to(self).into_object(vm)
483+
$f_to(self).into_pyobject(vm)
484484
}
485485
}
486486
)*};
@@ -1117,7 +1117,7 @@ mod array {
11171117
let typecode = vm.ctx.new_str(array.typecode_str());
11181118
let bytes = vm.ctx.new_bytes(array.get_bytes().to_vec());
11191119
let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
1120-
let code = PyInt::from(u8::from(code)).into_object(vm);
1120+
let code = PyInt::from(u8::from(code)).into_pyobject(vm);
11211121
let module = vm.import("array", None, 0)?;
11221122
let func = module.get_attr("_array_reconstructor", vm)?;
11231123
Ok((
@@ -1265,7 +1265,7 @@ mod array {
12651265
position: AtomicUsize::new(0),
12661266
array: zelf,
12671267
}
1268-
.into_object(vm))
1268+
.into_pyobject(vm))
12691269
}
12701270
}
12711271

stdlib/src/hashlib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mod hashlib {
7777

7878
#[pyslot]
7979
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
80-
Ok(PyHasher::new("md5", HashWrapper::md5()).into_object(vm))
80+
Ok(PyHasher::new("md5", HashWrapper::md5()).into_pyobject(vm))
8181
}
8282

8383
#[pyproperty]

vm/src/builtins/bytearray.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use crate::{
3333
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
3434
},
3535
utils::Either,
36-
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
37-
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
36+
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
37+
TryFromBorrowedObject, TryFromObject, VirtualMachine,
3838
};
3939
use bstr::ByteSlice;
4040
use std::{borrow::Cow, mem::size_of};
@@ -751,7 +751,7 @@ static BUFFER_METHODS: BufferMethods = BufferMethods {
751751
impl AsBuffer for PyByteArray {
752752
fn as_buffer(zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
753753
Ok(PyBuffer::new(
754-
zelf.to_owned().into_object(),
754+
zelf.to_owned().into(),
755755
BufferDescriptor::simple(zelf.len(), false),
756756
&BUFFER_METHODS,
757757
))
@@ -789,12 +789,12 @@ impl PyByteArray {
789789
Self::sequence_downcast(seq)
790790
.inner()
791791
.concat(other, vm)
792-
.map(|x| PyByteArray::from(x).into_object(vm))
792+
.map(|x| PyByteArray::from(x).into_pyobject(vm))
793793
}),
794794
repeat: Some(|seq, n, vm| {
795795
Self::sequence_downcast(seq)
796796
.mul(n as isize, vm)
797-
.map(|x| x.into_object(vm))
797+
.map(|x| x.into_pyobject(vm))
798798
}),
799799
item: Some(|seq, i, vm| {
800800
Self::sequence_downcast(seq)
@@ -833,7 +833,7 @@ impl Iterable for PyByteArray {
833833
Ok(PyByteArrayIterator {
834834
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
835835
}
836-
.into_object(vm))
836+
.into_pyobject(vm))
837837
}
838838
}
839839

vm/src/builtins/bytes.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::{
2020
IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
2121
},
2222
utils::Either,
23-
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
24-
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
23+
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
24+
TryFromBorrowedObject, TryFromObject, VirtualMachine,
2525
};
2626
use bstr::ByteSlice;
2727
use std::{borrow::Cow, mem::size_of, ops::Deref};
@@ -567,7 +567,7 @@ static BUFFER_METHODS: BufferMethods = BufferMethods {
567567
impl AsBuffer for PyBytes {
568568
fn as_buffer(zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
569569
let buf = PyBuffer::new(
570-
zelf.to_owned().into_object(),
570+
zelf.to_owned().into(),
571571
BufferDescriptor::simple(zelf.len(), true),
572572
&BUFFER_METHODS,
573573
);
@@ -657,7 +657,7 @@ impl Iterable for PyBytes {
657657
Ok(PyBytesIterator {
658658
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
659659
}
660-
.into_object(vm))
660+
.into_pyobject(vm))
661661
}
662662
}
663663

vm/src/builtins/dict.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl PyDict {
374374
if let Ok(other) = dicted {
375375
let other_cp = other.copy();
376376
PyDict::merge_dict(&other_cp.entries, zelf, vm)?;
377-
return Ok(other_cp.into_object(vm));
377+
return Ok(other_cp.into_pyobject(vm));
378378
}
379379
Ok(vm.ctx.not_implemented())
380380
}
@@ -385,7 +385,7 @@ impl PyDict {
385385
if let Ok(other) = dicted {
386386
let self_cp = self.copy();
387387
PyDict::merge_dict(&self_cp.entries, other, vm)?;
388-
return Ok(self_cp.into_object(vm));
388+
return Ok(self_cp.into_pyobject(vm));
389389
}
390390
Ok(vm.ctx.not_implemented())
391391
}
@@ -502,7 +502,7 @@ impl Unhashable for PyDict {}
502502

503503
impl Iterable for PyDict {
504504
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
505-
Ok(PyDictKeyIterator::new(zelf).into_object(vm))
505+
Ok(PyDictKeyIterator::new(zelf).into_pyobject(vm))
506506
}
507507
}
508508

@@ -737,7 +737,7 @@ macro_rules! dict_view {
737737

738738
impl Iterable for $name {
739739
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
740-
Ok($iter_name::new(zelf.dict.clone()).into_object(vm))
740+
Ok($iter_name::new(zelf.dict.clone()).into_pyobject(vm))
741741
}
742742
}
743743

@@ -1011,7 +1011,7 @@ trait ViewSetOps: DictView {
10111011
}
10121012
ref _set @ PySet => {
10131013
let inner = Self::to_set(zelf.to_owned(), vm)?;
1014-
let zelf_set = PySet { inner }.into_object(vm);
1014+
let zelf_set = PySet { inner }.into_pyobject(vm);
10151015
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
10161016
}
10171017
_ => {

vm/src/builtins/frame.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
frame::{Frame, FrameRef},
88
pyclass::PyClassImpl,
99
types::{Constructor, Unconstructible},
10-
AsObject, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine,
10+
AsObject, PyContext, PyObjectRef, PyRef, PyResult, VirtualMachine,
1111
};
1212

1313
pub fn init(context: &PyContext) {
@@ -46,7 +46,7 @@ impl FrameRef {
4646

4747
#[pyproperty]
4848
fn f_locals(self, vm: &VirtualMachine) -> PyResult {
49-
self.locals(vm).map(|x| x.into_object())
49+
self.locals(vm).map(Into::into)
5050
}
5151

5252
#[pyproperty]

vm/src/builtins/function.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ impl PyFunction {
311311
let is_gen = code.flags.contains(bytecode::CodeFlags::IS_GENERATOR);
312312
let is_coro = code.flags.contains(bytecode::CodeFlags::IS_COROUTINE);
313313
match (is_gen, is_coro) {
314-
(true, false) => Ok(PyGenerator::new(frame, self.name()).into_object(vm)),
315-
(false, true) => Ok(PyCoroutine::new(frame, self.name()).into_object(vm)),
316-
(true, true) => Ok(PyAsyncGen::new(frame, self.name()).into_object(vm)),
314+
(true, false) => Ok(PyGenerator::new(frame, self.name()).into_pyobject(vm)),
315+
(false, true) => Ok(PyCoroutine::new(frame, self.name()).into_pyobject(vm)),
316+
(true, true) => Ok(PyAsyncGen::new(frame, self.name()).into_pyobject(vm)),
317317
(false, false) => vm.run_frame_full(frame),
318318
}
319319
}

vm/src/builtins/genericalias.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ impl PyGenericAlias {
142142
vm,
143143
)?;
144144

145-
Ok(PyGenericAlias::new(self.origin.clone(), new_args.to_pyobject(vm), vm).into_object(vm))
145+
Ok(
146+
PyGenericAlias::new(self.origin.clone(), new_args.to_pyobject(vm), vm)
147+
.into_pyobject(vm),
148+
)
146149
}
147150

148151
#[pymethod(magic)]

vm/src/builtins/int.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl PyValue for PyInt {
5858
&vm.ctx.types.int_type
5959
}
6060

61-
fn into_object(self, vm: &VirtualMachine) -> PyObjectRef {
61+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
6262
vm.ctx.new_int(self.value).into()
6363
}
6464

vm/src/builtins/iter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ impl PySequenceIterator {
191191
if let IterStatus::Active(obj) = &internal.status {
192192
let seq = PySequence::with_methods(obj, self.seq_methods.clone());
193193
seq.length(vm)
194-
.map(|x| PyInt::from(x).into_object(vm))
194+
.map(|x| PyInt::from(x).into_pyobject(vm))
195195
.unwrap_or_else(|_| vm.ctx.not_implemented())
196196
} else {
197-
PyInt::from(0).into_object(vm)
197+
PyInt::from(0).into_pyobject(vm)
198198
}
199199
}
200200

vm/src/builtins/list.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use crate::{
1616
},
1717
utils::collection_repr,
1818
vm::{ReprGuard, VirtualMachine},
19-
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
20-
PyValue,
19+
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
2120
};
2221
use std::{borrow::Cow, fmt, ops::DerefMut};
2322

@@ -412,12 +411,12 @@ impl PyList {
412411
concat: Some(|seq, other, vm| {
413412
Self::sequence_downcast(seq)
414413
.concat(other, vm)
415-
.map(|x| x.into_object())
414+
.map(|x| x.into())
416415
}),
417416
repeat: Some(|seq, n, vm| {
418417
Self::sequence_downcast(seq)
419418
.mul(n as isize, vm)
420-
.map(|x| x.into_object())
419+
.map(|x| x.into())
421420
}),
422421
item: Some(|seq, i, vm| {
423422
Self::sequence_downcast(seq)
@@ -443,7 +442,7 @@ impl PyList {
443442
inplace_repeat: Some(|seq, n, vm| {
444443
let zelf = Self::sequence_downcast(seq);
445444
zelf.borrow_vec_mut().imul(vm, n as isize)?;
446-
Ok(zelf.to_owned().into_object())
445+
Ok(zelf.to_owned().into())
447446
}),
448447
};
449448
}
@@ -453,7 +452,7 @@ impl Iterable for PyList {
453452
Ok(PyListIterator {
454453
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
455454
}
456-
.into_object(vm))
455+
.into_pyobject(vm))
457456
}
458457
}
459458

vm/src/builtins/memory.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::{
1919
sliceable::wrap_index,
2020
types::{AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, PyComparisonOp},
2121
utils::Either,
22-
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
23-
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
22+
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
23+
TryFromBorrowedObject, TryFromObject, VirtualMachine,
2424
};
2525
use crossbeam_utils::atomic::AtomicCell;
2626
use itertools::Itertools;
@@ -255,7 +255,7 @@ impl PyMemoryView {
255255
other.init_slice(slice, 0, vm)?;
256256
other.init_len();
257257

258-
Ok(other.into_ref(vm).into_object())
258+
Ok(other.into_ref(vm).into())
259259
}
260260

261261
fn getitem_by_multi_idx(&self, indexes: &[isize], vm: &VirtualMachine) -> PyResult {
@@ -270,7 +270,7 @@ impl PyMemoryView {
270270
if zelf.desc.ndim() == 0 {
271271
// 0-d memoryview can be referenced using mv[...] or mv[()] only
272272
if needle.is(&vm.ctx.ellipsis) {
273-
return Ok(zelf.into_object());
273+
return Ok(zelf.into());
274274
}
275275
if let Some(tuple) = needle.payload::<PyTuple>() {
276276
if tuple.is_empty() {
@@ -539,9 +539,7 @@ impl PyMemoryView {
539539

540540
let mut v = Vec::with_capacity(shape);
541541
for _ in 0..shape {
542-
let obj = self
543-
._to_list(bytes, index + suboffset, dim + 1, vm)?
544-
.into_object();
542+
let obj = self._to_list(bytes, index + suboffset, dim + 1, vm)?.into();
545543
v.push(obj);
546544
index += stride;
547545
}
@@ -938,7 +936,7 @@ impl AsBuffer for PyMemoryView {
938936
Err(vm.new_value_error("operation forbidden on released memoryview object".to_owned()))
939937
} else {
940938
Ok(PyBuffer::new(
941-
zelf.to_owned().into_object(),
939+
zelf.to_owned().into(),
942940
zelf.desc.clone(),
943941
&BUFFER_METHODS,
944942
))

vm/src/builtins/pystr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ impl Iterable for PyStr {
12641264
Ok(PyStrIterator {
12651265
internal: PyMutex::new((PositionIterInternal::new(zelf, 0), 0)),
12661266
}
1267-
.into_object(vm))
1267+
.into_pyobject(vm))
12681268
}
12691269
}
12701270

vm/src/builtins/pysuper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl GetDescriptor for PySuper {
171171
}
172172
let zelf_class = zelf.as_object().class();
173173
if zelf_class.is(&vm.ctx.types.super_type) {
174-
Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_object(vm))
174+
Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_pyobject(vm))
175175
} else {
176176
let obj = vm.unwrap_or_none(zelf.obj.clone().map(|(o, _)| o));
177177
vm.invoke(&zelf.class(), (zelf.typ.clone(), obj))

vm/src/builtins/pytype.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -477,19 +477,19 @@ impl PyType {
477477
let mut attributes = dict.to_attributes();
478478
if let Some(f) = attributes.get_mut("__new__") {
479479
if f.class().is(&vm.ctx.types.function_type) {
480-
*f = PyStaticMethod::from(f.clone()).into_object(vm);
480+
*f = PyStaticMethod::from(f.clone()).into_pyobject(vm);
481481
}
482482
}
483483

484484
if let Some(f) = attributes.get_mut("__init_subclass__") {
485485
if f.class().is(&vm.ctx.types.function_type) {
486-
*f = PyClassMethod::from(f.clone()).into_object(vm);
486+
*f = PyClassMethod::from(f.clone()).into_pyobject(vm);
487487
}
488488
}
489489

490490
if let Some(f) = attributes.get_mut("__class_getitem__") {
491491
if f.class().is(&vm.ctx.types.function_type) {
492-
*f = PyClassMethod::from(f.clone()).into_object(vm);
492+
*f = PyClassMethod::from(f.clone()).into_pyobject(vm);
493493
}
494494
}
495495

0 commit comments

Comments
 (0)