Skip to content

Commit 6289f2e

Browse files
committed
descriptors and method calls to take &PyObject
1 parent bdf5434 commit 6289f2e

13 files changed

+35
-34
lines changed

vm/src/builtins/builtin_func.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
class::PyClassImpl,
55
function::{FuncArgs, IntoPyNativeFunc, PyNativeFunc},
66
types::{Callable, Constructor, GetDescriptor, Representable, Unconstructible},
7-
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
7+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
88
};
99
use std::fmt;
1010

@@ -189,19 +189,19 @@ impl fmt::Debug for PyBuiltinMethod {
189189

190190
impl GetDescriptor for PyBuiltinMethod {
191191
fn descr_get(
192-
zelf: PyObjectRef,
192+
zelf: &PyObject,
193193
obj: Option<PyObjectRef>,
194194
cls: Option<PyObjectRef>,
195195
vm: &VirtualMachine,
196196
) -> PyResult {
197197
let (_zelf, obj) = match Self::_check(&zelf, obj, vm) {
198198
Some(obj) => obj,
199-
None => return Ok(zelf),
199+
None => return Ok(zelf.to_owned()),
200200
};
201201
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
202-
zelf
202+
zelf.to_owned().into()
203203
} else {
204-
PyBoundMethod::new_ref(obj, zelf, &vm.ctx).into()
204+
PyBoundMethod::new_ref(obj, zelf.to_owned().into(), &vm.ctx).into()
205205
};
206206
Ok(r)
207207
}

vm/src/builtins/classmethod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
class::PyClassImpl,
44
common::lock::PyMutex,
55
types::{Constructor, GetDescriptor, Initializer, Representable},
6-
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
6+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
77
};
88

99
/// classmethod(function) -> method
@@ -48,7 +48,7 @@ impl PyPayload for PyClassMethod {
4848

4949
impl GetDescriptor for PyClassMethod {
5050
fn descr_get(
51-
zelf: PyObjectRef,
51+
zelf: &PyObject,
5252
obj: Option<PyObjectRef>,
5353
cls: Option<PyObjectRef>,
5454
vm: &VirtualMachine,

vm/src/builtins/descriptor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl Representable for MemberDescrObject {
201201

202202
impl GetDescriptor for MemberDescrObject {
203203
fn descr_get(
204-
zelf: PyObjectRef,
204+
zelf: &PyObject,
205205
obj: Option<PyObjectRef>,
206206
_cls: Option<PyObjectRef>,
207207
vm: &VirtualMachine,
@@ -211,7 +211,7 @@ impl GetDescriptor for MemberDescrObject {
211211
let zelf = Self::_as_pyref(&zelf, vm)?;
212212
zelf.member.get(x, vm)
213213
}
214-
None => Ok(zelf),
214+
None => Ok(zelf.to_owned()),
215215
}
216216
}
217217
}

vm/src/builtins/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,16 @@ impl PyFunction {
434434

435435
impl GetDescriptor for PyFunction {
436436
fn descr_get(
437-
zelf: PyObjectRef,
437+
zelf: &PyObject,
438438
obj: Option<PyObjectRef>,
439439
cls: Option<PyObjectRef>,
440440
vm: &VirtualMachine,
441441
) -> PyResult {
442442
let (_zelf, obj) = Self::_unwrap(&zelf, obj, vm)?;
443443
let obj = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
444-
zelf
444+
zelf.to_owned().into()
445445
} else {
446-
PyBoundMethod::new_ref(obj, zelf, &vm.ctx).into()
446+
PyBoundMethod::new_ref(obj, zelf.to_owned().into(), &vm.ctx).into()
447447
};
448448
Ok(obj)
449449
}

vm/src/builtins/getset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ impl PyPayload for PyGetSet {
4646

4747
impl GetDescriptor for PyGetSet {
4848
fn descr_get(
49-
zelf: PyObjectRef,
49+
zelf: &PyObject,
5050
obj: Option<PyObjectRef>,
5151
_cls: Option<PyObjectRef>,
5252
vm: &VirtualMachine,
5353
) -> PyResult {
5454
let (zelf, obj) = match Self::_check(&zelf, obj, vm) {
5555
Some(obj) => obj,
56-
None => return Ok(zelf),
56+
None => return Ok(zelf.to_owned()),
5757
};
5858
if let Some(ref f) = zelf.getter {
5959
f(vm, obj)

vm/src/builtins/property.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ pub struct PropertyArgs {
4040

4141
impl GetDescriptor for PyProperty {
4242
fn descr_get(
43-
zelf_obj: PyObjectRef,
43+
zelf: &PyObject,
4444
obj: Option<PyObjectRef>,
4545
_cls: Option<PyObjectRef>,
4646
vm: &VirtualMachine,
4747
) -> PyResult {
48-
let (zelf, obj) = Self::_unwrap(&zelf_obj, obj, vm)?;
48+
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
4949
if vm.is_none(&obj) {
50-
Ok(zelf_obj)
50+
Ok(zelf.to_owned().into())
5151
} else if let Some(getter) = zelf.getter.read().as_ref() {
5252
getter.call((obj,), vm)
5353
} else {

vm/src/builtins/staticmethod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
common::lock::PyMutex,
66
function::{FuncArgs, IntoPyNativeFunc},
77
types::{Callable, Constructor, GetDescriptor, Initializer, Representable},
8-
Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
8+
Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
99
};
1010

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

2323
impl GetDescriptor for PyStaticMethod {
2424
fn descr_get(
25-
zelf: PyObjectRef,
25+
zelf: &PyObject,
2626
obj: Option<PyObjectRef>,
2727
_cls: Option<PyObjectRef>,
2828
vm: &VirtualMachine,

vm/src/builtins/super.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
class::PyClassImpl,
99
function::{IntoFuncArgs, OptionalArg},
1010
types::{Callable, Constructor, GetAttr, GetDescriptor, Representable},
11-
AsObject, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
11+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
1212
};
1313

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

166166
impl GetDescriptor for PySuper {
167167
fn descr_get(
168-
zelf_obj: PyObjectRef,
168+
zelf: &PyObject,
169169
obj: Option<PyObjectRef>,
170170
_cls: Option<PyObjectRef>,
171171
vm: &VirtualMachine,
172172
) -> PyResult {
173-
let (zelf, obj) = Self::_unwrap(&zelf_obj, obj, vm)?;
173+
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
174174
if vm.is_none(&obj) || zelf.obj.is_some() {
175-
return Ok(zelf_obj);
175+
return Ok(zelf.to_owned().into());
176176
}
177177
let zelf_class = zelf.as_object().class();
178178
if zelf_class.is(vm.ctx.types.super_type) {

vm/src/builtins/type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ impl GetAttr for PyType {
10011001
let descr_get = attr_class.mro_find_map(|cls| cls.slots.descr_get.load());
10021002
if let Some(descr_get) = descr_get {
10031003
let mcl = mcl.to_owned().into();
1004-
return descr_get(attr.clone(), Some(zelf.to_owned().into()), Some(mcl), vm);
1004+
return descr_get(attr, Some(zelf.to_owned().into()), Some(mcl), vm);
10051005
}
10061006
}
10071007
}
@@ -1011,7 +1011,7 @@ impl GetAttr for PyType {
10111011
if let Some(attr) = zelf_attr {
10121012
let descr_get = attr.class().mro_find_map(|cls| cls.slots.descr_get.load());
10131013
if let Some(descr_get) = descr_get {
1014-
descr_get(attr, None, Some(zelf.to_owned().into()), vm)
1014+
descr_get(&attr, None, Some(zelf.to_owned().into()), vm)
10151015
} else {
10161016
Ok(attr)
10171017
}

vm/src/protocol/object.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl PyObject {
212212
.is_some()
213213
{
214214
let cls = obj_cls.to_owned().into();
215-
return descr_get(descr, Some(self.to_owned()), Some(cls), vm).map(Some);
215+
return descr_get(&descr, Some(self.to_owned()), Some(cls), vm).map(Some);
216216
}
217217
}
218218
Some((descr, descr_get))
@@ -234,7 +234,7 @@ impl PyObject {
234234
match descr_get {
235235
Some(descr_get) => {
236236
let cls = obj_cls.to_owned().into();
237-
descr_get(attr, Some(self.to_owned()), Some(cls), vm).map(Some)
237+
descr_get(&attr, Some(self.to_owned()), Some(cls), vm).map(Some)
238238
}
239239
None => Ok(Some(attr)),
240240
}

vm/src/types/slot.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub(crate) type RichCompareFunc = fn(
171171
pub(crate) type IterFunc = fn(PyObjectRef, &VirtualMachine) -> PyResult;
172172
pub(crate) type IterNextFunc = fn(&PyObject, &VirtualMachine) -> PyResult<PyIterReturn>;
173173
pub(crate) type DescrGetFunc =
174-
fn(PyObjectRef, Option<PyObjectRef>, Option<PyObjectRef>, &VirtualMachine) -> PyResult;
174+
fn(&PyObject, Option<PyObjectRef>, Option<PyObjectRef>, &VirtualMachine) -> PyResult;
175175
pub(crate) type DescrSetFunc =
176176
fn(&PyObject, PyObjectRef, PySetterValue, &VirtualMachine) -> PyResult<()>;
177177
pub(crate) type NewFunc = fn(PyTypeRef, FuncArgs, &VirtualMachine) -> PyResult;
@@ -328,7 +328,7 @@ fn iternext_wrapper(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyIterRetu
328328
}
329329

330330
fn descr_get_wrapper(
331-
zelf: PyObjectRef,
331+
zelf: &PyObject,
332332
obj: Option<PyObjectRef>,
333333
cls: Option<PyObjectRef>,
334334
vm: &VirtualMachine,
@@ -847,7 +847,7 @@ pub trait Callable: PyPayload {
847847
pub trait GetDescriptor: PyPayload {
848848
#[pyslot]
849849
fn descr_get(
850-
zelf: PyObjectRef,
850+
zelf: &PyObject,
851851
obj: Option<PyObjectRef>,
852852
cls: Option<PyObjectRef>,
853853
vm: &VirtualMachine,
@@ -861,7 +861,7 @@ pub trait GetDescriptor: PyPayload {
861861
cls: OptionalArg<PyObjectRef>,
862862
vm: &VirtualMachine,
863863
) -> PyResult {
864-
Self::descr_get(zelf, Some(obj), cls.into_option(), vm)
864+
Self::descr_get(&zelf, Some(obj), cls.into_option(), vm)
865865
}
866866

867867
#[inline]

vm/src/vm/method.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ impl PyMethod {
4444
.is_some()
4545
{
4646
let cls = cls.to_owned().into();
47-
return descr_get(descr, Some(obj), Some(cls), vm).map(Self::Attribute);
47+
return descr_get(&descr, Some(obj), Some(cls), vm)
48+
.map(Self::Attribute);
4849
}
4950
}
5051
descr_get
@@ -68,7 +69,7 @@ impl PyMethod {
6869
}),
6970
Some(descr_get) => {
7071
let cls = cls.to_owned().into();
71-
descr_get(attr, Some(obj), Some(cls), vm).map(Self::Attribute)
72+
descr_get(&attr, Some(obj), Some(cls), vm).map(Self::Attribute)
7273
}
7374
None => Ok(Self::Attribute(attr)),
7475
}

vm/src/vm/vm_object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl VirtualMachine {
8080
let descr_get = descr
8181
.class()
8282
.mro_find_map(|cls| cls.slots.descr_get.load())?;
83-
Some(descr_get(descr.to_owned(), obj, cls, self))
83+
Some(descr_get(descr, obj, cls, self))
8484
}
8585

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

0 commit comments

Comments
 (0)