From: <pj...@us...> - 2010-04-11 19:20:47
|
Revision: 7018 http://jython.svn.sourceforge.net/jython/?rev=7018&view=rev Author: pjenvey Date: 2010-04-11 19:20:41 +0000 (Sun, 11 Apr 2010) Log Message: ----------- add missing imp.is_builtin/frozen and load_compiled fixes #1390 Modified Paths: -------------- trunk/jython/Lib/test/test_chdir.py trunk/jython/Lib/test/test_import_jy.py trunk/jython/NEWS trunk/jython/src/org/python/modules/imp.java Modified: trunk/jython/Lib/test/test_chdir.py =================================================================== --- trunk/jython/Lib/test/test_chdir.py 2010-04-11 17:37:29 UTC (rev 7017) +++ trunk/jython/Lib/test/test_chdir.py 2010-04-11 19:20:41 UTC (rev 7018) @@ -269,7 +269,14 @@ 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): SYSPATH = ('',) Modified: trunk/jython/Lib/test/test_import_jy.py =================================================================== --- trunk/jython/Lib/test/test_import_jy.py 2010-04-11 17:37:29 UTC (rev 7017) +++ trunk/jython/Lib/test/test_import_jy.py 2010-04-11 19:20:41 UTC (rev 7018) @@ -2,6 +2,7 @@ Made for Jython. """ +from __future__ import with_statement import imp import os import shutil @@ -143,6 +144,24 @@ (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__ Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2010-04-11 17:37:29 UTC (rev 7017) +++ trunk/jython/NEWS 2010-04-11 19:20:41 UTC (rev 7018) @@ -29,6 +29,7 @@ - [ 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 Modified: trunk/jython/src/org/python/modules/imp.java =================================================================== --- trunk/jython/src/org/python/modules/imp.java 2010-04-11 17:37:29 UTC (rev 7017) +++ trunk/jython/src/org/python/modules/imp.java 2010-04-11 19:20:41 UTC (rev 7018) @@ -159,6 +159,25 @@ 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); } @@ -192,13 +211,14 @@ 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); @@ -221,15 +241,7 @@ 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); @@ -262,6 +274,14 @@ 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. * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |