Skip to content

descriptors and method calls to take &PyObject #4737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
class::PyClassImpl,
function::{FuncArgs, IntoPyNativeFunc, PyNativeFunc},
types::{Callable, Constructor, GetDescriptor, Representable, Unconstructible},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};
use std::fmt;

Expand Down Expand Up @@ -189,19 +189,19 @@ impl fmt::Debug for PyBuiltinMethod {

impl GetDescriptor for PyBuiltinMethod {
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (_zelf, obj) = match Self::_check(&zelf, obj, vm) {
Some(obj) => obj,
None => return Ok(zelf),
None => return Ok(zelf.to_owned()),
};
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
zelf
zelf.to_owned().into()
} else {
PyBoundMethod::new_ref(obj, zelf, &vm.ctx).into()
PyBoundMethod::new_ref(obj, zelf.to_owned().into(), &vm.ctx).into()
};
Ok(r)
}
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
class::PyClassImpl,
common::lock::PyMutex,
types::{Constructor, GetDescriptor, Initializer, Representable},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

/// classmethod(function) -> method
Expand Down Expand Up @@ -48,7 +48,7 @@ impl PyPayload for PyClassMethod {

impl GetDescriptor for PyClassMethod {
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
cls: Option<PyObjectRef>,
vm: &VirtualMachine,
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Representable for MemberDescrObject {

impl GetDescriptor for MemberDescrObject {
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
_cls: Option<PyObjectRef>,
vm: &VirtualMachine,
Expand All @@ -211,7 +211,7 @@ impl GetDescriptor for MemberDescrObject {
let zelf = Self::_as_pyref(&zelf, vm)?;
zelf.member.get(x, vm)
}
None => Ok(zelf),
None => Ok(zelf.to_owned()),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,16 @@ impl PyFunction {

impl GetDescriptor for PyFunction {
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (_zelf, obj) = Self::_unwrap(&zelf, obj, vm)?;
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
zelf
zelf.to_owned().into()
Copy link
Member Author

Choose a reason for hiding this comment

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

not sure this is a good idea or not..

} else {
PyBoundMethod::new_ref(obj, zelf, &vm.ctx).into()
PyBoundMethod::new_ref(obj, zelf.to_owned().into(), &vm.ctx).into()
};
Ok(obj)
}
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/getset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ impl PyPayload for PyGetSet {

impl GetDescriptor for PyGetSet {
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
_cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = match Self::_check(&zelf, obj, vm) {
Some(obj) => obj,
None => return Ok(zelf),
None => return Ok(zelf.to_owned()),
};
if let Some(ref f) = zelf.getter {
f(vm, obj)
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 @@ -40,14 +40,14 @@ pub struct PropertyArgs {

impl GetDescriptor for PyProperty {
fn descr_get(
zelf_obj: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
_cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = Self::_unwrap(&zelf_obj, obj, vm)?;
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
if vm.is_none(&obj) {
Ok(zelf_obj)
Ok(zelf.to_owned().into())
} else if let Some(getter) = zelf.getter.read().as_ref() {
getter.call((obj,), vm)
} else {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/staticmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
common::lock::PyMutex,
function::{FuncArgs, IntoPyNativeFunc},
types::{Callable, Constructor, GetDescriptor, Initializer, Representable},
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

#[pyclass(module = false, name = "staticmethod")]
Expand All @@ -22,7 +22,7 @@ impl PyPayload for PyStaticMethod {

impl GetDescriptor for PyStaticMethod {
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
_cls: Option<PyObjectRef>,
vm: &VirtualMachine,
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
class::PyClassImpl,
function::{IntoFuncArgs, OptionalArg},
types::{Callable, Constructor, GetAttr, GetDescriptor, Representable},
AsObject, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
};

#[pyclass(module = false, name = "super")]
Expand Down Expand Up @@ -165,14 +165,14 @@ impl GetAttr for PySuper {

impl GetDescriptor for PySuper {
fn descr_get(
zelf_obj: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
_cls: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = Self::_unwrap(&zelf_obj, obj, vm)?;
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
if vm.is_none(&obj) || zelf.obj.is_some() {
return Ok(zelf_obj);
return Ok(zelf.to_owned().into());
}
let zelf_class = zelf.as_object().class();
if zelf_class.is(vm.ctx.types.super_type) {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ impl GetAttr for PyType {
let descr_get = attr_class.mro_find_map(|cls| cls.slots.descr_get.load());
if let Some(descr_get) = descr_get {
let mcl = mcl.to_owned().into();
return descr_get(attr.clone(), Some(zelf.to_owned().into()), Some(mcl), vm);
return descr_get(attr, Some(zelf.to_owned().into()), Some(mcl), vm);
}
}
}
Expand All @@ -1011,7 +1011,7 @@ impl GetAttr for PyType {
if let Some(attr) = zelf_attr {
let descr_get = attr.class().mro_find_map(|cls| cls.slots.descr_get.load());
if let Some(descr_get) = descr_get {
descr_get(attr, None, Some(zelf.to_owned().into()), vm)
descr_get(&attr, None, Some(zelf.to_owned().into()), vm)
} else {
Ok(attr)
}
Expand Down
4 changes: 2 additions & 2 deletions vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl PyObject {
.is_some()
{
let cls = obj_cls.to_owned().into();
return descr_get(descr, Some(self.to_owned()), Some(cls), vm).map(Some);
return descr_get(&descr, Some(self.to_owned()), Some(cls), vm).map(Some);
}
}
Some((descr, descr_get))
Expand All @@ -234,7 +234,7 @@ impl PyObject {
match descr_get {
Some(descr_get) => {
let cls = obj_cls.to_owned().into();
descr_get(attr, Some(self.to_owned()), Some(cls), vm).map(Some)
descr_get(&attr, Some(self.to_owned()), Some(cls), vm).map(Some)
}
None => Ok(Some(attr)),
}
Expand Down
8 changes: 4 additions & 4 deletions vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub(crate) type RichCompareFunc = fn(
pub(crate) type IterFunc = fn(PyObjectRef, &VirtualMachine) -> PyResult;
pub(crate) type IterNextFunc = fn(&PyObject, &VirtualMachine) -> PyResult<PyIterReturn>;
pub(crate) type DescrGetFunc =
fn(PyObjectRef, Option<PyObjectRef>, Option<PyObjectRef>, &VirtualMachine) -> PyResult;
fn(&PyObject, Option<PyObjectRef>, Option<PyObjectRef>, &VirtualMachine) -> PyResult;
pub(crate) type DescrSetFunc =
fn(&PyObject, PyObjectRef, PySetterValue, &VirtualMachine) -> PyResult<()>;
pub(crate) type NewFunc = fn(PyTypeRef, FuncArgs, &VirtualMachine) -> PyResult;
Expand Down Expand Up @@ -328,7 +328,7 @@ fn iternext_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyIterRetu
}

fn descr_get_wrapper(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
cls: Option<PyObjectRef>,
vm: &VirtualMachine,
Expand Down Expand Up @@ -847,7 +847,7 @@ pub trait Callable: PyPayload {
pub trait GetDescriptor: PyPayload {
#[pyslot]
fn descr_get(
zelf: PyObjectRef,
zelf: &PyObject,
obj: Option<PyObjectRef>,
cls: Option<PyObjectRef>,
vm: &VirtualMachine,
Expand All @@ -861,7 +861,7 @@ pub trait GetDescriptor: PyPayload {
cls: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult {
Self::descr_get(zelf, Some(obj), cls.into_option(), vm)
Self::descr_get(&zelf, Some(obj), cls.into_option(), vm)
}

#[inline]
Expand Down
5 changes: 3 additions & 2 deletions vm/src/vm/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ impl PyMethod {
.is_some()
{
let cls = cls.to_owned().into();
return descr_get(descr, Some(obj), Some(cls), vm).map(Self::Attribute);
return descr_get(&descr, Some(obj), Some(cls), vm)
.map(Self::Attribute);
}
}
descr_get
Expand All @@ -68,7 +69,7 @@ impl PyMethod {
}),
Some(descr_get) => {
let cls = cls.to_owned().into();
descr_get(attr, Some(obj), Some(cls), vm).map(Self::Attribute)
descr_get(&attr, Some(obj), Some(cls), vm).map(Self::Attribute)
}
None => Ok(Self::Attribute(attr)),
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/vm/vm_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl VirtualMachine {
let descr_get = descr
.class()
.mro_find_map(|cls| cls.slots.descr_get.load())?;
Some(descr_get(descr.to_owned(), obj, cls, self))
Some(descr_get(descr, obj, cls, self))
}

pub fn call_get_descriptor(&self, descr: &PyObject, obj: PyObjectRef) -> Option<PyResult> {
Expand Down