[Jython-checkins] jython (2.5): cleanup import fromlist handling

philip.jenvey jython-checkins at python.org
Tue Oct 25 02:17:39 CEST 2011


http://hg.python.org/jython/rev/4c672dbbcdb2
changeset:   6254:4c672dbbcdb2
branch:      2.5
parent:      6245:8831af94a9fc
user:        Philip Jenvey <pjenvey at underboss.org>
date:        Mon Oct 24 17:12:51 2011 -0700
summary:
  cleanup import fromlist handling
fixes #1811
thanks Mike Bayer

files:
  Lib/test/issue1811/__init__.py    |  Bin 
  Lib/test/issue1811/foo.py         |    1 +
  Lib/test/test_import_jy.py        |    6 ++
  src/org/python/core/PyModule.java |    8 ---
  src/org/python/core/imp.java      |   43 ++++++++++++++---
  5 files changed, 41 insertions(+), 17 deletions(-)


diff --git a/Lib/test/issue1811/__init__.py b/Lib/test/issue1811/__init__.py
new file mode 100644
diff --git a/Lib/test/issue1811/foo.py b/Lib/test/issue1811/foo.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/issue1811/foo.py
@@ -0,0 +1,1 @@
+issue1811 = __import__('test.issue1811', globals(), locals(), ['foo'])
diff --git a/Lib/test/test_import_jy.py b/Lib/test/test_import_jy.py
--- a/Lib/test/test_import_jy.py
+++ b/Lib/test/test_import_jy.py
@@ -205,6 +205,12 @@
                 shutil.rmtree(test_support.TESTFN)
                 test_support.unlink(sym)
 
+    def test_issue1811(self):
+        # Previously this blew out the stack
+        from test.issue1811 import foo
+        self.assertTrue(foo.issue1811.foo is foo)
+
+
 def test_main():
     test_support.run_unittest(MislabeledImportTestCase,
                               OverrideBuiltinsImportTestCase,
diff --git a/src/org/python/core/PyModule.java b/src/org/python/core/PyModule.java
--- a/src/org/python/core/PyModule.java
+++ b/src/org/python/core/PyModule.java
@@ -119,14 +119,6 @@
         return null;
     }
 
-    @Override
-    public PyObject __findattr_ex__(String name) {
-        PyObject attr=super.__findattr_ex__(name);
-        if (attr!=null)
-            return attr;
-        return impAttr(name);
-    }
-
     public void __setattr__(String name, PyObject value) {
         module___setattr__(name, value);
     }
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
@@ -844,19 +844,44 @@
         }
 
         if (fromlist != null && fromlist != Py.None) {
-            StringBuilder modNameBuffer = new StringBuilder(name);
-            for (PyObject submodName : fromlist.asIterable()) {
-                if (mod.__findattr__(submodName.toString()) != null
-                    || submodName.toString().equals("*")) {
-                    continue;
-                }
-                String fullName = modNameBuffer.toString() + "." + submodName.toString();
-                import_next(mod, modNameBuffer, submodName.toString(), fullName, null);
-            }
+            ensureFromList(mod, fromlist, name);
         }
         return mod;
     }
 
+    private static void ensureFromList(PyObject mod, PyObject fromlist, String name) {
+        ensureFromList(mod, fromlist, name, false);
+    }
+
+    private static void ensureFromList(PyObject mod, PyObject fromlist, String name,
+                                       boolean recursive) {
+            if (mod.__findattr__("__path__") == null) {
+                return;
+            }
+
+            StringBuilder modNameBuffer = new StringBuilder(name);
+            for (PyObject item : fromlist.asIterable()) {
+                if (!Py.isInstance(item, PyString.TYPE)) {
+                    throw Py.TypeError("Item in ``from list'' not a string");
+                }
+                if (item.toString().equals("*")) {
+                    if (recursive) {
+                        // Avoid endless recursion
+                        continue;
+                    }
+                    PyObject all;
+                    if ((all = mod.__findattr__("__all__")) != null) {
+                        ensureFromList(mod, all, name, true);
+                    }
+                }
+
+                if (mod.__findattr__((PyString) item) == null) {
+                    String fullName = modNameBuffer.toString() + "." + item.toString();
+                    import_next(mod, modNameBuffer, item.toString(), fullName, null);
+                }
+            }
+    }
+
     /**
      * Import a module by name.
      *

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


More information about the Jython-checkins mailing list