Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions Lib/test/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,12 @@ def test_6(self):

import t6
self.assertEqual(fixdir(dir(t6)),
['__all__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__', '__path__',
'__spec__'])
['eggs', 'ham', 'spam'])
s = """
import t6
from t6 import *
self.assertEqual(fixdir(dir(t6)),
['__all__', '__cached__', '__doc__', '__file__',
'__loader__', '__name__', '__package__',
'__path__', '__spec__', 'eggs', 'ham', 'spam'])
['eggs', 'ham', 'spam'])
self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
"""
self.run_code(s)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dir(some_module) now returns __all__ if it is defined.
19 changes: 14 additions & 5 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,20 @@ module_dir(PyObject *self, PyObject *args)
{
_Py_IDENTIFIER(__dict__);
PyObject *result = NULL;
PyObject *module_all = NULL;
PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);

if (dict != NULL) {
if (PyDict_Check(dict))
result = PyDict_Keys(dict);
PyObject *all = PyUnicode_FromString("__all__");
if (all != NULL && dict != NULL) {
if (PyDict_Check(dict)) {
module_all = PyDict_GetItem(dict, all);
if (module_all != NULL) {
/* We need to actually check if it's a list and then copy the items */
Py_INCREF(module_all);
result = module_all;
}
else
result = PyDict_Keys(dict);
}
else {
const char *name = PyModule_GetName(self);
if (name)
Expand All @@ -743,7 +752,7 @@ module_dir(PyObject *self, PyObject *args)
name);
}
}

Py_XDECREF(all);
Py_XDECREF(dict);
return result;
}
Expand Down