Skip to content
Merged
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
19 changes: 2 additions & 17 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -252,22 +252,7 @@ impl PyBaseObject {

#[pymethod(magic)]
pub fn dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyList> {
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)]
Expand Down
20 changes: 19 additions & 1 deletion vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

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,
common::{hash::PyHash, str::to_ascii},
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,
Expand Down Expand Up @@ -62,6 +63,23 @@ impl PyObjectRef {
}

// PyObject *PyObject_Dir(PyObject *o)
pub fn dir(self, vm: &VirtualMachine) -> PyResult<PyList> {
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 {
Expand Down