From 61b5d9ce378a855d6c12924ebf674f7ee3413dd8 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Sun, 8 Oct 2023 15:48:06 +0300 Subject: [PATCH] implement dir for ByObjectRef --- vm/src/builtins/object.rs | 19 ++----------------- vm/src/protocol/object.rs | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 1a91595445..351e559df6 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -1,4 +1,4 @@ -use super::{PyDict, PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef}; +use super::{PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef}; use crate::common::hash::PyHash; use crate::{ class::PyClassImpl, @@ -252,22 +252,7 @@ impl PyBaseObject { #[pymethod(magic)] pub fn dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult { - let attributes = obj.class().get_attributes(); - - let dict = PyDict::from_attributes(attributes, vm)?.into_ref(&vm.ctx); - - // Get instance attributes: - if let Some(object_dict) = obj.dict() { - vm.call_method( - dict.as_object(), - identifier!(vm, update).as_str(), - (object_dict,), - )?; - } - - let attributes: Vec<_> = dict.into_iter().map(|(k, _v)| k).collect(); - - Ok(PyList::from(attributes)) + obj.dir(vm) } #[pymethod(magic)] diff --git a/vm/src/protocol/object.rs b/vm/src/protocol/object.rs index bebfd21446..639e24dda5 100644 --- a/vm/src/protocol/object.rs +++ b/vm/src/protocol/object.rs @@ -3,7 +3,7 @@ use crate::{ builtins::{ - pystr::AsPyStr, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyStr, PyStrRef, + pystr::AsPyStr, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyList, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef, }, bytesinner::ByteInnerNewOptions, @@ -11,6 +11,7 @@ use crate::{ convert::{ToPyObject, ToPyResult}, dictdatatype::DictKey, function::{Either, OptionalArg, PyArithmeticValue, PySetterValue}, + object::PyPayload, protocol::{PyIter, PyMapping, PySequence}, types::{Constructor, PyComparisonOp}, AsObject, Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine, @@ -62,6 +63,23 @@ impl PyObjectRef { } // PyObject *PyObject_Dir(PyObject *o) + pub fn dir(self, vm: &VirtualMachine) -> PyResult { + let attributes = self.class().get_attributes(); + + let dict = PyDict::from_attributes(attributes, vm)?.into_ref(&vm.ctx); + + if let Some(object_dict) = self.dict() { + vm.call_method( + dict.as_object(), + identifier!(vm, update).as_str(), + (object_dict,), + )?; + } + + let attributes: Vec<_> = dict.into_iter().map(|(k, _v)| k).collect(); + + Ok(PyList::from(attributes)) + } } impl PyObject {