Skip to content

Commit 86fca04

Browse files
committed
descriptors and method calls to take &PyObject
1 parent 1229798 commit 86fca04

24 files changed

+166
-195
lines changed

src/shell/helper.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use rustpython_vm::{
33
builtins::{PyDictRef, PyStrRef},
44
function::ArgIterable,
5-
identifier, PyResult, TryFromObject, VirtualMachine,
5+
identifier, AsObject, PyResult, TryFromObject, VirtualMachine,
66
};
77

88
pub struct ShellHelper<'vm> {
@@ -82,19 +82,17 @@ impl<'vm> ShellHelper<'vm> {
8282
current = current.get_attr(&attr, self.vm).ok()?;
8383
}
8484

85-
let current_iter = str_iter_method(current, identifier!(self.vm, __dir__)).ok()?;
85+
let current_iter = str_iter_method(&current, identifier!(self.vm, __dir__)).ok()?;
8686

8787
(last, current_iter, None)
8888
} else {
8989
// we need to get a variable based off of globals/builtins
9090

9191
let globals =
92-
str_iter_method(self.globals.clone().into(), identifier!(self.vm, keys)).ok()?;
93-
let builtins = str_iter_method(
94-
self.vm.builtins.clone().into(),
95-
identifier!(self.vm, __dir__),
96-
)
97-
.ok()?;
92+
str_iter_method(self.globals.as_object(), identifier!(self.vm, keys)).ok()?;
93+
let builtins =
94+
str_iter_method(self.vm.builtins.as_object(), identifier!(self.vm, __dir__))
95+
.ok()?;
9896
(first, globals, Some(builtins))
9997
};
10098
Some((word_start, iter1.chain(iter2.into_iter().flatten())))

vm/src/builtins/builtin_func.rs

+4-4
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,7 +189,7 @@ 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,
@@ -199,9 +199,9 @@ impl GetDescriptor for PyBuiltinMethod {
199199
Err(result) => return result,
200200
};
201201
let r = if vm.is_none(&obj) && !Self::_cls_is(&cls, obj.class()) {
202-
zelf.into()
202+
zelf.to_owned().into()
203203
} else {
204-
PyBoundMethod::new_ref(obj, zelf.into(), &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

+2-2
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

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
class::PyClassImpl,
44
function::PySetterValue,
55
types::{Constructor, GetDescriptor, Representable, Unconstructible},
6-
AsObject, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
6+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
77
};
88
use rustpython_common::lock::PyRwLock;
99

@@ -122,7 +122,7 @@ impl MemberDescrObject {
122122

123123
#[pyslot]
124124
fn descr_set(
125-
zelf: PyObjectRef,
125+
zelf: &PyObject,
126126
obj: PyObjectRef,
127127
value: PySetterValue<PyObjectRef>,
128128
vm: &VirtualMachine,
@@ -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::_zelf(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

+6-6
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,13 @@ impl PyFunction {
374374
// {"__builtins__", T_OBJECT, OFF(func_builtins), READONLY},
375375
#[pymember(magic)]
376376
fn globals(vm: &VirtualMachine, zelf: PyObjectRef) -> PyResult {
377-
let zelf = Self::_zelf(zelf, vm)?;
377+
let zelf = Self::_zelf(&zelf, vm)?;
378378
Ok(zelf.globals.clone().into())
379379
}
380380

381381
#[pymember(magic)]
382382
fn closure(vm: &VirtualMachine, zelf: PyObjectRef) -> PyResult {
383-
let zelf = Self::_zelf(zelf, vm)?;
383+
let zelf = Self::_zelf(&zelf, vm)?;
384384
Ok(vm.unwrap_or_none(zelf.closure.clone().map(|x| x.to_pyobject(vm))))
385385
}
386386

@@ -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.into()
444+
zelf.to_owned().into()
445445
} else {
446-
PyBoundMethod::new_ref(obj, zelf.into(), &vm.ctx).into()
446+
PyBoundMethod::new_ref(obj, zelf.to_owned().into(), &vm.ctx).into()
447447
};
448448
Ok(obj)
449449
}
@@ -507,7 +507,7 @@ impl GetAttr for PyBoundMethod {
507507
.interned_str(name)
508508
.and_then(|attr_name| zelf.get_class_attr(attr_name));
509509
if let Some(obj) = class_attr {
510-
return vm.call_if_get_descriptor(obj, zelf.to_owned().into());
510+
return vm.call_if_get_descriptor(&obj, zelf.to_owned().into());
511511
}
512512
zelf.function.get_attr(name, vm)
513513
}

vm/src/builtins/getset.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
class::PyClassImpl,
77
function::{IntoPyGetterFunc, IntoPySetterFunc, PyGetterFunc, PySetterFunc, PySetterValue},
88
types::{Constructor, GetDescriptor, Unconstructible},
9-
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
9+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
1010
};
1111

1212
#[pyclass(module = false, name = "getset_descriptor")]
@@ -46,7 +46,7 @@ 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,
@@ -100,12 +100,12 @@ impl PyGetSet {
100100

101101
#[pyslot]
102102
fn descr_set(
103-
zelf: PyObjectRef,
103+
zelf: &PyObject,
104104
obj: PyObjectRef,
105105
value: PySetterValue<PyObjectRef>,
106106
vm: &VirtualMachine,
107107
) -> PyResult<()> {
108-
let zelf = PyRef::<Self>::try_from_object(vm, zelf)?;
108+
let zelf = zelf.try_to_ref::<Self>(vm)?;
109109
if let Some(ref f) = zelf.setter {
110110
f(vm, obj, value)
111111
} else {
@@ -123,11 +123,11 @@ impl PyGetSet {
123123
value: PyObjectRef,
124124
vm: &VirtualMachine,
125125
) -> PyResult<()> {
126-
Self::descr_set(zelf, obj, PySetterValue::Assign(value), vm)
126+
Self::descr_set(&zelf, obj, PySetterValue::Assign(value), vm)
127127
}
128128
#[pymethod]
129129
fn __delete__(zelf: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
130-
Self::descr_set(zelf, obj, PySetterValue::Delete, vm)
130+
Self::descr_set(&zelf, obj, PySetterValue::Delete, vm)
131131
}
132132

133133
#[pygetset(magic)]

vm/src/builtins/property.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
class::PyClassImpl,
99
function::{FuncArgs, PySetterValue},
1010
types::{Constructor, GetDescriptor, Initializer},
11-
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
11+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1212
};
1313

1414
#[pyclass(module = false, name = "property")]
@@ -40,14 +40,14 @@ pub struct PropertyArgs {
4040

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

6363
#[pyslot]
6464
fn descr_set(
65-
zelf: PyObjectRef,
65+
zelf: &PyObject,
6666
obj: PyObjectRef,
6767
value: PySetterValue,
6868
vm: &VirtualMachine,
6969
) -> PyResult<()> {
70-
let zelf = PyRef::<Self>::try_from_object(vm, zelf)?;
70+
let zelf = zelf.try_to_ref::<Self>(vm)?;
7171
match value {
7272
PySetterValue::Assign(value) => {
7373
if let Some(setter) = zelf.setter.read().as_ref() {
@@ -92,11 +92,11 @@ impl PyProperty {
9292
value: PyObjectRef,
9393
vm: &VirtualMachine,
9494
) -> PyResult<()> {
95-
Self::descr_set(zelf, obj, PySetterValue::Assign(value), vm)
95+
Self::descr_set(&zelf, obj, PySetterValue::Assign(value), vm)
9696
}
9797
#[pymethod]
9898
fn __delete__(zelf: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
99-
Self::descr_set(zelf, obj, PySetterValue::Delete, vm)
99+
Self::descr_set(&zelf, obj, PySetterValue::Delete, vm)
100100
}
101101

102102
// Access functions

vm/src/builtins/staticmethod.rs

+2-2
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

+4-4
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")]
@@ -150,7 +150,7 @@ impl GetAttr for PySuper {
150150
if let Some(descr) = cls.get_direct_attr(name) {
151151
return vm
152152
.call_get_descriptor_specific(
153-
descr.clone(),
153+
&descr,
154154
// Only pass 'obj' param if this is instance-mode super (See https://bugs.python.org/issue743267)
155155
if obj.is(&start_type) { None } else { Some(obj) },
156156
Some(start_type.as_object().to_owned()),
@@ -165,14 +165,14 @@ impl GetAttr for PySuper {
165165

166166
impl GetDescriptor for PySuper {
167167
fn descr_get(
168-
zelf: PyObjectRef,
168+
zelf: &PyObject,
169169
obj: Option<PyObjectRef>,
170170
_cls: Option<PyObjectRef>,
171171
vm: &VirtualMachine,
172172
) -> PyResult {
173173
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
174174
if vm.is_none(&obj) || zelf.obj.is_some() {
175-
return Ok(zelf.into());
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

+8-8
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ impl PyType {
847847

848848
if let Some(init_subclass) = typ.get_super_attr(identifier!(vm, __init_subclass__)) {
849849
let init_subclass = vm
850-
.call_get_descriptor_specific(init_subclass.clone(), None, Some(typ.clone().into()))
850+
.call_get_descriptor_specific(&init_subclass, None, Some(typ.clone().into()))
851851
.unwrap_or(Ok(init_subclass))?;
852852
init_subclass.call(kwargs, vm)?;
853853
};
@@ -1004,7 +1004,7 @@ impl GetAttr for PyType {
10041004
let descr_get = attr_class.mro_find_map(|cls| cls.slots.descr_get.load());
10051005
if let Some(descr_get) = descr_get {
10061006
let mcl = mcl.to_owned().into();
1007-
return descr_get(attr.clone(), Some(zelf.to_owned().into()), Some(mcl), vm);
1007+
return descr_get(attr, Some(zelf.to_owned().into()), Some(mcl), vm);
10081008
}
10091009
}
10101010
}
@@ -1014,14 +1014,14 @@ impl GetAttr for PyType {
10141014
if let Some(ref attr) = zelf_attr {
10151015
let descr_get = attr.class().mro_find_map(|cls| cls.slots.descr_get.load());
10161016
if let Some(descr_get) = descr_get {
1017-
return descr_get(attr.clone(), None, Some(zelf.to_owned().into()), vm);
1017+
return descr_get(attr, None, Some(zelf.to_owned().into()), vm);
10181018
}
10191019
}
10201020

10211021
if let Some(cls_attr) = zelf_attr {
10221022
Ok(cls_attr)
10231023
} else if let Some(attr) = mcl_attr {
1024-
vm.call_if_get_descriptor(attr, zelf.to_owned().into())
1024+
vm.call_if_get_descriptor(&attr, zelf.to_owned().into())
10251025
} else {
10261026
return Err(attribute_error(zelf, name_str.as_str(), vm));
10271027
}
@@ -1040,7 +1040,7 @@ impl SetAttr for PyType {
10401040
if let Some(attr) = zelf.get_class_attr(attr_name) {
10411041
let descr_set = attr.class().mro_find_map(|cls| cls.slots.descr_set.load());
10421042
if let Some(descriptor) = descr_set {
1043-
return descriptor(attr, zelf.to_owned().into(), value, vm);
1043+
return descriptor(&attr, zelf.to_owned().into(), value, vm);
10441044
}
10451045
}
10461046
let assign = value.is_assign();
@@ -1137,10 +1137,10 @@ fn find_base_dict_descr(cls: &Py<PyType>, vm: &VirtualMachine) -> Option<PyObjec
11371137
fn subtype_get_dict(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
11381138
// TODO: obj.class().as_pyref() need to be supported
11391139
let ret = match find_base_dict_descr(obj.class(), vm) {
1140-
Some(descr) => vm.call_get_descriptor(descr, obj).unwrap_or_else(|obj| {
1140+
Some(descr) => vm.call_get_descriptor(&descr, obj).unwrap_or_else(|| {
11411141
Err(vm.new_type_error(format!(
11421142
"this __dict__ descriptor does not support '{}' objects",
1143-
obj.class()
1143+
descr.class()
11441144
)))
11451145
})?,
11461146
None => object::object_get_dict(obj, vm)?.into(),
@@ -1161,7 +1161,7 @@ fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -
11611161
cls.name()
11621162
))
11631163
})?;
1164-
descr_set(descr, obj, PySetterValue::Assign(value), vm)
1164+
descr_set(&descr, obj, PySetterValue::Assign(value), vm)
11651165
}
11661166
None => {
11671167
object::object_set_dict(obj, value.try_into_value(vm)?, vm)?;

vm/src/cformat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ fn spec_format_bytes(
3333
Ok(buffer.contiguous_or_collect(|bytes| spec.format_bytes(bytes)))
3434
} else {
3535
let bytes = vm
36-
.get_special_method(obj, identifier!(vm, __bytes__))?
37-
.map_err(|obj| {
36+
.get_special_method(&obj, identifier!(vm, __bytes__))?
37+
.ok_or_else(|| {
3838
vm.new_type_error(format!(
3939
"%b requires a bytes-like object, or an object that \
4040
implements __bytes__, not '{}'",

0 commit comments

Comments
 (0)