Skip to content

Commit c60c784

Browse files
committed
[GR-15813] When tregex.jar file is not classpath, is not possible to import a module.
PullRequest: graalpython/518
2 parents ae128dd + 447d031 commit c60c784

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed
Binary file not shown.

graalpython/com.oracle.graal.python.test/src/tests/test_zipimport.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ def get_file():
2525

2626
ZIP_FILE_NAME = 'testzipfile.zip'
2727
EGG_FILE_NAME = 'testeggfile.egg'
28+
GR15813_FILE_NAME = 'testGR15813.zip'
2829
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
2930
ZIP_PATH = os.path.join(DIR_PATH, ZIP_FILE_NAME)
3031
ZIP_ABS_PATH = os.path.abspath(ZIP_PATH);
3132
EGG_PATH = os.path.join(DIR_PATH, EGG_FILE_NAME)
3233
EGG_ABS_PATH = os.path.abspath(EGG_PATH);
34+
GR15813_PATH = os.path.join(DIR_PATH, GR15813_FILE_NAME)
3335

3436
class ZipImportBaseTestCase(unittest.TestCase):
3537

@@ -202,3 +204,15 @@ def test_egg_get_readme(self):
202204
data = self.z.get_data("read.me")
203205
self.assertTrue(type(data) is bytes)
204206
self.assertEqual(bytes(b'Pokus\n'), data)
207+
208+
class GR15813ImportTests(ZipImportBaseTestCase):
209+
210+
# don't edit the zip file !!!
211+
def setUp(self):
212+
ZipImportBaseTestCase.setUp(self)
213+
self.z = zipimport.zipimporter(GR15813_PATH)
214+
215+
def test_zipimporter_gr_18813(self):
216+
self.assertTrue(self.z.prefix == "")
217+
self.assertTrue(type(self.z._files) is dict)
218+
self.assertTrue(6, len(self.z._files))

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,23 @@ public int read() throws IOException {
150150
if (ch == LOC_SIG[1]) {
151151
state = AFTER_PK;
152152
} else {
153-
state = BEFORE_P;
153+
state = ch != LOC_SIG[0] ? BEFORE_P : AFTER_P;
154154
}
155155
break;
156156
case AFTER_PK:
157157
if (ch == LOC_SIG[2]) {
158158
state = AFTER_PK3;
159159
} else {
160-
state = BEFORE_P;
160+
state = ch != LOC_SIG[0] ? BEFORE_P : AFTER_P;
161161
}
162162
break;
163163
case AFTER_PK3:
164164
if (ch == LOC_SIG[3]) {
165165
positions.add(pos - 4); // store the LOC position
166+
state = BEFORE_P;
167+
} else {
168+
state = ch != LOC_SIG[0] ? BEFORE_P : AFTER_P;
166169
}
167-
state = BEFORE_P;
168170
}
169171
return ch;
170172
}
@@ -238,15 +240,19 @@ private void initZipImporter(PZipImporter self, String path) {
238240
long lastZipEntryCSize = 0;
239241
long lastZipEntryPos = 0;
240242
int lastZipLocFileHeaderSize = 0;
241-
long zipEntryPos;
243+
long zipEntryPos = 0;
242244

243245
byte[] extraField;
244246
while ((entry = zis.getNextEntry()) != null) {
245-
zipEntryPos = locis.positions.remove(0);
246-
// handles situation when the local file signature is
247-
// in the content of a file
248-
while (lastZipEntryPos + lastZipEntryCSize + lastZipLocFileHeaderSize > zipEntryPos) {
247+
if (!locis.positions.isEmpty()) {
249248
zipEntryPos = locis.positions.remove(0);
249+
// handles situation when the local file signature is
250+
// in the content of a file
251+
while (lastZipEntryPos + lastZipEntryCSize + lastZipLocFileHeaderSize > zipEntryPos) {
252+
zipEntryPos = locis.positions.remove(0);
253+
}
254+
} else {
255+
throw raise(PythonErrorType.ZipImportError, "cannot handle Zip file: '%s'", archive);
250256
}
251257

252258
PTuple tuple = factory().createTuple(new Object[]{

0 commit comments

Comments
 (0)