|
1 | 1 | use super::objdict::PyDictRef;
|
2 | 2 | use super::objlist::PyList;
|
3 |
| -use super::objstr::PyStringRef; |
| 3 | +use super::objstr::{PyString, PyStringRef}; |
4 | 4 | use super::objtype;
|
5 | 5 | use crate::function::PyFuncArgs;
|
6 | 6 | use crate::obj::objproperty::PropertyBuilder;
|
@@ -117,13 +117,29 @@ fn object_repr(zelf: PyObjectRef, _vm: &VirtualMachine) -> String {
|
117 | 117 | format!("<{} object at 0x{:x}>", zelf.class().name, zelf.get_id())
|
118 | 118 | }
|
119 | 119 |
|
120 |
| -pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyList { |
121 |
| - let attributes = get_attributes(&obj); |
| 120 | +pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyList> { |
| 121 | + let mut attributes: PyAttributes = objtype::get_attributes(obj.class()); |
| 122 | + |
| 123 | + // 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 | + } |
| 135 | + } |
| 136 | + |
122 | 137 | let attributes: Vec<PyObjectRef> = attributes
|
123 | 138 | .keys()
|
124 | 139 | .map(|k| vm.ctx.new_str(k.to_string()))
|
125 | 140 | .collect();
|
126 |
| - PyList::from(attributes) |
| 141 | + |
| 142 | + Ok(PyList::from(attributes)) |
127 | 143 | }
|
128 | 144 |
|
129 | 145 | fn object_format(
|
@@ -230,17 +246,3 @@ fn object_getattr(
|
230 | 246 | Ok(None)
|
231 | 247 | }
|
232 | 248 | }
|
233 |
| - |
234 |
| -pub fn get_attributes(obj: &PyObjectRef) -> PyAttributes { |
235 |
| - // Get class attributes: |
236 |
| - let mut attributes = objtype::get_attributes(obj.class()); |
237 |
| - |
238 |
| - // Get instance attributes: |
239 |
| - if let Some(dict) = &obj.dict { |
240 |
| - for (key, value) in dict { |
241 |
| - attributes.insert(key.to_string(), value.clone()); |
242 |
| - } |
243 |
| - } |
244 |
| - |
245 |
| - attributes |
246 |
| -} |
|
0 commit comments