Skip to content

Commit e0c0f60

Browse files
committed
Use the path to the imported item as __file__, not the package path
1 parent b26ac42 commit e0c0f60

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

Lib/test/test_classpathimporter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ def tearDown(self):
1818
except KeyError:
1919
pass
2020

21-
def setClassLoaderAndCheck(self, jar):
21+
def setClassLoaderAndCheck(self, jar, prefix):
2222
Thread.currentThread().contextClassLoader = test_support.make_jar_classloader(jar)
2323
import flat_in_jar
2424
self.assertEquals(flat_in_jar.value, 7)
2525
import jar_pkg
26+
self.assertEquals(prefix + '/jar_pkg/__init__$py.class', jar_pkg.__file__)
2627
from jar_pkg import prefer_compiled
28+
self.assertEquals(prefix + '/jar_pkg/prefer_compiled$py.class', prefer_compiled.__file__)
2729
self.assert_(prefer_compiled.compiled)
2830
self.assertRaises(NameError, __import__, 'flat_bad')
2931
self.assertRaises(NameError, __import__, 'jar_pkg.bad')
3032

3133
def test_default_pyclasspath(self):
32-
self.setClassLoaderAndCheck("classimport.jar")
34+
self.setClassLoaderAndCheck("classimport.jar", "__pyclasspath__")
3335

3436
def test_path_in_pyclasspath(self):
3537
sys.path = ['__pyclasspath__/Lib']
36-
self.setClassLoaderAndCheck("classimport_Lib.jar")
38+
self.setClassLoaderAndCheck("classimport_Lib.jar", "__pyclasspath__/Lib")
3739

3840
def test_main():
3941
test_support.run_unittest(ClasspathImporterTestCase)

src/org/python/core/ClasspathPyImporter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ protected String makeFilename(String fullname) {
109109
return path.replace(PYCLASSPATH_PREFIX, "") + fullname.replace('.', '/');
110110
}
111111

112+
@Override
113+
protected String makeFilePath(String fullname) {
114+
return path + fullname.replace('.', '/');
115+
}
116+
112117
@Override
113118
protected String makePackagePath(String fullname) {
114119
return path;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public importer() {
5454
*/
5555
protected abstract String makeFilename(String fullname);
5656

57+
/**
58+
* Given a full module name, return the potential file path including the archive (without
59+
* extension).
60+
*/
61+
protected abstract String makeFilePath(String fullname);
62+
5763
/**
5864
* Returns an entry for a filename from makeFilename with a potential suffix such that this
5965
* importer can make a bundle with it, or null if fullFilename doesn't exist in this importer.
@@ -156,7 +162,7 @@ protected final ModuleInfo getModuleInfo(String fullname) {
156162
*/
157163
protected final ModuleCodeData getModuleCode(String fullname) {
158164
String path = makeFilename(fullname);
159-
String fullPath = makePackagePath(fullname);
165+
String fullPath = makeFilePath(fullname);
160166

161167
if (path.length() < 0) {
162168
return null;
@@ -186,7 +192,7 @@ protected final ModuleCodeData getModuleCode(String fullname) {
186192
try {
187193
codeBytes = imp.readCode(fullname, bundle.inputStream, true);
188194
} catch (IOException ioe) {
189-
throw Py.ImportError(ioe.getMessage() + "[path=" + fullPath + searchPath + "]");
195+
throw Py.ImportError(ioe.getMessage() + "[path=" + fullSearchPath + "]");
190196
}
191197
} else {
192198
codeBytes = imp.compileSource(fullname, bundle.inputStream, fullSearchPath);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ protected String makePackagePath(String fullname) {
422422
return archive + File.separator + prefix + getSubname(fullname);
423423
}
424424

425+
@Override
426+
protected String makeFilePath(String fullname) {
427+
return makePackagePath(fullname);
428+
}
429+
425430
@Override
426431
protected PyObject makeEntry(String fullFilename) {
427432
return files.__finditem__(fullFilename);

0 commit comments

Comments
 (0)