Skip to content

Commit 883a494

Browse files
Merge pull request RustPython#598 from palaviv/dir-module
Fix module dir
2 parents a264017 + 2d31e35 commit 883a494

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

tests/snippets/builtin_dir.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
class A:
3+
def test():
4+
pass
5+
6+
a = A()
7+
8+
assert "test" in dir(a)
9+
10+
import socket
11+
12+
assert "AF_INET" in dir(socket)

vm/src/obj/objtype.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::objdict;
22
use super::objstr;
33
use super::objtype; // Required for arg_check! to use isinstance
44
use crate::pyobject::{
5-
AttributeProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject, PyObjectPayload,
6-
PyObjectRef, PyResult, TypeProtocol,
5+
AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject,
6+
PyObjectPayload, PyObjectRef, PyResult, TypeProtocol,
77
};
88
use crate::vm::VirtualMachine;
99
use std::cell::RefCell;
@@ -277,6 +277,13 @@ pub fn get_attributes(obj: &PyObjectRef) -> PyAttributes {
277277
attributes.insert(name.to_string(), value.clone());
278278
}
279279
}
280+
281+
// Get module attributes:
282+
if let PyObjectPayload::Module { ref scope, .. } = &obj.payload {
283+
for (name, value) in scope.locals.get_key_value_pairs().iter() {
284+
attributes.insert(objstr::get_value(name).to_string(), value.clone());
285+
}
286+
}
280287
attributes
281288
}
282289

0 commit comments

Comments
 (0)