summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorIvan Levkivskyi <levkivskyi@gmail.com>2017-12-14 10:59:44 (GMT)
committerGitHub <noreply@github.com>2017-12-14 10:59:44 (GMT)
commit5364b5cd7571f2dfa75acd37b388c14ac33fef73 (patch)
tree43b89bf162a571e979946d2d9dcfda82b4e0f4c8 /Objects
parent9e7c136ad8bc8e8eec50c2a8ae5ff02752f695a2 (diff)
downloadcpython-5364b5cd7571f2dfa75acd37b388c14ac33fef73.zip
cpython-5364b5cd7571f2dfa75acd37b388c14ac33fef73.tar.gz
cpython-5364b5cd7571f2dfa75acd37b388c14ac33fef73.tar.bz2
bpo-32225: Implementation of PEP 562 (#4731)
Implement PEP 562: module __getattr__ and __dir__. The implementation simply updates module_getattro and module_dir.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/moduleobject.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 2973263..d6cde40 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -679,12 +679,19 @@ module_repr(PyModuleObject *m)
static PyObject*
module_getattro(PyModuleObject *m, PyObject *name)
{
- PyObject *attr, *mod_name;
+ PyObject *attr, *mod_name, *getattr;
attr = PyObject_GenericGetAttr((PyObject *)m, name);
- if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
+ if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
return attr;
+ }
PyErr_Clear();
if (m->md_dict) {
+ _Py_IDENTIFIER(__getattr__);
+ getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__);
+ if (getattr) {
+ PyObject* stack[1] = {name};
+ return _PyObject_FastCall(getattr, stack, 1);
+ }
_Py_IDENTIFIER(__name__);
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
if (mod_name && PyUnicode_Check(mod_name)) {
@@ -730,8 +737,15 @@ module_dir(PyObject *self, PyObject *args)
PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
if (dict != NULL) {
- if (PyDict_Check(dict))
- result = PyDict_Keys(dict);
+ if (PyDict_Check(dict)) {
+ PyObject *dirfunc = PyDict_GetItemString(dict, "__dir__");
+ if (dirfunc) {
+ result = _PyObject_CallNoArg(dirfunc);
+ }
+ else {
+ result = PyDict_Keys(dict);
+ }
+ }
else {
const char *name = PyModule_GetName(self);
if (name)