From 87ad2a0916ec4230afd8b6bfbf7506793aa491d8 Mon Sep 17 00:00:00 2001 From: Cody Piersall Date: Fri, 15 Sep 2017 21:08:32 -0500 Subject: [PATCH 1/3] Customize module.__dir__. Allow dir(module) to be informed by module.__all__, if it exists. --- Objects/moduleobject.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 2be49fbda38908..07e5321e16478c 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -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) @@ -743,7 +752,7 @@ module_dir(PyObject *self, PyObject *args) name); } } - + Py_XDECREF(all); Py_XDECREF(dict); return result; } From bcd663b5d81f0f639a6ecacae4ddf08f0c8fed34 Mon Sep 17 00:00:00 2001 From: Cody Piersall Date: Sun, 17 Sep 2017 21:12:36 -0500 Subject: [PATCH 2/3] Fix test_pkg to account for new module.__dir__. --- Lib/test/test_pkg.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_pkg.py b/Lib/test/test_pkg.py index 532e8fe6d0d886..7624cf5625ff41 100644 --- a/Lib/test/test_pkg.py +++ b/Lib/test/test_pkg.py @@ -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) From eab88877efb9423b12597684ad09c3ab713086c4 Mon Sep 17 00:00:00 2001 From: Cody Piersall Date: Sun, 17 Sep 2017 21:45:04 -0500 Subject: [PATCH 3/3] Add blurb. --- .../Core and Builtins/2017-09-17-21-44-58.bpo-31503.jrzqsj.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-09-17-21-44-58.bpo-31503.jrzqsj.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-17-21-44-58.bpo-31503.jrzqsj.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-17-21-44-58.bpo-31503.jrzqsj.rst new file mode 100644 index 00000000000000..8a36d27021caf8 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-17-21-44-58.bpo-31503.jrzqsj.rst @@ -0,0 +1 @@ +dir(some_module) now returns __all__ if it is defined.