IntoObject::into_object() and PyValue::into_pyobject(vm)

This commit is contained in:
Jeong Yunwon committed 2022-04-19 00:38:18 +09:00
1 parent e0ce5779e6
commit c4b80b3a24
41 files changed
+148 -153

No files matched your search

+1 -3
View File
@@ -3,9 +3,7 @@ use criterion::{
Criterion, Throughput,
};
use rustpython_compiler::Mode;
use rustpython_vm::{
common::ascii, InitParameter, Interpreter, PyObjectWrap, PyResult, PySettings,
};
use rustpython_vm::{common::ascii, InitParameter, Interpreter, PyResult, PySettings};
use std::{
ffi, fs, io,
path::{Path, PathBuf},
+3 -3
View File
@@ -480,7 +480,7 @@ mod array {
$f_swap(self)
}
fn to_object(self, vm: &VirtualMachine) -> PyObjectRef {
$f_to(self).into_object(vm)
$f_to(self).into_pyobject(vm)
}
}
)*};
@@ -1117,7 +1117,7 @@ mod array {
let typecode = vm.ctx.new_str(array.typecode_str());
let bytes = vm.ctx.new_bytes(array.get_bytes().to_vec());
let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
let code = PyInt::from(u8::from(code)).into_object(vm);
let code = PyInt::from(u8::from(code)).into_pyobject(vm);
let module = vm.import("array", None, 0)?;
let func = module.get_attr("_array_reconstructor", vm)?;
Ok((
@@ -1265,7 +1265,7 @@ mod array {
position: AtomicUsize::new(0),
array: zelf,
}
.into_object(vm))
.into_pyobject(vm))
}
}
+1 -1
View File
@@ -77,7 +77,7 @@ mod hashlib {
#[pyslot]
fn slot_new(_cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Ok(PyHasher::new("md5", HashWrapper::md5()).into_object(vm))
Ok(PyHasher::new("md5", HashWrapper::md5()).into_pyobject(vm))
}
#[pyproperty]
+6 -6
View File
@@ -33,8 +33,8 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
utils::Either,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
};
use bstr::ByteSlice;
use std::{borrow::Cow, mem::size_of};
@@ -751,7 +751,7 @@ static BUFFER_METHODS: BufferMethods = BufferMethods {
impl AsBuffer for PyByteArray {
fn as_buffer(zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
Ok(PyBuffer::new(
zelf.to_owned().into_object(),
zelf.to_owned().into(),
BufferDescriptor::simple(zelf.len(), false),
&BUFFER_METHODS,
))
@@ -789,12 +789,12 @@ impl PyByteArray {
Self::sequence_downcast(seq)
.inner()
.concat(other, vm)
.map(|x| PyByteArray::from(x).into_object(vm))
.map(|x| PyByteArray::from(x).into_pyobject(vm))
}),
repeat: Some(|seq, n, vm| {
Self::sequence_downcast(seq)
.mul(n as isize, vm)
.map(|x| x.into_object(vm))
.map(|x| x.into_pyobject(vm))
}),
item: Some(|seq, i, vm| {
Self::sequence_downcast(seq)
@@ -833,7 +833,7 @@ impl Iterable for PyByteArray {
Ok(PyByteArrayIterator {
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
.into_pyobject(vm))
}
}
+4 -4
View File
@@ -20,8 +20,8 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
},
utils::Either,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
};
use bstr::ByteSlice;
use std::{borrow::Cow, mem::size_of, ops::Deref};
@@ -567,7 +567,7 @@ static BUFFER_METHODS: BufferMethods = BufferMethods {
impl AsBuffer for PyBytes {
fn as_buffer(zelf: &PyObjectView<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
let buf = PyBuffer::new(
zelf.to_owned().into_object(),
zelf.to_owned().into(),
BufferDescriptor::simple(zelf.len(), true),
&BUFFER_METHODS,
);
@@ -657,7 +657,7 @@ impl Iterable for PyBytes {
Ok(PyBytesIterator {
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
.into_pyobject(vm))
}
}
+5 -5
View File
@@ -374,7 +374,7 @@ impl PyDict {
if let Ok(other) = dicted {
let other_cp = other.copy();
PyDict::merge_dict(&other_cp.entries, zelf, vm)?;
return Ok(other_cp.into_object(vm));
return Ok(other_cp.into_pyobject(vm));
}
Ok(vm.ctx.not_implemented())
}
@@ -385,7 +385,7 @@ impl PyDict {
if let Ok(other) = dicted {
let self_cp = self.copy();
PyDict::merge_dict(&self_cp.entries, other, vm)?;
return Ok(self_cp.into_object(vm));
return Ok(self_cp.into_pyobject(vm));
}
Ok(vm.ctx.not_implemented())
}
@@ -502,7 +502,7 @@ impl Unhashable for PyDict {}
impl Iterable for PyDict {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyDictKeyIterator::new(zelf).into_object(vm))
Ok(PyDictKeyIterator::new(zelf).into_pyobject(vm))
}
}
@@ -737,7 +737,7 @@ macro_rules! dict_view {
impl Iterable for $name {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok($iter_name::new(zelf.dict.clone()).into_object(vm))
Ok($iter_name::new(zelf.dict.clone()).into_pyobject(vm))
}
}
@@ -1011,7 +1011,7 @@ trait ViewSetOps: DictView {
}
ref _set @ PySet => {
let inner = Self::to_set(zelf.to_owned(), vm)?;
let zelf_set = PySet { inner }.into_object(vm);
let zelf_set = PySet { inner }.into_pyobject(vm);
PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm)
}
_ => {
+2 -2
View File
@@ -7,7 +7,7 @@ use crate::{
frame::{Frame, FrameRef},
pyclass::PyClassImpl,
types::{Constructor, Unconstructible},
AsObject, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine,
AsObject, PyContext, PyObjectRef, PyRef, PyResult, VirtualMachine,
};
pub fn init(context: &PyContext) {
@@ -46,7 +46,7 @@ impl FrameRef {
#[pyproperty]
fn f_locals(self, vm: &VirtualMachine) -> PyResult {
self.locals(vm).map(|x| x.into_object())
self.locals(vm).map(Into::into)
}
#[pyproperty]
+3 -3
View File
@@ -311,9 +311,9 @@ impl PyFunction {
let is_gen = code.flags.contains(bytecode::CodeFlags::IS_GENERATOR);
let is_coro = code.flags.contains(bytecode::CodeFlags::IS_COROUTINE);
match (is_gen, is_coro) {
(true, false) => Ok(PyGenerator::new(frame, self.name()).into_object(vm)),
(false, true) => Ok(PyCoroutine::new(frame, self.name()).into_object(vm)),
(true, true) => Ok(PyAsyncGen::new(frame, self.name()).into_object(vm)),
(true, false) => Ok(PyGenerator::new(frame, self.name()).into_pyobject(vm)),
(false, true) => Ok(PyCoroutine::new(frame, self.name()).into_pyobject(vm)),
(true, true) => Ok(PyAsyncGen::new(frame, self.name()).into_pyobject(vm)),
(false, false) => vm.run_frame_full(frame),
}
}
+4 -1
View File
@@ -142,7 +142,10 @@ impl PyGenericAlias {
vm,
)?;
Ok(PyGenericAlias::new(self.origin.clone(), new_args.to_pyobject(vm), vm).into_object(vm))
Ok(
PyGenericAlias::new(self.origin.clone(), new_args.to_pyobject(vm), vm)
.into_pyobject(vm),
)
}
#[pymethod(magic)]
+1 -1
View File
@@ -58,7 +58,7 @@ impl PyValue for PyInt {
&vm.ctx.types.int_type
}
fn into_object(self, vm: &VirtualMachine) -> PyObjectRef {
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(self.value).into()
}
+2 -2
View File
@@ -191,10 +191,10 @@ impl PySequenceIterator {
if let IterStatus::Active(obj) = &internal.status {
let seq = PySequence::with_methods(obj, self.seq_methods.clone());
seq.length(vm)
.map(|x| PyInt::from(x).into_object(vm))
.map(|x| PyInt::from(x).into_pyobject(vm))
.unwrap_or_else(|_| vm.ctx.not_implemented())
} else {
PyInt::from(0).into_object(vm)
PyInt::from(0).into_pyobject(vm)
}
}
+5 -6
View File
@@ -16,8 +16,7 @@ use crate::{
},
utils::collection_repr,
vm::{ReprGuard, VirtualMachine},
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
};
use std::{borrow::Cow, fmt, ops::DerefMut};
@@ -412,12 +411,12 @@ impl PyList {
concat: Some(|seq, other, vm| {
Self::sequence_downcast(seq)
.concat(other, vm)
.map(|x| x.into_object())
.map(|x| x.into())
}),
repeat: Some(|seq, n, vm| {
Self::sequence_downcast(seq)
.mul(n as isize, vm)
.map(|x| x.into_object())
.map(|x| x.into())
}),
item: Some(|seq, i, vm| {
Self::sequence_downcast(seq)
@@ -443,7 +442,7 @@ impl PyList {
inplace_repeat: Some(|seq, n, vm| {
let zelf = Self::sequence_downcast(seq);
zelf.borrow_vec_mut().imul(vm, n as isize)?;
Ok(zelf.to_owned().into_object())
Ok(zelf.to_owned().into())
}),
};
}
@@ -453,7 +452,7 @@ impl Iterable for PyList {
Ok(PyListIterator {
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
.into_pyobject(vm))
}
}
+6 -8
View File
@@ -19,8 +19,8 @@ use crate::{
sliceable::wrap_index,
types::{AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, PyComparisonOp},
utils::Either,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
AsObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;
use itertools::Itertools;
@@ -255,7 +255,7 @@ impl PyMemoryView {
other.init_slice(slice, 0, vm)?;
other.init_len();
Ok(other.into_ref(vm).into_object())
Ok(other.into_ref(vm).into())
}
fn getitem_by_multi_idx(&self, indexes: &[isize], vm: &VirtualMachine) -> PyResult {
@@ -270,7 +270,7 @@ impl PyMemoryView {
if zelf.desc.ndim() == 0 {
// 0-d memoryview can be referenced using mv[...] or mv[()] only
if needle.is(&vm.ctx.ellipsis) {
return Ok(zelf.into_object());
return Ok(zelf.into());
}
if let Some(tuple) = needle.payload::<PyTuple>() {
if tuple.is_empty() {
@@ -539,9 +539,7 @@ impl PyMemoryView {
let mut v = Vec::with_capacity(shape);
for _ in 0..shape {
let obj = self
._to_list(bytes, index + suboffset, dim + 1, vm)?
.into_object();
let obj = self._to_list(bytes, index + suboffset, dim + 1, vm)?.into();
v.push(obj);
index += stride;
}
@@ -938,7 +936,7 @@ impl AsBuffer for PyMemoryView {
Err(vm.new_value_error("operation forbidden on released memoryview object".to_owned()))
} else {
Ok(PyBuffer::new(
zelf.to_owned().into_object(),
zelf.to_owned().into(),
zelf.desc.clone(),
&BUFFER_METHODS,
))
+1 -1
View File
@@ -1264,7 +1264,7 @@ impl Iterable for PyStr {
Ok(PyStrIterator {
internal: PyMutex::new((PositionIterInternal::new(zelf, 0), 0)),
}
.into_object(vm))
.into_pyobject(vm))
}
}
+1 -1
View File
@@ -171,7 +171,7 @@ impl GetDescriptor for PySuper {
}
let zelf_class = zelf.as_object().class();
if zelf_class.is(&vm.ctx.types.super_type) {
Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_object(vm))
Ok(PySuper::new(zelf.typ.clone(), obj, vm)?.into_pyobject(vm))
} else {
let obj = vm.unwrap_or_none(zelf.obj.clone().map(|(o, _)| o));
vm.invoke(&zelf.class(), (zelf.typ.clone(), obj))
+3 -3
View File
@@ -477,19 +477,19 @@ impl PyType {
let mut attributes = dict.to_attributes();
if let Some(f) = attributes.get_mut("__new__") {
if f.class().is(&vm.ctx.types.function_type) {
*f = PyStaticMethod::from(f.clone()).into_object(vm);
*f = PyStaticMethod::from(f.clone()).into_pyobject(vm);
}
}
if let Some(f) = attributes.get_mut("__init_subclass__") {
if f.class().is(&vm.ctx.types.function_type) {
*f = PyClassMethod::from(f.clone()).into_object(vm);
*f = PyClassMethod::from(f.clone()).into_pyobject(vm);
}
}
if let Some(f) = attributes.get_mut("__class_getitem__") {
if f.class().is(&vm.ctx.types.function_type) {
*f = PyClassMethod::from(f.clone()).into_object(vm);
*f = PyClassMethod::from(f.clone()).into_pyobject(vm);
}
}
+13 -8
View File
@@ -242,7 +242,7 @@ impl PyRange {
// always fit in a usize.
length: length.to_usize().unwrap_or(0),
}
.into_object(vm)
.into_pyobject(vm)
} else {
PyLongRangeIterator {
index: AtomicCell::new(0),
@@ -250,7 +250,7 @@ impl PyRange {
step,
length,
}
.into_object(vm)
.into_pyobject(vm)
},
)
}
@@ -284,7 +284,7 @@ impl PyRange {
}
} else {
iter_search(
self.clone().into_object(vm),
self.clone().into_pyobject(vm),
needle,
SearchType::Contains,
vm,
@@ -315,8 +315,13 @@ impl PyRange {
// Fallback to iteration.
Ok(BigInt::from_bytes_be(
Sign::Plus,
&iter_search(self.clone().into_object(vm), needle, SearchType::Index, vm)?
.to_be_bytes(),
&iter_search(
self.clone().into_pyobject(vm),
needle,
SearchType::Index,
vm,
)?
.to_be_bytes(),
))
}
}
@@ -332,7 +337,7 @@ impl PyRange {
} else {
// Dealing with classes who might compare equal with ints in their
// __eq__, slow search.
iter_search(self.clone().into_object(vm), item, SearchType::Count, vm)
iter_search(self.clone().into_pyobject(vm), item, SearchType::Count, vm)
}
}
@@ -493,7 +498,7 @@ impl Iterable for PyRange {
// always fit in a usize.
length: length.to_usize().unwrap_or(0),
}
.into_object(vm))
.into_pyobject(vm))
} else {
Ok(PyLongRangeIterator {
index: AtomicCell::new(0),
@@ -501,7 +506,7 @@ impl Iterable for PyRange {
step: step.clone(),
length,
}
.into_object(vm))
.into_pyobject(vm))
}
}
}
+3 -3
View File
@@ -338,7 +338,7 @@ impl PySetInner {
&PyFrozenSet {
inner: set.inner.copy(),
}
.into_object(vm),
.into_pyobject(vm),
vm,
)
// If operation raised KeyError, report original set (set.remove)
@@ -688,7 +688,7 @@ impl Unhashable for PySet {}
impl Iterable for PySet {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(zelf.inner.iter().into_object(vm))
Ok(zelf.inner.iter().into_pyobject(vm))
}
}
@@ -933,7 +933,7 @@ impl Comparable for PyFrozenSet {
impl Iterable for PyFrozenSet {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(zelf.inner.iter().into_object(vm))
Ok(zelf.inner.iter().into_pyobject(vm))
}
}
+1 -1
View File
@@ -394,7 +394,7 @@ impl Iterable for PyTuple {
Ok(PyTupleIterator {
internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
}
.into_object(vm))
.into_pyobject(vm))
}
}
+3 -3
View File
@@ -7,8 +7,7 @@ use crate::{
function::OptionalArg,
pyclass::PyClassImpl,
types::{Callable, Comparable, Constructor, Hashable, PyComparisonOp},
AsObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
VirtualMachine,
AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
pub use crate::pyobject::PyWeak;
@@ -44,7 +43,8 @@ impl Constructor for PyWeak {
vm: &VirtualMachine,
) -> PyResult {
let weak = referent.downgrade_with_typ(callback.into_option(), cls, vm)?;
Ok(weak.into_object())
let pyref_weak: PyRef<PyWeak> = weak.into();
Ok(pyref_weak.into())
}
}
+12
View File
@@ -0,0 +1,12 @@
use crate::PyObjectRef;
pub trait IntoObject
where
Self: Into<PyObjectRef>,
{
fn into_object(self) -> PyObjectRef {
self.into()
}
}
impl<T> IntoObject for T where T: Into<PyObjectRef> {}
+2
View File
@@ -1,7 +1,9 @@
mod into_object;
mod to_pyobject;
mod transmute_from;
mod try_from;
pub use into_object::IntoObject;
pub use to_pyobject::{ToPyException, ToPyObject, ToPyResult};
pub use transmute_from::TransmuteFromObject;
pub use try_from::{TryFromBorrowedObject, TryFromObject};
+5 -5
View File
@@ -8,7 +8,7 @@ use crate::{
PySlice, PyStr, PyStrRef, PyTraceback, PyTypeRef,
},
bytecode,
convert::ToPyResult,
convert::{IntoObject, ToPyResult},
coroutine::Coro,
exceptions::ExceptionCtor,
function::{ArgMapping, FuncArgs},
@@ -16,8 +16,8 @@ use crate::{
scope::Scope,
stdlib::builtins,
types::PyComparisonOp,
AsObject, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
TryFromObject, VirtualMachine,
AsObject, PyMethod, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
VirtualMachine,
};
use indexmap::IndexMap;
use itertools::Itertools;
@@ -727,7 +727,7 @@ impl ExecutingFrame<'_> {
bytecode::Instruction::YieldValue => {
let value = self.pop_value();
let value = if self.code.flags.contains(bytecode::CodeFlags::IS_COROUTINE) {
PyAsyncGenWrappedValue(value).into_object(vm)
PyAsyncGenWrappedValue(value).into_pyobject(vm)
} else {
value
};
@@ -1606,7 +1606,7 @@ impl ExecutingFrame<'_> {
defaults,
kw_only_defaults,
)
.into_object(vm);
.into_pyobject(vm);
func_obj.set_attr("__doc__", vm.ctx.none(), vm)?;
+8 -10
View File
@@ -3,8 +3,7 @@ use crate::{
builtins::{iter::PySequenceIterator, PyDict, PyDictRef},
convert::ToPyObject,
protocol::{PyIter, PyIterIter, PyMapping, PyMappingMethods},
AsObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject,
VirtualMachine,
AsObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use std::{borrow::Borrow, marker::PhantomData};
@@ -33,9 +32,9 @@ impl AsRef<PyObject> for ArgCallable {
}
}
impl PyObjectWrap for ArgCallable {
fn into_object(self) -> PyObjectRef {
self.obj
impl From<ArgCallable> for PyObjectRef {
fn from(value: ArgCallable) -> PyObjectRef {
value.obj
}
}
@@ -70,7 +69,7 @@ impl<T> ArgIterable<T> {
pub fn iter<'a>(&self, vm: &'a VirtualMachine) -> PyResult<PyIterIter<'a, T>> {
let iter = PyIter::new(match self.iterfn {
Some(f) => f(self.iterable.clone(), vm)?,
None => PySequenceIterator::new(self.iterable.clone(), vm)?.into_object(vm),
None => PySequenceIterator::new(self.iterable.clone(), vm)?.into_pyobject(vm),
});
iter.into_iter(vm)
}
@@ -130,10 +129,9 @@ impl AsRef<PyObject> for ArgMapping {
}
}
impl PyObjectWrap for ArgMapping {
#[inline(always)]
fn into_object(self) -> PyObjectRef {
self.obj
impl From<ArgMapping> for PyObjectRef {
fn from(value: ArgMapping) -> PyObjectRef {
value.obj
}
}
+1 -1
View File
@@ -89,7 +89,7 @@ mod pyobject {
pub use self::convert::{TryFromBorrowedObject, TryFromObject};
// pyobject items
pub use self::pyobject::{
AsObject, PyContext, PyMethod, PyObjectWrap, PyRefExact, PyResult, PyStructSequence, PyValue,
AsObject, PyContext, PyMethod, PyRefExact, PyResult, PyStructSequence, PyValue,
};
// pyobjectrc items
pub use self::pyobject::{PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyRef, PyWeakRef};
+2 -2
View File
@@ -72,7 +72,7 @@ macro_rules! py_namespace {
/// use rustpython_vm::{PyValue};
///
/// # rustpython_vm::Interpreter::default().enter(|vm| {
/// let obj = PyInt::from(0).into_object(vm);
/// let obj = PyInt::from(0).into_pyobject(vm);
/// assert_eq!(
/// "int",
/// match_class!(match obj {
@@ -96,7 +96,7 @@ macro_rules! py_namespace {
/// use rustpython_vm::{ PyValue};
///
/// # rustpython_vm::Interpreter::default().enter(|vm| {
/// let obj = PyInt::from(0).into_object(vm);
/// let obj = PyInt::from(0).into_pyobject(vm);
///
/// let int_value = match_class!(match obj {
/// i @ PyInt => i.as_bigint().clone(),
+5 -5
View File
@@ -9,8 +9,8 @@ use crate::{
pyobject::PyObjectPayload,
sliceable::wrap_index,
types::{Constructor, Unconstructible},
AsObject, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue,
TryFromBorrowedObject, VirtualMachine,
AsObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromBorrowedObject,
VirtualMachine,
};
use itertools::Itertools;
use std::{borrow::Cow, fmt::Debug, ops::Range};
@@ -65,7 +65,7 @@ impl PyBuffer {
pub fn from_byte_vector(bytes: Vec<u8>, vm: &VirtualMachine) -> Self {
let bytes_len = bytes.len();
PyBuffer::new(
PyValue::into_object(VecBuffer::from(bytes), vm),
PyValue::into_pyobject(VecBuffer::from(bytes), vm),
BufferDescriptor::simple(bytes_len, true),
&VEC_BUFFER_METHODS,
)
@@ -421,14 +421,14 @@ impl PyRef<VecBuffer> {
pub fn into_pybuffer(self, readonly: bool) -> PyBuffer {
let len = self.data.lock().len();
PyBuffer::new(
self.into_object(),
self.into(),
BufferDescriptor::simple(len, readonly),
&VEC_BUFFER_METHODS,
)
}
pub fn into_pybuffer_with_descriptor(self, desc: BufferDescriptor) -> PyBuffer {
PyBuffer::new(self.into_object(), desc, &VEC_BUFFER_METHODS)
PyBuffer::new(self.into(), desc, &VEC_BUFFER_METHODS)
}
}
+5 -6
View File
@@ -1,8 +1,7 @@
use crate::{
builtins::iter::PySequenceIterator,
convert::{ToPyObject, ToPyResult},
AsObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject,
VirtualMachine,
AsObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use std::borrow::Borrow;
use std::ops::Deref;
@@ -70,9 +69,9 @@ impl PyIter<PyObjectRef> {
}
}
impl PyObjectWrap for PyIter<PyObjectRef> {
fn into_object(self) -> PyObjectRef {
self.0
impl From<PyIter<PyObjectRef>> for PyObjectRef {
fn from(value: PyIter<PyObjectRef>) -> PyObjectRef {
value.0
}
}
@@ -134,7 +133,7 @@ impl TryFromObject for PyIter<PyObjectRef> {
)))
}
} else if let Ok(seq_iter) = PySequenceIterator::new(iter_target.clone(), vm) {
Ok(Self(seq_iter.into_object(vm)))
Ok(Self(seq_iter.into_pyobject(vm)))
} else {
Err(vm.new_type_error(format!(
"'{}' object is not iterable",
+2 -2
View File
@@ -229,7 +229,7 @@ impl PySequence<'_> {
stop: stop.to_pyobject(vm),
step: None,
};
mapping.subscript(&slice.into_object(vm), vm)
mapping.subscript(&slice.into_pyobject(vm), vm)
} else {
Err(vm.new_type_error(format!("'{}' object is unsliceable", self.obj.class())))
}
@@ -249,7 +249,7 @@ impl PySequence<'_> {
stop: stop.to_pyobject(vm),
step: None,
};
f(&mapping, &slice.into_object(vm), value, vm)
f(&mapping, &slice.into_pyobject(vm), value, vm)
} else {
Err(vm.new_type_error(format!(
"'{}' object doesn't support slice {}",
+2 -19
View File
@@ -526,7 +526,7 @@ where
{
#[inline(always)]
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
PyValue::into_object(self, vm)
PyValue::into_pyobject(self, vm)
}
}
@@ -564,7 +564,7 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static {
fn class(vm: &VirtualMachine) -> &PyTypeRef;
#[inline]
fn into_object(self, vm: &VirtualMachine) -> PyObjectRef {
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
self.into_ref(vm).into()
}
@@ -685,23 +685,6 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static {
}
}
pub trait PyObjectWrap
where
Self: AsObject,
{
fn into_object(self) -> PyObjectRef;
}
impl<T> From<T> for PyObjectRef
where
T: PyObjectWrap,
{
#[inline(always)]
fn from(py_ref: T) -> Self {
py_ref.into_object()
}
}
#[derive(Debug)]
pub enum PyMethod {
Function {
+7 -7
View File
@@ -3,7 +3,7 @@ use crate::common::linked_list::{Link, LinkedList, Pointers};
use crate::common::lock::{PyMutex, PyMutexGuard, PyRwLock};
use crate::common::refcount::RefCount;
use crate::{
_pyobject::{AsObject, PyObjectPayload, PyObjectWrap, PyResult},
_pyobject::{AsObject, PyObjectPayload, PyResult},
builtins::{PyBaseExceptionRef, PyDictRef, PyTypeRef},
vm::VirtualMachine,
};
@@ -792,10 +792,10 @@ impl Borrow<PyObject> for PyObjectWeak {
}
}
impl PyObjectWrap for PyObjectWeak {
impl From<PyObjectWeak> for PyRef<PyWeak> {
#[inline(always)]
fn into_object(self) -> PyObjectRef {
self.weak.into_object()
fn from(value: PyObjectWeak) -> Self {
value.weak
}
}
@@ -999,13 +999,13 @@ where
}
}
impl<T> PyObjectWrap for PyRef<T>
impl<T> From<PyRef<T>> for PyObjectRef
where
T: PyObjectPayload,
{
#[inline]
fn into_object(self) -> PyObjectRef {
let me = ManuallyDrop::new(self);
fn from(value: PyRef<T>) -> Self {
let me = ManuallyDrop::new(value);
PyObjectRef { ptr: me.ptr.cast() }
}
}
+5 -6
View File
@@ -33,8 +33,7 @@ mod builtins {
stdlib::sys,
types::PyComparisonOp,
utils::Either,
AsObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject,
VirtualMachine,
AsObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use num_traits::{Signed, ToPrimitive, Zero};
@@ -433,8 +432,8 @@ mod builtins {
}
#[pyfunction]
fn locals(vm: &VirtualMachine) -> PyResult {
vm.current_locals().map(|x| x.into_object())
fn locals(vm: &VirtualMachine) -> PyResult<ArgMapping> {
vm.current_locals()
}
fn min_or_max(
@@ -690,7 +689,7 @@ mod builtins {
})?;
let len = obj.length(vm)?;
let obj_iterator = PyReverseSequenceIterator::new(obj, len);
Ok(obj_iterator.into_object(vm))
Ok(obj_iterator.into_pyobject(vm))
}
}
@@ -793,7 +792,7 @@ mod builtins {
vm.new_type_error("vars() argument must have __dict__ attribute".to_owned())
})
} else {
Ok(vm.current_locals()?.into_object())
Ok(vm.current_locals()?.into())
}
}
+1 -1
View File
@@ -571,7 +571,7 @@ mod _collections {
impl Iterable for PyDeque {
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
Ok(PyDequeIterator::new(zelf).into_object(vm))
Ok(PyDequeIterator::new(zelf).into_pyobject(vm))
}
}
+2 -2
View File
@@ -114,7 +114,7 @@ mod _io {
types::{Constructor, Destructor, IterNext, Iterable},
utils::Either,
vm::{ReprGuard, VirtualMachine},
AsObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
AsObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject,
};
use bstr::ByteSlice;
@@ -3313,7 +3313,7 @@ mod _io {
fn getbuffer(self, vm: &VirtualMachine) -> PyResult<PyMemoryView> {
let len = self.buffer.read().cursor.get_ref().len();
let buffer = PyBuffer::new(
self.into_object(),
self.into(),
BufferDescriptor::simple(len, false),
&BYTES_IO_BUFFER_METHODS,
);
+1 -1
View File
@@ -484,7 +484,7 @@ mod _sre {
/* get segment following last match */
sublist.push(slice_drive(&state.string, last_pos, state.end, vm));
let list = PyList::from(sublist).into_object(vm);
let list = PyList::from(sublist).into_pyobject(vm);
let join_type: PyObjectRef = if zelf.isbytes {
vm.ctx.new_bytes(vec![]).into()
+2 -2
View File
@@ -143,7 +143,7 @@ mod symtable {
.symtable
.sub_tables
.iter()
.map(|t| to_py_symbol_table(t.clone()).into_object(vm))
.map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm))
.collect();
Ok(children)
}
@@ -231,7 +231,7 @@ mod symtable {
let namespaces = self
.namespaces
.iter()
.map(|table| to_py_symbol_table(table.clone()).into_object(vm))
.map(|table| to_py_symbol_table(table.clone()).into_pyobject(vm))
.collect();
Ok(namespaces)
}
+2 -2
View File
@@ -9,7 +9,7 @@ pub(crate) use _weakref::make_module;
#[pymodule]
mod _weakref {
use crate::builtins::{PyDictRef, PyTypeRef, PyWeak};
use crate::{PyObjectRef, PyObjectWrap, PyResult, VirtualMachine};
use crate::{PyObjectRef, PyRef, PyResult, VirtualMachine};
#[pyattr(name = "ref")]
fn ref_(vm: &VirtualMachine) -> PyTypeRef {
@@ -40,7 +40,7 @@ mod _weakref {
#[pyfunction]
fn getweakrefs(obj: PyObjectRef) -> Vec<PyObjectRef> {
match obj.get_weak_references() {
Some(v) => v.into_iter().map(|weak| weak.into_object()).collect(),
Some(v) => v.into_iter().map(|weak| PyRef::from(weak).into()).collect(),
None => vec![],
}
}
+6 -6
View File
@@ -1,7 +1,7 @@
use crate::{
builtins::{PyFloat, PyStr},
convert::{ToPyException, ToPyObject},
AsObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, TryFromObject, VirtualMachine,
AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
};
use num_traits::ToPrimitive;
use std::borrow::Borrow;
@@ -31,12 +31,12 @@ impl<A: AsRef<PyObject>, B: AsRef<PyObject>> AsRef<PyObject> for Either<A, B> {
}
}
impl<A: PyObjectWrap, B: PyObjectWrap> PyObjectWrap for Either<A, B> {
impl<A: Into<PyObjectRef>, B: Into<PyObjectRef>> From<Either<A, B>> for PyObjectRef {
#[inline(always)]
fn into_object(self) -> PyObjectRef {
match self {
Either::A(a) => a.into_object(),
Either::B(b) => b.into_object(),
fn from(value: Either<A, B>) -> Self {
match value {
Either::A(a) => a.into(),
Either::B(b) => b.into(),
}
}
}
+2 -2
View File
@@ -10,7 +10,7 @@ use crate::{
convert::ToPyObject,
scope::Scope,
vm::VirtualMachine,
AsObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyValue,
AsObject, PyObject, PyObjectRef, PyRef, PyValue,
};
/// Collection of object creation helpers
@@ -44,7 +44,7 @@ impl VirtualMachine {
.unwrap_or_else(|| self.ctx.none()),
self,
);
module.into_object()
module.into()
}
pub fn new_scope_with_builtins(&self) -> Scope {
+2 -2
View File
@@ -112,7 +112,7 @@ mod _browser {
JsFuture::from(response_format.get_response(&response)?).await
};
Ok(PyPromise::from_future(future).into_object(vm))
Ok(PyPromise::from_future(future).into_pyobject(vm))
}
#[pyfunction]
@@ -253,7 +253,7 @@ mod _browser {
})
};
Ok(PyPromise::from_future(future).into_object(vm))
Ok(PyPromise::from_future(future).into_pyobject(vm))
}
}
+6 -7
View File
@@ -14,12 +14,11 @@ mod _js {
use js_sys::{Array, Object, Promise, Reflect};
use rustpython_vm::{
builtins::{PyBaseExceptionRef, PyFloat, PyStrRef, PyTypeRef},
convert::ToPyObject,
convert::{IntoObject, ToPyObject},
function::{ArgCallable, OptionalArg, OptionalOption, PosArgs},
protocol::PyIterReturn,
types::{IterNext, IterNextIterable},
PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject,
VirtualMachine,
PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use std::{cell, fmt, future};
use wasm_bindgen::{closure::Closure, prelude::*, JsCast};
@@ -322,11 +321,11 @@ mod _js {
}
};
stored_vm_from_wasm(&wasm_vm).interp.enter(move |vm| {
let mut pyargs = vec![PyJsValue::new(this).into_object(vm)];
let mut pyargs = vec![PyJsValue::new(this).into_pyobject(vm)];
pyargs.extend(
Vec::from(args)
.into_iter()
.map(|arg| PyJsValue::new(arg).into_object(vm)),
.map(|arg| PyJsValue::new(arg).into_pyobject(vm)),
);
let res = vm.invoke(&py_obj, pyargs);
convert::pyresult_to_jsresult(vm, res)
@@ -524,8 +523,8 @@ mod _js {
vm.invoke(
then,
(
on_fulfill.map(|c| c.into_object()),
on_reject.map(|c| c.into_object()),
on_fulfill.map(IntoObject::into_object),
on_reject.map(IntoObject::into_object),
),
),
vm,