diff Python/pythonrun.c @ 83607:e39a8f8ceb9f

#17115,17116: Have modules initialize the __package__ and __loader__ attributes to None. The long-term goal is for people to be able to rely on these attributes existing and checking for None to see if they have been set. Since import itself sets these attributes when a loader does not the only instances when the attributes are None are from someone overloading __import__() and not using a loader or someone creating a module from scratch. This patch also unifies module initialization. Before you could have different attributes with default values depending on how the module object was created. Now the only way to not get the same default set of attributes is to circumvent initialization by calling ModuleType.__new__() directly.
author Brett Cannon <brett@python.org>
date Sat, 04 May 2013 13:56:58 -0400
parents 97522b189c79
children 0b34fd75b937
line wrap: on
line diff
--- a/Python/pythonrun.c	Sat May 04 18:07:12 2013 +0300
+++ b/Python/pythonrun.c	Sat May 04 13:56:58 2013 -0400
@@ -866,7 +866,8 @@
      * be set if __main__ gets further initialized later in the startup
      * process.
      */
-    if (PyDict_GetItemString(d, "__loader__") == NULL) {
+    PyObject *loader = PyDict_GetItemString(d, "__loader__");
+    if (loader == NULL || loader == Py_None) {
         PyObject *loader = PyObject_GetAttrString(interp->importlib,
                                                   "BuiltinImporter");
         if (loader == NULL) {