Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,23 @@ def testGetData(self):
z.close()
os.remove(TEMP_ZIP)

def test_issue31291(self):
# There shouldn't be an assertion failure in get_data().
class FunnyStr(str):
def replace(self, old, new):
return 42
z = ZipFile(TEMP_ZIP, "w")
try:
name = "test31291.dat"
data = b'foo'
z.writestr(name, data)
z.close()
zi = zipimport.zipimporter(TEMP_ZIP)
self.assertEqual(data, zi.get_data(FunnyStr(name)))
finally:
z.close()
os.remove(TEMP_ZIP)

def testImporterAttr(self):
src = """if 1: # indent hack
def get_file():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an assertion failure in `zipimport.zipimporter.get_data` on Windows,
when the return value of ``pathname.replace('/','\\')`` isn't a string.
Patch by Oren Milman.
3 changes: 2 additions & 1 deletion Modules/zipimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,8 @@ zipimport_zipimporter_get_data_impl(ZipImporter *self, PyObject *path)
Py_ssize_t path_start, path_len, len;

#ifdef ALTSEP
path = _PyObject_CallMethodId(path, &PyId_replace, "CC", ALTSEP, SEP);
path = _PyObject_CallMethodId((PyObject *)&PyUnicode_Type, &PyId_replace,
"OCC", path, ALTSEP, SEP);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this.
would it be better to simply check whether the return value of pathname.replace('/', '\') is a str?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current solution LGTM.

if (!path)
return NULL;
#else
Expand Down