Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 638d218

Browse files
authored
Fix builtins.dir (#5829)
1 parent 86d8d23 commit 638d218

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

Lib/test/test_descr.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,8 +2528,6 @@ def __iter__(self):
25282528
else:
25292529
self.fail("no ValueError from dict(%r)" % bad)
25302530

2531-
# TODO: RUSTPYTHON
2532-
@unittest.expectedFailure
25332531
def test_dir(self):
25342532
# Testing dir() ...
25352533
junk = 12

vm/src/builtins/module.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PyDictRef, PyStr, PyStrRef, PyType, PyTypeRef};
1+
use super::{PyDict, PyDictRef, PyStr, PyStrRef, PyType, PyTypeRef};
22
use crate::{
33
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
44
builtins::{PyStrInterned, pystr::AsPyStr},
@@ -170,10 +170,11 @@ impl PyModule {
170170

171171
#[pymethod(magic)]
172172
fn dir(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
173-
let dict = zelf
174-
.as_object()
175-
.dict()
176-
.ok_or_else(|| vm.new_value_error("module has no dict".to_owned()))?;
173+
// First check if __dict__ attribute exists and is actually a dictionary
174+
let dict_attr = zelf.as_object().get_attr(identifier!(vm, __dict__), vm)?;
175+
let dict = dict_attr
176+
.downcast::<PyDict>()
177+
.map_err(|_| vm.new_type_error("<module>.__dict__ is not a dictionary".to_owned()))?;
177178
let attrs = dict.into_iter().map(|(k, _v)| k).collect();
178179
Ok(attrs)
179180
}

0 commit comments

Comments
 (0)