|
3 | 3 |
|
4 | 4 | use crate::{
|
5 | 5 | builtins::{
|
6 |
| - pystr::AsPyStr, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyStr, PyStrRef, |
| 6 | + pystr::AsPyStr, PyBytes, PyDict, PyDictRef, PyGenericAlias, PyInt, PyList, PyStr, PyStrRef, |
7 | 7 | PyTuple, PyTupleRef, PyType, PyTypeRef,
|
8 | 8 | },
|
9 | 9 | bytesinner::ByteInnerNewOptions,
|
10 | 10 | common::{hash::PyHash, str::to_ascii},
|
11 | 11 | convert::{ToPyObject, ToPyResult},
|
12 | 12 | dictdatatype::DictKey,
|
13 | 13 | function::{Either, OptionalArg, PyArithmeticValue, PySetterValue},
|
| 14 | + object::PyPayload, |
14 | 15 | protocol::{PyIter, PyMapping, PySequence},
|
15 | 16 | types::{Constructor, PyComparisonOp},
|
16 | 17 | AsObject, Py, PyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
|
@@ -62,6 +63,23 @@ impl PyObjectRef {
|
62 | 63 | }
|
63 | 64 |
|
64 | 65 | // 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 | + } |
65 | 83 | }
|
66 | 84 |
|
67 | 85 | impl PyObject {
|
|
0 commit comments