diff Objects/moduleobject.c @ 77126:702009f3c0b1

issue 14660: Implement PEP 420, namespace packages.
author Eric V. Smith <eric@trueblade.com>
date Thu, 24 May 2012 20:21:04 -0400
parents eb68502731dd
children 571777bf5527
line wrap: on
line diff
--- a/Objects/moduleobject.c	Thu May 24 21:49:59 2012 +0300
+++ b/Objects/moduleobject.c	Thu May 24 20:21:04 2012 -0400
@@ -366,8 +366,28 @@
 static PyObject *
 module_repr(PyModuleObject *m)
 {
-    PyObject *name, *filename, *repr;
+    PyObject *name, *filename, *repr, *loader = NULL;
 
+    /* See if the module has an __loader__.  If it does, give the loader the
+     * first shot at producing a repr for the module.
+     */
+    if (m->md_dict != NULL) {
+        loader = PyDict_GetItemString(m->md_dict, "__loader__");
+    }
+    if (loader != NULL) {
+        repr = PyObject_CallMethod(loader, "module_repr", "(O)",
+                                   (PyObject *)m, NULL);
+        if (repr == NULL) {
+            PyErr_Clear();
+        }
+        else {
+            return repr;
+        }
+    }
+    /* __loader__.module_repr(m) did not provide us with a repr.  Next, see if
+     * the module has an __file__.  If it doesn't then use repr(__loader__) if
+     * it exists, otherwise, just use module.__name__.
+     */
     name = PyModule_GetNameObject((PyObject *)m);
     if (name == NULL) {
         PyErr_Clear();
@@ -378,8 +398,17 @@
     filename = PyModule_GetFilenameObject((PyObject *)m);
     if (filename == NULL) {
         PyErr_Clear();
-        repr = PyUnicode_FromFormat("<module %R (built-in)>", name);
+        /* There's no m.__file__, so if there was an __loader__, use that in
+         * the repr, otherwise, the only thing you can use is m.__name__
+         */
+        if (loader == NULL) {
+            repr = PyUnicode_FromFormat("<module %R>", name);
+        }
+        else {
+            repr = PyUnicode_FromFormat("<module %R (%R)>", name, loader);
+        }
     }
+    /* Finally, use m.__file__ */
     else {
         repr = PyUnicode_FromFormat("<module %R from %R>", name, filename);
         Py_DECREF(filename);