|
1 | 1 | use super::objdict::PyDictRef;
|
2 | 2 | use super::objlist::PyList;
|
3 |
| -use super::objstr::{PyString, PyStringRef}; |
| 3 | +use super::objstr::PyStringRef; |
4 | 4 | use super::objtype;
|
5 | 5 | use crate::function::PyFuncArgs;
|
6 | 6 | use crate::obj::objproperty::PropertyBuilder;
|
@@ -118,26 +118,19 @@ fn object_repr(zelf: PyObjectRef, _vm: &VirtualMachine) -> String {
|
118 | 118 | }
|
119 | 119 |
|
120 | 120 | pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyList> {
|
121 |
| - let mut attributes: PyAttributes = objtype::get_attributes(obj.class()); |
| 121 | + let attributes: PyAttributes = objtype::get_attributes(obj.class()); |
| 122 | + |
| 123 | + let dict = PyDictRef::from_attributes(attributes, vm)?; |
122 | 124 |
|
123 | 125 | // Get instance attributes:
|
124 |
| - if let Some(dict) = &obj.dict { |
125 |
| - for (key, value) in dict { |
126 |
| - if let Some(key_string) = key.payload::<PyString>() { |
127 |
| - attributes.insert(key_string.to_string(), value.clone()); |
128 |
| - } else { |
129 |
| - return Err(vm.new_type_error(format!( |
130 |
| - "Attribute is not a string: {:?}", |
131 |
| - vm.to_pystr(&key)? |
132 |
| - ))); |
133 |
| - } |
134 |
| - } |
| 126 | + if let Some(object_dict) = &obj.dict { |
| 127 | + vm.invoke( |
| 128 | + vm.get_attribute(dict.clone().into_object(), "update")?, |
| 129 | + object_dict.clone().into_object(), |
| 130 | + )?; |
135 | 131 | }
|
136 | 132 |
|
137 |
| - let attributes: Vec<PyObjectRef> = attributes |
138 |
| - .keys() |
139 |
| - .map(|k| vm.ctx.new_str(k.to_string())) |
140 |
| - .collect(); |
| 133 | + let attributes: Vec<_> = dict.into_iter().map(|(k, _v)| k.clone()).collect(); |
141 | 134 |
|
142 | 135 | Ok(PyList::from(attributes))
|
143 | 136 | }
|
|
0 commit comments