Favicon

Incorrect __import__ calls


Hi,

I have noticed differences in __import__ call arguments between Jython and CPython. See below what module 'name' and 'fromlist' arguments are when several import statements are used. CPython is used as the reference.

$ jython test.py
--------------------------------------------------
statement: import hell.first_circle
  - expected: ('hell.first_circle', None)
  - got:      ('hell.first_circle', ())
--------------------------------------------------
statement: import hell.first_circle as limbo
  - expected: ('hell.first_circle', None)
  - got:      ('hell.first_circle', ('*',))
--------------------------------------------------
statement: from hell.ninth_circle import *
  - expected: ('hell.ninth_circle', ('*',))
  - got:      ('hell.ninth_circle', ('*',))
--------------------------------------------------
statement: from hell.ninth_circle import antaeus
  - expected: ('hell.ninth_circle', ('antaeus',))
  - got:      ('hell.ninth_circle', ('antaeus',))
--------------------------------------------------
statement: from hell.ninth_circle import antaeus as giant
  - expected: ('hell.ninth_circle', ('antaeus',))
  - got:      ('hell.ninth_circle', ('antaeus',))

*** FAILURE: 2 errors ***
The first failure is minor: () is used instead of None to denote an empty fromlist.

The second one is a bigger problem IMHO: it sends the wrong signal that all symbols from hell.first_circle should be imported when only the module is searched for. While this is not an issue with the standard __builtin__.__import__ function, it  may be a pain when __import__ is overriden -- that's how I discovered the issue in the first place. I've produced a modified imp.java file that should address this second issue (but not the first, empty tuples are still used, so there are still 2 errors even with the patch), see attached diff.txt

Can anyone review/comment this patch and tell me if it breaks something ? Shall I go on and replace '()' with 'None' as an empty fromlist argument to fully conform to CPython ? File a bug report and assign the issue to anyone ?

Cheers,

SB

Attachment (test.py): text/x-python, 1157 bytes
Index: src/org/python/core/imp.java
===================================================================
--- src/org/python/core/imp.java	(révision 5349)
+++ src/org/python/core/imp.java	(copie de travail)
@@ -747,8 +747,12 @@
      */
     public static PyObject importOneAs(String mod, PyFrame frame) {
         PyObject module = __builtin__.__import__(mod, frame.f_globals, frame
-                .getLocals(), getStarArg());
+                .getLocals(), Py.EmptyTuple);
         // frame.setlocal(asname, module);
+        String[] parts = mod.split("\\.");
+        for (int i=1; i < parts.length; i++) {
+            module = module.__getattr__(parts[i]);
+        }
         return module;
     }

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Jython-dev mailing list
Jython-dev <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-dev

Gmane