[Jython-checkins] jython: Applied patch from #2228 and extended it to all places where filename was

stefan.richthofer jython-checkins at python.org
Thu Jan 19 22:58:29 EST 2017


https://hg.python.org/jython/rev/7d7923408952
changeset:   8008:7d7923408952
user:        Stefan Richthofer <stefan.richthofer at gmx.de>
date:        Fri Jan 20 04:57:38 2017 +0100
summary:
  Applied patch from #2228 and extended it to all places where filename was manually changed to .class-postfix.

files:
  ACKNOWLEDGMENTS                           |   1 +
  Lib/compileall.py                         |   2 +-
  Lib/distutils/util.py                     |   6 +-
  Lib/imp.py                                |   4 +-
  Lib/zipfile.py                            |   6 +-
  NEWS                                      |   1 +
  src/org/python/core/imp.java              |   4 +-
  src/org/python/core/util/importer.java    |   5 +-
  src/org/python/modules/_imp.java          |  32 +++++-----
  src/org/python/util/JycompileAntTask.java |  10 ++-
  10 files changed, 42 insertions(+), 29 deletions(-)


diff --git a/ACKNOWLEDGMENTS b/ACKNOWLEDGMENTS
--- a/ACKNOWLEDGMENTS
+++ b/ACKNOWLEDGMENTS
@@ -177,6 +177,7 @@
     Doug Clayton
     Carl Wicklow
     James Mudd
+    Mat Booth
 
 Local Variables:
 mode: indented-text
diff --git a/Lib/compileall.py b/Lib/compileall.py
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -86,7 +86,7 @@
                 try:
                     mtime = int(os.stat(fullname).st_mtime)
                     expect = struct.pack('<4sl', imp.get_magic(), mtime)
-                    cfile = fullname.replace('.py', '$py.class')
+                    cfile = imp._makeCompiledFilename(fullname)
                     with open(cfile, 'rb') as chandle:
                         actual = chandle.read(8)
                     if expect == actual:
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -534,7 +534,7 @@
     # mode simply calls 'byte_compile()' in direct mode, a weird sort of
     # cross-process recursion.  Hey, it works!
     else:
-        from py_compile import compile
+        import py_compile
 
         for file in py_files:
             if file[-3:] != ".py":
@@ -546,7 +546,7 @@
             #   cfile - byte-compiled file
             #   dfile - purported source filename (same as 'file' by default)
             if sys.platform.startswith('java'):
-                cfile = file[:-3] + '$py.class'
+                cfile = py_compile.imp._makeCompiledFilename(file)
             else:
                 cfile = file + (__debug__ and "c" or "o")
             dfile = file
@@ -564,7 +564,7 @@
                 if force or newer(file, cfile):
                     log.info("byte-compiling %s to %s", file, cfile_base)
                     if not dry_run:
-                        compile(file, cfile, dfile)
+                        py_compile.compile(file, cfile, dfile)
                 else:
                     log.debug("skipping byte-compilation of %s to %s",
                               file, cfile_base)
diff --git a/Lib/imp.py b/Lib/imp.py
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -4,7 +4,8 @@
                   __doc__, acquire_lock, find_module, getClass, get_magic, get_suffixes,
                   is_builtin, is_frozen,
                   load_compiled, load_dynamic, load_module, load_source,
-                  lock_held, new_module, release_lock, reload)
+                  lock_held, new_module, release_lock, reload,
+                  makeCompiledFilename as _makeCompiledFilename)
 
 
 class NullImporter(object):
@@ -15,4 +16,3 @@
 
     def find_module(self, fullname, path=None):
         return None
-
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1425,7 +1425,11 @@
         /python/lib/string, return (/python/lib/string.pyc, string).
         """
         file_py  = pathname + ".py"
-        file_pyc = pathname + (".pyc" if not _is_jython else "$py.class")
+        if _is_jython:
+            import imp
+            file_pyc = imp._makeCompiledFilename(file_py)
+        else:
+            file_pyc = pathname + ".pyc"
         file_pyo = pathname + ".pyo"
         if os.path.isfile(file_pyo) and \
                             os.stat(file_pyo).st_mtime >= os.stat(file_py).st_mtime:
diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@
 
 Jython 2.7.1rc1
   Bugs fixed
+    - [ 2228 ] Re-use "makeCompiledFilename" function where possible
     - [ 608632 ] __doc__foo should accept java String
     - [ 2523 ] defaultdict.__getitem__() does not propagate exceptions raised by calling
                default_factory
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
@@ -602,7 +602,7 @@
     static PyObject loadFromSource(PySystemState sys, String name, String modName, String entry) {
         String dirName = sys.getPath(entry);
         String sourceName = "__init__.py";
-        String compiledName = "__init__$py.class";
+        String compiledName = makeCompiledFilename(sourceName);
         // display names are for identification purposes (e.g. __file__): when entry is
         // null it forces java.io.File to be a relative path (e.g. foo/bar.py instead of
         // /tmp/foo/bar.py)
@@ -633,7 +633,7 @@
         if (!pkg) {
             Py.writeDebug(IMPORT_LOG, "trying source " + dir.getPath());
             sourceName = name + ".py";
-            compiledName = name + "$py.class";
+            compiledName = makeCompiledFilename(sourceName);
             displaySourceName = new File(displayDirName, sourceName).getPath();
             displayCompiledName = new File(displayDirName, compiledName).getPath();
             sourceFile = new File(dirName, sourceName);
diff --git a/src/org/python/core/util/importer.java b/src/org/python/core/util/importer.java
--- a/src/org/python/core/util/importer.java
+++ b/src/org/python/core/util/importer.java
@@ -80,10 +80,11 @@
     protected abstract Bundle makeBundle(String filenameAndSuffix, T entry);
 
     private SearchOrderEntry[] makeSearchOrder(){
+        String initName = "__init__.py";
         return new SearchOrderEntry[] {
-            new SearchOrderEntry(getSeparator() + "__init__$py.class",
+            new SearchOrderEntry(getSeparator() + imp.makeCompiledFilename(initName),
                                  EnumSet.of(EntryType.IS_PACKAGE, EntryType.IS_BYTECODE)),
-            new SearchOrderEntry(getSeparator() + "__init__.py",
+            new SearchOrderEntry(getSeparator() + initName,
                                  EnumSet.of(EntryType.IS_PACKAGE, EntryType.IS_SOURCE)),
             new SearchOrderEntry("$py.class", EnumSet.of(EntryType.IS_BYTECODE)),
             new SearchOrderEntry(".py", EnumSet.of(EntryType.IS_SOURCE)),};
diff --git a/src/org/python/modules/_imp.java b/src/org/python/modules/_imp.java
--- a/src/org/python/modules/_imp.java
+++ b/src/org/python/modules/_imp.java
@@ -2,6 +2,7 @@
 package org.python.modules;
 
 import org.python.core.__builtin__;
+import org.python.core.imp;
 import org.python.core.Py;
 import org.python.core.PyFile;
 import org.python.core.PyList;
@@ -60,7 +61,7 @@
     }
 
     private static boolean caseok(File file, String filename) {
-        return org.python.core.imp.caseok(file, filename);
+        return imp.caseok(file, filename);
     }
 
     /**
@@ -74,7 +75,7 @@
     static ModuleInfo findFromSource(String name, String entry, boolean findingPackage,
                                      boolean preferSource) {
         String sourceName = "__init__.py";
-        String compiledName = "__init__$py.class";
+        String compiledName = makeCompiledFilename(sourceName);
         String directoryName = PySystemState.getPathLazy(entry);
         // displayDirName is for identification purposes: when null it
         // forces java.io.File to be a relative path (e.g. foo/bar.py
@@ -96,7 +97,7 @@
             } else {
                 Py.writeDebug("import", "trying source " + dir.getPath());
                 sourceName = name + ".py";
-                compiledName = name + "$py.class";
+                compiledName = makeCompiledFilename(sourceName);
                 sourceFile = new File(directoryName, sourceName);
                 compiledFile = new File(directoryName, compiledName);
             }
@@ -152,8 +153,8 @@
         }
         PySystemState sys = Py.getSystemState();
         String compiledFilename =
-                org.python.core.imp.makeCompiledFilename(sys.getPath(filename));
-        mod = org.python.core.imp.createFromSource(modname.intern(), (InputStream)o,
+                makeCompiledFilename(sys.getPath(filename));
+        mod = imp.createFromSource(modname.intern(), (InputStream)o,
                                                    filename, compiledFilename);
         PyObject modules = sys.modules;
         modules.__setitem__(modname.intern(), mod);
@@ -180,7 +181,7 @@
         if (sourceName.endsWith("$py.class")) {
             sourceName = sourceName.substring(0, sourceName.length() - 9) + ".py";
         }
-        return org.python.core.imp.loadFromCompiled(name.intern(), stream, sourceName, pathname);
+        return imp.loadFromCompiled(name.intern(), stream, sourceName, pathname);
     }
 
     public static PyObject find_module(String name) {
@@ -226,7 +227,7 @@
 
                     // 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);
+                    compiledName = makeCompiledFilename(resolvedFilename);
                     if (name.endsWith(".__init__")) {
                         name = name.substring(0, name.length() - ".__init__".length());
                     } else if (name.equals("__init__")) {
@@ -239,17 +240,14 @@
                         mtime = fp.lastModified();
                     }
 
-                    mod = org.python.core.imp.createFromSource(name.intern(),
-                                                               (InputStream)o,
-                                                               filename.toString(),
-                                                               compiledName,
-                                                               mtime);
+                    mod = imp.createFromSource(name.intern(), (InputStream)o,
+                            filename.toString(), compiledName, mtime);
                     break;
                 case PY_COMPILED:
                     mod = load_compiled(name, filename.toString(), file);
                     break;
                 case PKG_DIRECTORY:
-                    PyModule m = org.python.core.imp.addModule(name);
+                    PyModule m = imp.addModule(name);
                     m.__dict__.__setitem__("__path__", new PyList(new PyObject[] {filename}));
                     m.__dict__.__setitem__("__file__", filename);
                     ModuleInfo mi = findFromSource(name, filename.toString(), true, true);
@@ -266,10 +264,14 @@
         return mod;
     }
 
+    public static String makeCompiledFilename(String filename) {
+        return imp.makeCompiledFilename(filename);
+    }
+
     public static PyObject get_magic() {
-	return new PyString("\u0003\u00f3\r\n");
+        return new PyString("\u0003\u00f3\r\n");
     }
-    
+
     public static PyObject get_suffixes() {
         return new PyList(new PyObject[] {new PyTuple(new PyString(".py"),
                                                       new PyString("r"),
diff --git a/src/org/python/util/JycompileAntTask.java b/src/org/python/util/JycompileAntTask.java
--- a/src/org/python/util/JycompileAntTask.java
+++ b/src/org/python/util/JycompileAntTask.java
@@ -33,9 +33,13 @@
                 String name = _py_compile.getModuleName(src);
                 String compiledFilePath = name.replace('.', '/');
                 if (src.getName().endsWith("__init__.py")) {
-                    compiledFilePath += "/__init__";
+                    compiledFilePath += "/__init__.py";
+                } else {
+                    compiledFilePath += ".py";
+                    // so we can apply imp.makeCompiledFilename
                 }
-                File compiled = new File(destDir, compiledFilePath + "$py.class");
+                File compiled = new File(destDir,
+                        imp.makeCompiledFilename(compiledFilePath));
                 compile(src, compiled, name);
             } catch (RuntimeException e) {
                 log("Could not compile " + src);
@@ -69,6 +73,6 @@
     }
 
     protected String getTo() {
-        return "*$py.class";
+        return imp.makeCompiledFilename(getFrom());
     }
 }

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


More information about the Jython-checkins mailing list