Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ mod array {
return Self::reduce(zelf, vm);
}
let array = zelf.read();
let cls = zelf.class().clone();
let cls = zelf.class().to_owned();
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();
Expand All @@ -1148,7 +1148,7 @@ mod array {
vm: &VirtualMachine,
) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
let array = zelf.read();
let cls = zelf.class().clone();
let cls = zelf.class().to_owned();
let typecode = vm.ctx.new_str(array.typecode_str());
let values = if array.typecode() == 'u' {
let s = Self::_wchar_bytes_to_string(array.get_bytes(), array.itemsize(), vm)?;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod math {
use crate::vm::{
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef, PyStrInterned},
function::{ArgIntoFloat, ArgIterable, Either, OptionalArg, PosArgs},
identifier, AsObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
identifier, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
};
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};
Expand Down
2 changes: 1 addition & 1 deletion vm/src/anystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
builtins::{PyIntRef, PyTupleRef},
cformat::CFormatString,
function::OptionalOption,
AsObject, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
};
use num_traits::{cast::ToPrimitive, sign::Signed};
use std::str::FromStr;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/builtinfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl GetDescriptor for PyBuiltinMethod {
Ok(obj) => obj,
Err(result) => return result,
};
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
zelf.into()
} else {
PyBoundMethod::new_ref(obj, zelf.into(), &vm.ctx).into()
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ impl PyByteArray {
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
let bytes = PyBytes::from(zelf.borrow_buf().to_vec()).to_pyobject(vm);
(
zelf.class().clone(),
zelf.class().to_owned(),
PyTuple::new_ref(vec![bytes], &vm.ctx),
zelf.as_object().dict(),
)
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ impl PyBytes {
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
let bytes = PyBytes::from(zelf.inner.elements.clone()).to_pyobject(vm);
(
zelf.class().clone(),
zelf.class().to_owned(),
PyTuple::new_ref(vec![bytes], &vm.ctx),
zelf.as_object().dict(),
)
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl GetDescriptor for PyClassMethod {
vm: &VirtualMachine,
) -> PyResult {
let (zelf, _obj) = Self::_unwrap(zelf, obj, vm)?;
let cls = cls.unwrap_or_else(|| _obj.class().clone().into());
let cls = cls.unwrap_or_else(|| _obj.class().to_owned().into());
let call_descr_get: PyResult<PyObjectRef> = zelf.callable.lock().get_attr("__get__", vm);
match call_descr_get {
Err(_) => Ok(PyBoundMethod::new_ref(cls, zelf.callable.lock().clone(), &vm.ctx).into()),
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl PyEnumerate {
#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>) -> (PyTypeRef, (PyIter, BigInt)) {
(
zelf.class().clone(),
zelf.class().to_owned(),
(zelf.iterator.clone(), zelf.counter.read().clone()),
)
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl GetDescriptor for PyFunction {
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
zelf.into()
} else {
PyBoundMethod::new_ref(obj, zelf.into(), &vm.ctx).into()
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl PyBaseObject {

#[pygetset(name = "__class__")]
fn get_class(obj: PyObjectRef) -> PyTypeRef {
obj.class().clone()
obj.class().to_owned()
}

#[pygetset(name = "__class__", setter)]
Expand All @@ -270,7 +270,7 @@ impl PyBaseObject {
match value.downcast::<PyType>() {
Ok(cls) => {
// FIXME(#1979) cls instances might have a payload
*instance.class_lock().write() = cls;
instance.set_class(cls, vm);
Ok(())
}
Err(value) => {
Expand Down Expand Up @@ -311,7 +311,7 @@ impl PyBaseObject {
let __reduce__ = identifier!(vm, __reduce__);
if let Some(reduce) = vm.get_attribute_opt(obj.clone(), __reduce__)? {
let object_reduce = vm.ctx.types.object_type.get_attr(__reduce__).unwrap();
let typ_obj: PyObjectRef = obj.class().clone().into();
let typ_obj: PyObjectRef = obj.class().to_owned().into();
let class_reduce = typ_obj.get_attr(__reduce__, vm)?;
if !class_reduce.is(&object_reduce) {
return vm.invoke(&reduce, ());
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl PyProperty {
deleter: PyRwLock::new(zelf.fdel()),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, zelf.class().clone())
.into_ref_with_type(vm, zelf.class().to_owned())
}

#[pymethod]
Expand All @@ -183,7 +183,7 @@ impl PyProperty {
deleter: PyRwLock::new(zelf.fdel()),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, zelf.class().clone())
.into_ref_with_type(vm, zelf.class().to_owned())
}

#[pymethod]
Expand All @@ -198,7 +198,7 @@ impl PyProperty {
deleter: PyRwLock::new(deleter.or_else(|| zelf.fdel())),
doc: PyRwLock::new(None),
}
.into_ref_with_type(vm, zelf.class().clone())
.into_ref_with_type(vm, zelf.class().to_owned())
}

#[pygetset(magic)]
Expand Down
4 changes: 1 addition & 3 deletions vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ fn reduce_set(
vm: &VirtualMachine,
) -> PyResult<(PyTypeRef, PyTupleRef, Option<PyDictRef>)> {
Ok((
zelf.class().clone(),
zelf.class().to_owned(),
vm.new_tuple((extract_set(zelf)
.unwrap_or(&PySetInner::default())
.elements(),)),
Expand Down Expand Up @@ -1056,8 +1056,6 @@ impl TryFromObject for AnySet {
if class.fast_issubclass(vm.ctx.types.set_type)
|| class.fast_issubclass(vm.ctx.types.frozenset_type)
{
// the class lease needs to be drop to be able to return the object
drop(class);
Ok(AnySet { object: obj })
} else {
Err(vm.new_type_error(format!("{} is not a subtype of set or frozenset", class)))
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl PySlice {
(Option<PyObjectRef>, PyObjectRef, Option<PyObjectRef>),
)> {
Ok((
zelf.class().clone(),
zelf.class().to_owned(),
(zelf.start.clone(), zelf.stop.clone(), zelf.step.clone()),
))
}
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl GetDescriptor for PySuper {
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))
vm.invoke(zelf.class(), (zelf.typ.clone(), obj))
}
}
}
Expand All @@ -200,7 +200,7 @@ fn supercheck(ty: PyTypeRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<
}
}
if obj.fast_isinstance(&ty) {
return Ok(obj.class().clone());
return Ok(obj.class().to_owned());
}
let class_attr = obj.get_attr("__class__", vm)?;
if let Ok(cls) = class_attr.downcast::<PyType>() {
Expand Down
39 changes: 18 additions & 21 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl PyType {

let mros = bases
.iter()
.map(|x| x.iter_mro().cloned().collect())
.map(|x| x.iter_mro().map(|x| x.to_owned()).collect())
.collect();
let mro = linearise_mro(mros)?;

Expand Down Expand Up @@ -247,7 +247,7 @@ impl PyType {
*slots.name.get_mut() = Some(String::from(name));

let bases = vec![base.clone()];
let mro = base.iter_mro().cloned().collect();
let mro = base.iter_mro().map(|x| x.to_owned()).collect();

let new_type = PyRef::new_ref(
PyType {
Expand Down Expand Up @@ -353,20 +353,20 @@ impl PyType {
}
}

impl PyTypeRef {
impl Py<PyType> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea.

/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
/// method.
pub fn fast_issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
self.as_object().is(cls.borrow()) || self.mro.iter().any(|c| c.is(cls.borrow()))
}

pub fn iter_mro(&self) -> impl Iterator<Item = &PyTypeRef> + DoubleEndedIterator {
std::iter::once(self).chain(self.mro.iter())
pub fn iter_mro(&self) -> impl Iterator<Item = &Py<PyType>> + DoubleEndedIterator {
std::iter::once(self).chain(self.mro.iter().map(|x| x.deref()))
}

pub fn iter_base_chain(&self) -> impl Iterator<Item = &PyTypeRef> {
std::iter::successors(Some(self), |cls| cls.base.as_ref())
pub fn iter_base_chain(&self) -> impl Iterator<Item = &Py<PyType>> {
std::iter::successors(Some(self), |cls| cls.base.as_deref())
}
}

Expand Down Expand Up @@ -569,7 +569,7 @@ impl PyType {

#[pymethod]
fn mro(zelf: PyRef<Self>) -> Vec<PyObjectRef> {
zelf.iter_mro().map(|cls| cls.clone().into()).collect()
zelf.iter_mro().map(|cls| cls.to_owned().into()).collect()
}

#[pymethod(magic)]
Expand All @@ -588,7 +588,7 @@ impl PyType {

let is_type_type = metatype.is(vm.ctx.types.type_type);
if is_type_type && args.args.len() == 1 && args.kwargs.is_empty() {
return Ok(args.args[0].class().clone().into());
return Ok(args.args[0].class().to_owned().into());
}

if args.args.len() != 3 {
Expand Down Expand Up @@ -921,7 +921,7 @@ impl GetAttr for PyType {
if has_descr_set {
let descr_get = attr_class.mro_find_map(|cls| cls.slots.descr_get.load());
if let Some(descr_get) = descr_get {
let mcl = mcl.into_owned().into();
let mcl = mcl.to_owned().into();
return descr_get(attr.clone(), Some(zelf.to_owned().into()), Some(mcl), vm);
}
}
Expand All @@ -932,15 +932,13 @@ impl GetAttr for PyType {
if let Some(ref attr) = zelf_attr {
let descr_get = attr.class().mro_find_map(|cls| cls.slots.descr_get.load());
if let Some(descr_get) = descr_get {
drop(mcl);
return descr_get(attr.clone(), None, Some(zelf.to_owned().into()), vm);
}
}

if let Some(cls_attr) = zelf_attr {
Ok(cls_attr)
} else if let Some(attr) = mcl_attr {
drop(mcl);
vm.call_if_get_descriptor(attr, zelf.to_owned().into())
} else {
return Err(attribute_error(zelf, name_str.as_str(), vm));
Expand Down Expand Up @@ -1007,7 +1005,7 @@ impl Callable for PyType {
}
}

fn find_base_dict_descr(cls: &PyTypeRef, vm: &VirtualMachine) -> Option<PyObjectRef> {
fn find_base_dict_descr(cls: &Py<PyType>, vm: &VirtualMachine) -> Option<PyObjectRef> {
cls.iter_base_chain().skip(1).find_map(|cls| {
// TODO: should actually be some translation of:
// cls.slot_dictoffset != 0 && !cls.flags.contains(HEAPTYPE)
Expand All @@ -1021,12 +1019,11 @@ fn find_base_dict_descr(cls: &PyTypeRef, vm: &VirtualMachine) -> Option<PyObject

fn subtype_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
// TODO: obj.class().as_pyref() need to be supported
let cls = obj.class().clone();
let ret = match find_base_dict_descr(&cls, vm) {
Some(descr) => vm.call_get_descriptor(descr, obj).unwrap_or_else(|_| {
let ret = match find_base_dict_descr(obj.class(), vm) {
Some(descr) => vm.call_get_descriptor(descr, obj).unwrap_or_else(|obj| {
Err(vm.new_type_error(format!(
"this __dict__ descriptor does not support '{}' objects",
cls.name()
obj.class()
)))
})?,
None => object::object_get_dict(obj, vm)?.into(),
Expand All @@ -1035,8 +1032,8 @@ fn subtype_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
}

fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let cls = obj.class().clone();
match find_base_dict_descr(&cls, vm) {
let cls = obj.class();
match find_base_dict_descr(cls, vm) {
Some(descr) => {
let descr_set = descr
.class()
Expand Down Expand Up @@ -1152,10 +1149,10 @@ fn calculate_meta_class(
let mut winner = metatype;
for base in bases {
let base_type = base.class();
if winner.fast_issubclass(&base_type) {
if winner.fast_issubclass(base_type) {
continue;
} else if base_type.fast_issubclass(&winner) {
winner = base_type.into_owned();
winner = base_type.to_owned();
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Constructor for PyZip {
impl PyZip {
#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
let cls = zelf.class().clone();
let cls = zelf.class().to_owned();
let iterators = zelf
.iterators
.iter()
Expand Down
8 changes: 4 additions & 4 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl VirtualMachine {
exc: PyBaseExceptionRef,
) -> (PyObjectRef, PyObjectRef, PyObjectRef) {
let tb = exc.traceback().to_pyobject(self);
let class = exc.class().clone();
let class = exc.class().to_owned();
(class.into(), exc.into(), tb)
}

Expand Down Expand Up @@ -524,9 +524,9 @@ impl PyBaseException {
#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyTupleRef {
if let Some(dict) = zelf.as_object().dict().filter(|x| !x.is_empty()) {
return vm.new_tuple((zelf.class().clone(), zelf.args(), dict));
return vm.new_tuple((zelf.class().to_owned(), zelf.args(), dict));
} else {
return vm.new_tuple((zelf.class().clone(), zelf.args()));
return vm.new_tuple((zelf.class().to_owned(), zelf.args()));
}
}
}
Expand Down Expand Up @@ -912,7 +912,7 @@ fn os_error_str(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyResult<PyStrR
fn os_error_reduce(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyTupleRef {
let args = exc.args();
let obj = exc.as_object().to_owned();
let mut result: Vec<PyObjectRef> = vec![obj.class().clone().into()];
let mut result: Vec<PyObjectRef> = vec![obj.class().to_owned().into()];

if args.len() >= 2 && args.len() <= 5 {
// SAFETY: len() == 2 is checked so get_arg 1 or 2 won't panic
Expand Down
2 changes: 2 additions & 0 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct Frame {

// member
pub trace_lines: PyMutex<bool>,
pub temporary_refs: PyMutex<Vec<PyObjectRef>>,
}

impl PyPayload for Frame {
Expand Down Expand Up @@ -162,6 +163,7 @@ impl Frame {
state: PyMutex::new(state),
trace: PyMutex::new(vm.ctx.none()),
trace_lines: PyMutex::new(true),
temporary_refs: PyMutex::new(vec![]),
}
}
}
Expand Down
Loading