Skip to content

Commit da9aacb

Browse files
committed
o fix recompiling due to stale byte code not tagging an mtime on the new code
o change importer isAcceptableBytecode -> getSourceMtime to fix zipimporter to check mtime against the actual bytecode's tagged mtime
1 parent 5b67ee1 commit da9aacb

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

src/org/python/core/ClasspathPyImporter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Copyright (c) Jython Developers */
12
package org.python.core;
23

34
import java.io.IOException;
@@ -63,8 +64,9 @@ final PyObject ClasspathPyImporter_load_module(String fullname) {
6364
}
6465

6566
@Override
66-
protected boolean isAcceptableBytecode(String searchPath, String entry) {
67-
return true;
67+
protected long getSourceMtime(String path) {
68+
// Can't determine this easily
69+
return -1;
6870
}
6971

7072
@Override

src/org/python/core/imp.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,12 @@ static PyObject loadFromSource(PySystemState sys, String name, String modName, S
474474

475475
boolean pkg = false;
476476
try {
477-
pkg = dir.isDirectory() && caseok(dir, name) && (sourceFile.isFile()
478-
|| compiledFile.isFile());
477+
pkg = dir.isDirectory() && caseok(dir, name)
478+
&& (sourceFile.isFile() || compiledFile.isFile());
479479
} catch (SecurityException e) {
480+
// ok
480481
}
482+
481483
if (!pkg) {
482484
Py.writeDebug(IMPORT_LOG, "trying source " + dir.getPath());
483485
sourceName = name + ".py";
@@ -506,7 +508,7 @@ static PyObject loadFromSource(PySystemState sys, String name, String modName, S
506508
}
507509
}
508510
return createFromSource(modName, makeStream(sourceFile), displaySourceName,
509-
compiledFile.getPath());
511+
compiledFile.getPath(), pyTime);
510512
}
511513
return createFromSource(modName, makeStream(sourceFile), displaySourceName,
512514
compiledFile.getPath(), pyTime);
@@ -519,6 +521,7 @@ static PyObject loadFromSource(PySystemState sys, String name, String modName, S
519521
displayCompiledName);
520522
}
521523
} catch (SecurityException e) {
524+
// ok
522525
}
523526
return null;
524527
}

src/org/python/core/util/importer.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* Copyright (c) Jython Developers */
12
package org.python.core.util;
23

34
import java.io.IOException;
@@ -127,7 +128,14 @@ public Bundle(InputStream inputStream) {
127128
public abstract void close();
128129
}
129130

130-
protected abstract boolean isAcceptableBytecode(String searchPath, T entry);
131+
/**
132+
* Given a path to a compiled file in the archive, return the modification time of the
133+
* matching .py file.
134+
*
135+
* @param path to the compiled file
136+
* @return long mtime of the .py, or -1 if no source is available
137+
*/
138+
protected abstract long getSourceMtime(String path);
131139

132140
/**
133141
* Return module information for the module with the fully qualified name.
@@ -181,20 +189,24 @@ protected final ModuleCodeData getModuleCode(String fullname) {
181189

182190
boolean ispackage = entry.type.contains(EntryType.IS_PACKAGE);
183191
boolean isbytecode = entry.type.contains(EntryType.IS_BYTECODE);
184-
185-
if (isbytecode && !isAcceptableBytecode(searchPath, tocEntry)) {
186-
continue;
192+
long mtime = -1;
193+
if (isbytecode) {
194+
mtime = getSourceMtime(searchPath);
187195
}
188196

189197
Bundle bundle = makeBundle(searchPath, tocEntry);
190198
byte[] codeBytes;
191199
try {
192200
if (isbytecode) {
193201
try {
194-
codeBytes = imp.readCode(fullname, bundle.inputStream, true);
202+
codeBytes = imp.readCode(fullname, bundle.inputStream, true, mtime);
195203
} catch (IOException ioe) {
196204
throw Py.ImportError(ioe.getMessage() + "[path=" + fullSearchPath + "]");
197205
}
206+
if (codeBytes == null) {
207+
// bad magic number or non-matching mtime in byte code, try next
208+
continue;
209+
}
198210
} else {
199211
codeBytes = imp.compileSource(fullname, bundle.inputStream, fullSearchPath);
200212
}

src/org/python/modules/zipimport/zipimporter.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.python.core.ArgParser;
1313
import org.python.core.Py;
1414
import org.python.core.PyDictionary;
15+
import org.python.core.PyException;
1516
import org.python.core.PyInteger;
1617
import org.python.core.PyLong;
1718
import org.python.core.PyObject;
@@ -299,31 +300,27 @@ public ZipBundle makeBundle(String datapath, PyObject entry) {
299300
}
300301
}
301302

302-
/**
303-
* Determine if the byte code at path with the specified toc entry has a modification time
304-
* greater than its accompanying source code's.
305-
*
306-
* @param path a String path to the byte code
307-
* @param tocEntry the byte code's PyObject toc entry
308-
* @return boolean whether or not the byte code is older
309-
*/
310303
@Override
311-
protected boolean isAcceptableBytecode(String path, PyObject tocEntry) {
304+
protected long getSourceMtime(String path) {
312305
String sourcePath = path.substring(0, path.length() - 9) + ".py";
313306
PyObject sourceTocEntry = files.__finditem__(sourcePath);
314307
if (sourceTocEntry == null) {
315-
return true;// If there is no source, assume the bytecode is ok
308+
return -1;
316309
}
310+
311+
int time;
312+
int date;
317313
try {
318-
long bytecodeTime = dosTimeToEpoch(tocEntry.__finditem__(5).asInt(0),
319-
tocEntry.__finditem__(6).asInt(0));
320-
long sourceTime = dosTimeToEpoch(sourceTocEntry.__finditem__(5).asInt(0),
321-
sourceTocEntry.__finditem__(6).asInt(0));
322-
return bytecodeTime < sourceTime;
323-
}
324-
catch (PyObject.ConversionException ce) {
325-
return false;
314+
time = sourceTocEntry.__finditem__(5).asInt();
315+
date = sourceTocEntry.__finditem__(6).asInt();
316+
} catch (PyException pye) {
317+
if (!pye.match(Py.TypeError)) {
318+
throw pye;
319+
}
320+
time = -1;
321+
date = -1;
326322
}
323+
return dosTimeToEpoch(time, date);
327324
}
328325

329326
/**

0 commit comments

Comments
 (0)