Introduce AsPyObject to replace IdProtocol

This commit is contained in:
Jeong Yunwon
2022-04-17 08:20:28 +09:00
parent 9be1dc1f8b
commit 625f8675ae
68 changed files with 141 additions and 161 deletions

View File

@@ -51,8 +51,8 @@ use rustpython_vm::{
compile, match_class,
scope::Scope,
stdlib::{atexit, sys},
InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, TypeProtocol,
VirtualMachine,
AsPyObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject,
TypeProtocol, VirtualMachine,
};
use std::{env, path::Path, process, str::FromStr};
@@ -536,12 +536,8 @@ fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
main_module
.dict()
.and_then(|d| {
d.set_item(
"__annotations__",
vm.ctx.new_dict().as_object().to_owned(),
vm,
)
.ok()
d.set_item("__annotations__", vm.ctx.new_dict().into(), vm)
.ok()
})
.expect("Failed to initialize __main__.__annotations__");

View File

@@ -85,9 +85,8 @@ impl<'vm> ShellHelper<'vm> {
} else {
// we need to get a variable based off of globals/builtins
let globals = str_iter_method(self.globals.as_object().to_owned(), "keys").ok()?;
let builtins =
str_iter_method(self.vm.builtins.as_object().to_owned(), "__dir__").ok()?;
let globals = str_iter_method(self.globals.clone().into(), "keys").ok()?;
let builtins = str_iter_method(self.vm.builtins.clone().into(), "__dir__").ok()?;
(first, globals, Some(builtins))
};
Some((word_start, iter1.chain(iter2.into_iter().flatten())))

View File

@@ -33,14 +33,13 @@ mod array {
AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable,
PyComparisonOp,
},
IdProtocol, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
},
};
use itertools::Itertools;
use num_traits::ToPrimitive;
use std::cmp::Ordering;
use std::{fmt, os::raw};
use std::{cmp::Ordering, fmt, os::raw};
macro_rules! def_array_enum {
($(($n:ident, $t:ty, $c:literal, $scode:literal)),*$(,)?) => {

View File

@@ -9,7 +9,7 @@ mod _json {
function::{IntoPyObject, IntoPyResult, OptionalArg},
protocol::PyIterReturn,
types::{Callable, Constructor},
IdProtocol, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine,
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine,
};
use num_bigint::BigInt;
use std::str::FromStr;

View File

@@ -7,7 +7,7 @@ use crate::{
protocol::PyIterReturn,
pyclass::PyClassImpl,
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -33,7 +33,7 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
utils::Either,
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
};
use bstr::ByteSlice;

View File

@@ -22,7 +22,7 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
},
utils::Either,
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
};
use bstr::ByteSlice;

View File

@@ -7,7 +7,7 @@ use crate::{
bytecode::{self, BorrowedConstant, Constant, ConstantBag},
function::FuncArgs,
pyclass::{PyClassImpl, StaticType},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use num_traits::Zero;

View File

@@ -7,7 +7,7 @@ use crate::{
},
pyclass::PyClassImpl,
types::{Comparable, Constructor, Hashable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use num_complex::Complex64;

View File

@@ -6,7 +6,7 @@ use crate::{
protocol::PyIterReturn,
pyclass::PyClassImpl,
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
#[pyclass(module = false, name = "coroutine")]

View File

@@ -21,7 +21,7 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
vm::{ReprGuard, VirtualMachine},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol,
};
use rustpython_common::lock::PyMutex;

View File

@@ -9,7 +9,7 @@ use crate::{
},
pyclass::PyClassImpl,
types::{Comparable, Constructor, Hashable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject,
TryFromObject, TypeProtocol, VirtualMachine,
};
use num_bigint::{BigInt, ToBigInt};

View File

@@ -7,7 +7,7 @@ use crate::{
frame::{Frame, FrameRef},
pyclass::PyClassImpl,
types::{Constructor, Unconstructible},
IdProtocol, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyObjectWrap, PyRef, PyResult, VirtualMachine,
};
pub fn init(context: &PyContext) {

View File

@@ -14,7 +14,7 @@ use crate::{
pyclass::PyClassImpl,
scope::Scope,
types::{Callable, Comparable, Constructor, GetAttr, GetDescriptor, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
#[cfg(feature = "jit")]

View File

@@ -2,7 +2,7 @@ use crate::{
builtins::{float, int, pybool, PyBaseExceptionRef, PyDictRef, PyFunction, PyStrRef},
bytecode::CodeFlags,
function::{FuncArgs, IntoPyObject},
IdProtocol, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine,
};
use num_traits::ToPrimitive;
use rustpython_jit::{AbiValue, Args, CompiledCode, JitArgumentError, JitType};

View File

@@ -10,7 +10,7 @@ use crate::{
protocol::PyIterReturn,
pyclass::PyClassImpl,
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
#[pyclass(module = false, name = "generator")]

View File

@@ -5,7 +5,7 @@ use crate::{
protocol::PyMappingMethods,
pyclass::PyClassImpl,
types::{AsMapping, Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol, VirtualMachine,
};
use std::fmt;

View File

@@ -9,7 +9,7 @@ use crate::{
},
pyclass::PyClassImpl,
types::{Comparable, Constructor, Hashable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromBorrowedObject,
TypeProtocol, VirtualMachine,
};
use bstr::ByteSlice;

View File

@@ -15,8 +15,8 @@ use crate::{
},
utils::collection_repr,
vm::{ReprGuard, VirtualMachine},
PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue,
TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TypeProtocol,
};
use std::{borrow::Cow, fmt, ops::DerefMut};

View File

@@ -18,7 +18,7 @@ use crate::{
sliceable::wrap_index,
types::{AsBuffer, AsMapping, AsSequence, Comparable, Constructor, Hashable, PyComparisonOp},
utils::Either,
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -4,7 +4,7 @@ use crate::{
function::{FuncArgs, IntoPyObject},
pyclass::PyClassImpl,
types::GetAttr,
PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, VirtualMachine,
};
#[pyclass(module = false, name = "module")]

View File

@@ -5,7 +5,7 @@ use crate::{
pyclass::PyClassImpl,
types::{Comparable, Constructor, PyComparisonOp},
vm::ReprGuard,
IdProtocol, PyContext, PyObject, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObject, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
/// A simple attribute-based namespace.

View File

@@ -5,7 +5,7 @@ use crate::{
pyclass::PyClassImpl,
types::PyComparisonOp,
utils::Either,
IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
/// object()

View File

@@ -3,7 +3,7 @@ use crate::{
function::{IntoPyObject, OptionalArg},
pyclass::PyClassImpl,
types::Constructor,
IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject,
AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject,
TypeProtocol, VirtualMachine,
};
use num_bigint::Sign;

View File

@@ -18,7 +18,7 @@ use crate::{
AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable,
Iterable, PyComparisonOp, Unconstructible,
},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TypeProtocol, VirtualMachine,
};
use ascii::{AsciiStr, AsciiString};

View File

@@ -8,7 +8,7 @@ use crate::{
function::OptionalArg,
pyclass::PyClassImpl,
types::{Constructor, GetAttr, GetDescriptor},
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
#[pyclass(module = false, name = "super")]

View File

@@ -12,7 +12,7 @@ use crate::{
pyclass::{PyClassImpl, StaticType},
pyobject::PyLease,
types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr},
IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol,
AsPyObject, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use itertools::Itertools;

View File

@@ -6,7 +6,7 @@ use crate::{
protocol::PyMappingMethods,
pyclass::PyClassImpl,
types::{AsMapping, Comparable, GetAttr, Hashable, Iterable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol, VirtualMachine,
};
use std::fmt;

View File

@@ -9,7 +9,7 @@ use crate::{
AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable,
Iterable, PyComparisonOp, Unconstructible,
},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -17,7 +17,7 @@ use crate::{
},
utils::collection_repr,
vm::{ReprGuard, VirtualMachine},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol,
};
use std::borrow::Cow;

View File

@@ -4,7 +4,8 @@ use crate::{
function::{FuncArgs, IntoPyObject, OptionalArg, PyComparisonValue},
pyclass::PyClassImpl,
types::{Comparable, Constructor, Hashable, PyComparisonOp, Unhashable},
PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use num_bigint::{BigInt, ToBigInt};
use num_traits::{One, Signed, ToPrimitive, Zero};

View File

@@ -14,7 +14,7 @@ use crate::{
},
utils::collection_repr,
vm::{ReprGuard, VirtualMachine},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol,
};
use std::{borrow::Cow, fmt, marker::PhantomData};

View File

@@ -7,8 +7,8 @@ use crate::{
function::OptionalArg,
pyclass::PyClassImpl,
types::{Callable, Comparable, Constructor, Hashable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
TypeProtocol, VirtualMachine,
};
pub use crate::pyobject::PyWeak;

View File

@@ -9,7 +9,7 @@ use crate::{
sequence::{SequenceMutOp, SequenceOp},
types::PyComparisonOp,
utils::Either,
IdProtocol, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, VirtualMachine,
};
use bstr::ByteSlice;
use itertools::Itertools;

View File

@@ -2,7 +2,7 @@ use crate::{
builtins::{PyBaseExceptionRef, PyBytesRef, PyStr, PyStrRef, PyTuple, PyTupleRef},
common::{ascii, lock::PyRwLock},
function::IntoPyObject,
IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
VirtualMachine,
};
use std::{borrow::Cow, collections::HashMap, fmt::Write, ops::Range};

View File

@@ -3,7 +3,7 @@ use crate::{
common::lock::PyMutex,
frame::{ExecutionResult, FrameRef},
protocol::PyIterReturn,
IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -10,7 +10,7 @@ use crate::common::{
use crate::{
builtins::{PyInt, PyStr, PyStrRef},
function::IntoPyObject,
IdProtocol, PyObject, PyObjectRef, PyObjectWrap, PyRefExact, PyResult, TypeProtocol,
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRefExact, PyResult, TypeProtocol,
VirtualMachine,
};
use num_traits::ToPrimitive;

View File

@@ -9,7 +9,7 @@ use crate::{
pyclass::{PyClassImpl, StaticType},
stdlib::sys,
suggestion::offer_suggestions,
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -15,7 +15,7 @@ use crate::{
scope::Scope,
stdlib::builtins,
types::PyComparisonOp,
IdProtocol, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
AsPyObject, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol, VirtualMachine,
};
use indexmap::IndexMap;

View File

@@ -1,7 +1,7 @@
use crate::{
convert::TryFromObject,
function::IntoPyObject,
pyobject::{IdProtocol, PyObjectRef, PyResult},
pyobject::{AsPyObject, PyObjectRef, PyResult},
VirtualMachine,
};

View File

@@ -127,7 +127,7 @@ pub fn import_codeobj(
let attrs = vm.ctx.new_dict();
attrs.set_item("__name__", vm.ctx.new_str(module_name).into(), vm)?;
if set_file_attr {
attrs.set_item("__file__", code_obj.source_path.as_object().to_owned(), vm)?;
attrs.set_item("__file__", code_obj.source_path.clone().into(), vm)?;
}
let module = vm.new_module(module_name, attrs.clone(), None);

View File

@@ -89,12 +89,12 @@ mod pyobject {
pub use self::convert::{TryFromBorrowedObject, TryFromObject};
// pyobject items
pub use self::pyobject::{
IdProtocol, PyContext, PyMethod, PyObjectPayload, PyRefExact, PyResult, PyStructSequence,
PyContext, PyMethod, PyObjectPayload, PyObjectWrap, PyRefExact, PyResult, PyStructSequence,
PyValue, TypeProtocol,
};
// pyobjectrc items
pub use self::pyobject::{
PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef,
AsPyObject, PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyRef, PyWeakRef,
};
pub use self::vm::{InitParameter, Interpreter, PySettings, VirtualMachine};
pub use rustpython_bytecode as bytecode;

View File

@@ -49,7 +49,7 @@ macro_rules! py_namespace {
{
let namespace = $crate::builtins::PyNamespace::new_ref(&$vm.ctx);
$(
$vm.__module_set_attr(namespace.as_object(), $name, $value).unwrap();
$vm.__module_set_attr($crate::pyobject::AsPyObject::as_object(&namespace), $name, $value).unwrap();
)*
namespace
}

View File

@@ -1,8 +1,8 @@
use crate::{
builtins::iter::PySequenceIterator,
function::{IntoPyObject, IntoPyResult},
PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject, TypeProtocol,
VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TryFromObject,
TypeProtocol, VirtualMachine,
};
use std::borrow::Borrow;
use std::ops::Deref;

View File

@@ -5,7 +5,7 @@ use crate::{
},
common::lock::OnceCell,
function::IntoPyResult,
IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, TypeProtocol, VirtualMachine,
};
// Mapping protocol

View File

@@ -13,7 +13,7 @@ use crate::{
protocol::{PyIter, PyMapping, PySequence},
types::{Constructor, PyComparisonOp},
utils::Either,
IdProtocol, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine,
};
// RustPython doesn't need these items

View File

@@ -3,7 +3,7 @@ use crate::{
common::lock::OnceCell,
function::{IntoPyObject, PyArithmeticValue},
protocol::PyMapping,
IdProtocol, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
use itertools::Itertools;
use std::{

View File

@@ -1,6 +1,3 @@
pub use crate::_pyobjectrc::{
PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef,
};
use crate::common::lock::PyRwLockReadGuard;
use crate::{
builtins::{
@@ -20,6 +17,7 @@ use crate::{
pyclass::{PyClassImpl, StaticType},
types::{PyTypeFlags, PyTypeSlots, TypeZoo},
VirtualMachine,
_pyobjectrc::{PyObject, PyObjectRef, PyObjectView, PyRef},
};
use num_bigint::BigInt;
use num_traits::ToPrimitive;
@@ -389,46 +387,42 @@ impl<T: PyValue> IntoPyObject for PyRefExact<T> {
}
}
pub trait IdProtocol {
fn get_id(&self) -> usize;
pub trait AsPyObject
where
Self: Borrow<PyObject>,
{
#[inline(always)]
fn as_object(&self) -> &PyObject {
self.borrow()
}
#[inline(always)]
fn get_id(&self) -> usize {
self.as_object()._get_id()
}
#[inline(always)]
fn is<T>(&self, other: &T) -> bool
where
T: IdProtocol,
T: AsPyObject,
{
self.get_id() == other.get_id()
}
}
// impl<T: ?Sized> IdProtocol for PyRc<T> {
// fn get_id(&self) -> usize {
// &**self as *const T as *const () as usize
impl<T> AsPyObject for T where T: Borrow<PyObject> {}
impl PyObject {
#[inline]
fn _get_id(&self) -> usize {
self as *const PyObject as usize
}
}
// impl<T: ?Sized> Borrow<PyObject> for PyRc<T> {
// fn borrow(&self) -> &PyObject {
// unsafe { &*(&**self as *const T as *const PyObject) }
// }
// }
impl<T: PyObjectPayload> IdProtocol for PyRef<T> {
fn get_id(&self) -> usize {
self.as_object().get_id()
}
}
impl<T: PyObjectPayload> IdProtocol for PyObjectView<T> {
fn get_id(&self) -> usize {
self.as_object().get_id()
}
}
impl<'a, T: PyObjectPayload> IdProtocol for PyLease<'a, T> {
fn get_id(&self) -> usize {
self.inner.get_id()
}
}
impl<T: IdProtocol> IdProtocol for &'_ T {
fn get_id(&self) -> usize {
(&**self).get_id()
}
}
/// A borrow of a reference to a Python object. This avoids having clone the `PyRef<T>`/
/// `PyObjectRef`, which isn't that cheap as that increments the atomic reference counter.
pub struct PyLease<'a, T: PyObjectPayload> {
@@ -705,6 +699,22 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static {
}
}
pub trait PyObjectWrap
where
Self: AsPyObject,
{
fn into_object(self) -> PyObjectRef;
}
impl<T> From<T> for PyObjectRef
where
T: PyObjectWrap,
{
fn from(py_ref: T) -> Self {
py_ref.into_object()
}
}
#[derive(Debug)]
pub enum PyMethod {
Function {

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::{IdProtocol, PyObjectPayload, PyResult, TypeProtocol},
_pyobject::{AsPyObject, PyObjectPayload, PyObjectWrap, PyResult, TypeProtocol},
builtins::{PyBaseExceptionRef, PyDictRef, PyTypeRef},
vm::VirtualMachine,
};
@@ -464,18 +464,6 @@ impl ToOwned for PyObject {
}
}
pub trait PyObjectWrap
where
Self: Borrow<PyObject>,
{
#[inline(always)]
fn as_object(&self) -> &PyObject {
self.borrow()
}
fn into_object(self) -> PyObjectRef;
}
impl PyObjectRef {
pub fn into_raw(self) -> *const PyObject {
let ptr = self.as_raw();
@@ -770,30 +758,22 @@ impl AsRef<PyObject> for PyObject {
}
}
impl IdProtocol for PyObjectRef {
fn get_id(&self) -> usize {
PyObject::get_id(self)
}
}
impl IdProtocol for PyObject {
fn get_id(&self) -> usize {
self as *const PyObject as usize
}
}
impl<'a, T: PyObjectPayload> From<&'a PyObjectView<T>> for &'a PyObject {
fn from(py_ref: &'a PyObjectView<T>) -> Self {
py_ref.as_object()
}
}
impl<T> From<T> for PyObjectRef
where
T: PyObjectWrap,
{
fn from(py_ref: T) -> Self {
py_ref.into_object()
impl Borrow<PyObject> for PyObjectWeak {
#[inline]
fn borrow(&self) -> &PyObject {
self.weak.as_object()
}
}
impl PyObjectWrap for PyObjectWeak {
fn into_object(self) -> PyObjectRef {
self.weak.into_object()
}
}
@@ -802,10 +782,6 @@ impl PyObjectWeak {
pub fn upgrade(&self) -> Option<PyObjectRef> {
self.weak.upgrade()
}
pub fn into_object(self) -> PyObjectRef {
self.weak.into_object()
}
}
impl Drop for PyObjectRef {
@@ -854,11 +830,6 @@ impl fmt::Debug for PyObjectWeak {
pub struct PyObjectView<T: PyObjectPayload>(PyInner<T>);
impl<T: PyObjectPayload> PyObjectView<T> {
#[inline(always)]
pub fn as_object(&self) -> &PyObject {
unsafe { &*(&self.0 as *const PyInner<T> as *const PyObject) }
}
pub fn downgrade(
&self,
callback: Option<PyObjectRef>,
@@ -886,14 +857,16 @@ impl<T: PyObjectPayload> ToOwned for PyObjectView<T> {
impl<T: PyObjectPayload> Deref for PyObjectView<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0.payload
}
}
impl<T: PyObjectPayload> Borrow<PyObject> for PyObjectView<T> {
#[inline(always)]
fn borrow(&self) -> &PyObject {
self.as_object()
unsafe { &*(&self.0 as *const PyInner<T> as *const PyObject) }
}
}
@@ -901,8 +874,9 @@ impl<T> AsRef<PyObject> for PyObjectView<T>
where
T: PyObjectPayload,
{
#[inline(always)]
fn as_ref(&self) -> &PyObject {
self.as_object()
self.borrow()
}
}

View File

@@ -3,7 +3,7 @@ use crate::{
types::{richcompare_wrapper, PyComparisonOp, RichCompareFunc},
utils::Either,
vm::VirtualMachine,
IdProtocol, PyObject, PyObjectRef, PyResult, TypeProtocol,
AsPyObject, PyObject, PyObjectRef, PyResult, TypeProtocol,
};
use itertools::Itertools;
use optional::Optioned;

View File

@@ -8,7 +8,7 @@ mod gen;
use crate::{
builtins::{self, PyStrRef, PyTypeRef},
pyclass::{PyClassImpl, StaticType},
IdProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
VirtualMachine,
};
use num_complex::Complex64;

View File

@@ -33,7 +33,7 @@ mod builtins {
stdlib::sys,
types::PyComparisonOp,
utils::Either,
IdProtocol, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject,
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol, VirtualMachine,
};
use num_traits::{Signed, ToPrimitive, Zero};

View File

@@ -7,7 +7,7 @@ mod _codecs {
builtins::{PyBaseExceptionRef, PyBytes, PyBytesRef, PyStr, PyStrRef, PyTuple},
codecs,
function::{ArgBytesLike, FuncArgs},
IdProtocol, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine,
};
use std::ops::Range;

View File

@@ -19,7 +19,7 @@ mod _collections {
},
utils::collection_repr,
vm::ReprGuard,
PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;
use std::cmp::max;

View File

@@ -88,7 +88,7 @@ mod _io {
types::{Constructor, Destructor, IterNext, Iterable},
utils::Either,
vm::{ReprGuard, VirtualMachine},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, TypeProtocol,
};
use bstr::ByteSlice;
@@ -3661,7 +3661,8 @@ mod fileio {
ArgBytesLike, ArgMemoryBuffer, FuncArgs, IntoPyException, OptionalArg, OptionalOption,
},
stdlib::os,
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;
use std::io::{Read, Write};

View File

@@ -12,7 +12,7 @@ mod decl {
protocol::{PyIter, PyIterReturn},
stdlib::sys,
types::{Constructor, IterNext, IterNextIterable},
IdProtocol, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, PyWeakRef, TypeProtocol,
AsPyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, PyWeakRef, TypeProtocol,
VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -10,7 +10,7 @@ mod decl {
bytecode,
function::{ArgBytesLike, IntoPyObject},
protocol::PyBuffer,
pyobject::{IdProtocol, TypeProtocol},
pyobject::{AsPyObject, TypeProtocol},
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
};
/// TODO

View File

@@ -20,7 +20,7 @@ mod _operator {
},
utils::Either,
vm::ReprGuard,
IdProtocol, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol,
AsPyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};

View File

@@ -3,8 +3,8 @@ use crate::{
crt_fd::Fd,
function::{ArgumentError, FromArgs, FuncArgs, IntoPyException, IntoPyObject},
protocol::PyBuffer,
PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol,
VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject,
TypeProtocol, VirtualMachine,
};
use std::{
ffi, fs,

View File

@@ -5,7 +5,7 @@ mod pwd {
use crate::{
builtins::{PyIntRef, PyStrRef},
function::{IntoPyException, IntoPyObject},
PyObjectRef, PyResult, PyStructSequence, VirtualMachine,
AsPyObject, PyObjectRef, PyResult, PyStructSequence, VirtualMachine,
};
use nix::unistd::{self, User};
use std::ptr::NonNull;

View File

@@ -10,7 +10,7 @@ pub(crate) mod _thread {
py_io,
types::{Constructor, GetAttr, SetAttr},
utils::Either,
IdProtocol, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
use parking_lot::{
lock_api::{RawMutex as RawMutexT, RawMutexTimed, RawReentrantMutex},

View File

@@ -9,7 +9,7 @@ pub(crate) use _weakref::make_module;
#[pymodule]
mod _weakref {
use crate::builtins::{PyDictRef, PyTypeRef, PyWeak};
use crate::{PyObjectRef, PyResult, VirtualMachine};
use crate::{PyObjectRef, PyObjectWrap, PyResult, VirtualMachine};
#[pyattr(name = "ref")]
fn ref_(vm: &VirtualMachine) -> PyTypeRef {

View File

@@ -2,7 +2,7 @@ use crate::{
builtins::{PyStr, PyStrRef},
exceptions::types::PyBaseExceptionRef,
sliceable::SliceableSequenceOp,
IdProtocol, PyObjectRef, PyObjectView, TypeProtocol, VirtualMachine,
AsPyObject, PyObjectRef, PyObjectView, TypeProtocol, VirtualMachine,
};
use rustpython_common::str::levenshtein::{levenshtein_distance, MOVE_COST};
use std::iter::ExactSizeIterator;

View File

@@ -6,7 +6,7 @@ use crate::{
protocol::{PyBuffer, PyIterReturn, PyMapping, PyMappingMethods},
protocol::{PySequence, PySequenceMethods},
utils::Either,
IdProtocol, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol,
AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

View File

@@ -24,8 +24,8 @@ use crate::{
protocol::PyIterIter,
pyobject::PyLease,
scope::Scope,
signal, stdlib, IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWrap, PyRef, PyRefExact,
PyResult, PyValue, TypeProtocol,
signal, stdlib, AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyRefExact, PyResult,
PyValue, TypeProtocol,
};
use crossbeam_utils::atomic::AtomicCell;
use std::{
@@ -872,7 +872,7 @@ impl VirtualMachine {
if !context_exc.is(exception) {
let mut o = context_exc.clone();
while let Some(context) = o.context() {
if context.is(&exception) {
if context.is(exception) {
o.set_context(None);
break;
}

View File

@@ -10,7 +10,7 @@ use crate::{
function::IntoPyObject,
scope::Scope,
vm::VirtualMachine,
PyObject, PyObjectRef, PyObjectWrap, PyRef, TypeProtocol,
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRef, TypeProtocol,
};
/// Collection of object creation helpers

View File

@@ -2,7 +2,7 @@ use crate::{
builtins::{PyBaseExceptionRef, PyList, PyStr},
function::{FuncArgs, IntoFuncArgs},
vm::VirtualMachine,
IdProtocol, PyMethod, PyObject, PyObjectRef, PyObjectWrap, PyResult, PyValue, TypeProtocol,
AsPyObject, PyMethod, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
};
/// Trace events for sys.settrace and sys.setprofile.

View File

@@ -4,7 +4,7 @@ use crate::{
protocol::PyIterReturn,
types::PyComparisonOp,
vm::VirtualMachine,
IdProtocol, PyMethod, PyObject, PyObjectRef, PyResult, TypeProtocol,
AsPyObject, PyMethod, PyObject, PyObjectRef, PyResult, TypeProtocol,
};
/// Collection of operators