1. python_mirrors
  2. jython-fullhistory

Commits

Philip Jenvey  committed e989237

add missing imp.is_builtin/frozen and load_compiled
fixes #1390

  • Participants
  • Parent commits b656274
  • Branches default

Comments (0)

Files changed (4)

File Lib/test/test_chdir.py

View file
  • Ignore whitespace
         self.assertEqual(mod.__file__, self.basename1)
         self.assert_(os.path.exists(self.bytecode))
 
+    def test_imp_load_compiled(self):
+        __import__(self.mod_name)
+        self.assertTrue(os.path.exists(self.bytecode))
+        basename = os.path.basename(self.bytecode)
+        mod = imp.load_compiled(self.mod_name, basename)
+        self.assertEqual(mod.__file__, basename)
+
 
 class ImportPackageTestCase(BaseChdirTestCase):
 

File Lib/test/test_import_jy.py

View file
  • Ignore whitespace
 
 Made for Jython.
 """
+from __future__ import with_statement
 import imp
 import os
 import shutil
                          (None, '__builtin__', ('', '', 6)))
         self.assertEqual(imp.find_module('imp'), (None, 'imp', ('', '', 6)))
 
+    def test_imp_is_builtin(self):
+        self.assertTrue(all(imp.is_builtin(mod)
+                            for mod in ['sys', '__builtin__', 'imp']))
+        self.assertFalse(imp.is_builtin('os'))
+
+    def test_load_compiled(self):
+        compiled = os.__file__
+        if compiled.endswith('.py'):
+            compiled = compiled[:-3] + COMPILED_SUFFIX
+
+        os.__doc__ = 'foo'
+        self.assertEqual(os, imp.load_compiled("os", compiled))
+        self.assertFalse(os.__doc__ == 'foo')
+        with open(compiled, 'rb') as fp:
+            os.__doc__ = 'foo'
+            self.assertEqual(os, imp.load_compiled("os", compiled, fp))
+            self.assertFalse(os.__doc__ == 'foo')
+
     def test_getattr_module(self):
         '''Replacing __getattr__ in a class shouldn't lead to calls to __getitem__
 

File NEWS

View file
  • Ignore whitespace
     - [ 1582 ] com.ziclix.python.sql.PyConnection leaks memory
     - [ 1520 ] os.listdir doesn't return unicode when requested
     - [ 1483 ] optparse std module dies on non-ASCII unicode data
+    - [ 1390 ] ihooks fails due to unimplemented methods in imp module
     - Fix runtime issues during exitfuncs triggered via SystemRestart (such as
       during Django or Pylons development mode reloading)
     - Fix pickling of collections.defaultdict objects

File src/org/python/modules/imp.java

View file
  • Ignore whitespace
         return mod;
     }
 
+    public static PyObject load_compiled(String name, String pathname) {
+        return load_compiled(name, pathname, new PyFile(pathname, "rb", -1));
+    }
+
+    public static PyObject load_compiled(String name, String pathname, PyObject file) {
+        InputStream stream = (InputStream) file.__tojava__(InputStream.class);
+        if (stream == Py.NoConversion) {
+            throw Py.TypeError("must be a file-like object");
+        }
+
+        // XXX: Ideally we wouldn't care about sourceName here (see
+        // http://bugs.jython.org/issue1605847 msg3805)
+        String sourceName = pathname;
+        if (sourceName.endsWith("$py.class")) {
+            sourceName = sourceName.substring(0, sourceName.length() - 9) + ".py";
+        }
+        return org.python.core.imp.loadFromCompiled(name.intern(), stream, sourceName, pathname);
+    }
+
     public static PyObject find_module(String name) {
         return find_module(name, Py.None);
     }
         PySystemState sys = Py.getSystemState();
         int type = data.__getitem__(2).asInt();
         while(mod == Py.None) {
-            Object o = file.__tojava__(InputStream.class);
-            if (o == Py.NoConversion) {
-                throw Py.TypeError("must be a file-like object");
-            }
             String compiledName;
             switch (type) {
                 case PY_SOURCE:
+                    Object o = file.__tojava__(InputStream.class);
+                    if (o == Py.NoConversion) {
+                        throw Py.TypeError("must be a file-like object");
+                    }
+
                     // XXX: This should load the accompanying byte code file instead, if it exists
                     String resolvedFilename = sys.getPath(filename.toString());
                     compiledName = org.python.core.imp.makeCompiledFilename(resolvedFilename);
                                                                mtime);
                     break;
                 case PY_COMPILED:
-                    compiledName = filename.toString();
-                    // XXX: Ideally we wouldn't care about sourceName here (see
-                    // http://bugs.jython.org/issue1605847 msg3805)
-                    String sourceName = compiledName;
-                    if (compiledName.endsWith("$py.class")) {
-                        sourceName = compiledName.substring(0, compiledName.length() - 9) + ".py";
-                    }
-                    mod = org.python.core.imp.loadFromCompiled(
-                        name.intern(), (InputStream)o, sourceName, filename.toString());
+                    mod = load_compiled(name, filename.toString(), file);
                     break;
                 case PKG_DIRECTORY:
                     PyModule m = org.python.core.imp.addModule(name);
         return new PyModule(name, null);
     }
 
+    public static boolean is_builtin(String name) {
+        return PySystemState.getBuiltin(name) != null;
+    }
+
+    public static boolean is_frozen(String name) {
+        return false;
+    }
+
     /**
      * Acquires the interpreter's import lock for the current thread.
      *