Skip to content

Commit 830389f

Browse files
authored
implement dir for ByObjectRef (RustPython#5088)
1 parent 2c3dbb5 commit 830389f

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

vm/src/builtins/object.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyDict, PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef};
1+
use super::{PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef};
22
use crate::common::hash::PyHash;
33
use crate::{
44
class::PyClassImpl,
@@ -252,22 +252,7 @@ impl PyBaseObject {
252252

253253
#[pymethod(magic)]
254254
pub fn dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyList> {
255-
let attributes = obj.class().get_attributes();
256-
257-
let dict = PyDict::from_attributes(attributes, vm)?.into_ref(&vm.ctx);
258-
259-
// Get instance attributes:
260-
if let Some(object_dict) = obj.dict() {
261-
vm.call_method(
262-
dict.as_object(),
263-
identifier!(vm, update).as_str(),
264-
(object_dict,),
265-
)?;
266-
}
267-
268-
let attributes: Vec<_> = dict.into_iter().map(|(k, _v)| k).collect();
269-
270-
Ok(PyList::from(attributes))
255+
obj.dir(vm)
271256
}
272257

273258
#[pymethod(magic)]

vm/src/protocol/object.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
44
use crate::{
55
builtins::{
6-
pystr::AsPyStr, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyStr, PyStrRef,
6+
pystr::AsPyStr, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyList, PyStr, PyStrRef,
77
PyTuple, PyTupleRef, PyType, PyTypeRef,
88
},
99
bytesinner::ByteInnerNewOptions,
1010
common::{hash::PyHash, str::to_ascii},
1111
convert::{ToPyObject, ToPyResult},
1212
dictdatatype::DictKey,
1313
function::{Either, OptionalArg, PyArithmeticValue, PySetterValue},
14+
object::PyPayload,
1415
protocol::{PyIter, PyMapping, PySequence},
1516
types::{Constructor, PyComparisonOp},
1617
AsObject, Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
@@ -62,6 +63,23 @@ impl PyObjectRef {
6263
}
6364

6465
// PyObject *PyObject_Dir(PyObject *o)
66+
pub fn dir(self, vm: &VirtualMachine) -> PyResult<PyList> {
67+
let attributes = self.class().get_attributes();
68+
69+
let dict = PyDict::from_attributes(attributes, vm)?.into_ref(&vm.ctx);
70+
71+
if let Some(object_dict) = self.dict() {
72+
vm.call_method(
73+
dict.as_object(),
74+
identifier!(vm, update).as_str(),
75+
(object_dict,),
76+
)?;
77+
}
78+
79+
let attributes: Vec<_> = dict.into_iter().map(|(k, _v)| k).collect();
80+
81+
Ok(PyList::from(attributes))
82+
}
6583
}
6684

6785
impl PyObject {

0 commit comments

Comments
 (0)