[Jython-checkins] jython: Emit warning or raise exception for nonexistent parent module

jim.baker jython-checkins at python.org
Wed Dec 24 18:36:00 CET 2014


https://hg.python.org/jython/rev/8d6bf085fbeb
changeset:   7469:8d6bf085fbeb
user:        Jim Baker <jim.baker at rackspace.com>
date:        Wed Dec 24 10:35:40 2014 -0700
summary:
  Emit warning or raise exception for nonexistent parent module

Python expects __package__, when set, to correspond to the parent
module; this is then used to support relative imports. Ordinarily this
setting is done as part of the import process, however, it is possible
for user code to change or set, including to a nonexistent module. If
the parent doesn't exists, we follow what was done for
http://bugs.python.org/issue3221:

* For absolute imports, emit a RuntimeWarning, since it's possible
  that __package__ is being used for other purposes in code written
  prior to Python 2.6

* For relative imports, raise a SystemError

Ports the corresponding fix from bug 3221 in the get_parent function
in import.c

files:
  src/org/python/core/imp.java |  15 +++++++++++++++
  1 files changed, 15 insertions(+), 0 deletions(-)


diff --git a/src/org/python/core/imp.java b/src/org/python/core/imp.java
--- a/src/org/python/core/imp.java
+++ b/src/org/python/core/imp.java
@@ -676,6 +676,8 @@
 	 */
     private static String get_parent(PyObject dict, int level) {
         String modname;
+        int orig_level = level;
+
         if ((dict == null && level == -1) || level == 0) {
         	// try an absolute import
             return null;
@@ -726,6 +728,19 @@
             }
             modname = modname.substring(0, dot);
         }
+
+        if (Py.getSystemState().modules.__finditem__(modname) == null) {
+            if (orig_level < 1) {
+                Py.warning(Py.RuntimeWarning,
+                        String.format(
+                                "Parent module '%.200s' not found " +
+                                "while handling absolute import", modname));
+            } else {
+                throw Py.SystemError(String.format(
+                        "Parent module '%.200s' not loaded, " +
+                        "cannot perform relative import", modname));
+            }
+        }
         return modname.intern();
     }
 

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list