[Python-checkins] bpo-31291: Fixed an assertion failure in zipimport.zipimporter.get_data() (#3226)

Serhiy Storchaka webhook-mailer at python.org
Tue Aug 29 13:40:18 EDT 2017


https://github.com/python/cpython/commit/631fdee6e61b4ba8ce800f827fecdd536bfb04f3
commit: 631fdee6e61b4ba8ce800f827fecdd536bfb04f3
branch: master
author: Oren Milman <orenmn at gmail.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2017-08-29T20:40:15+03:00
summary:

bpo-31291: Fixed an assertion failure in zipimport.zipimporter.get_data() (#3226)

if pathname.replace('/', '\\') returns non-string.

files:
A Misc/NEWS.d/next/Core and Builtins/2017-08-28-11-51-29.bpo-31291.t8QggK.rst
M Lib/test/test_zipimport.py
M Modules/zipimport.c

diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index 7ddbc509f9b..daa5138751b 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -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():
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-08-28-11-51-29.bpo-31291.t8QggK.rst b/Misc/NEWS.d/next/Core and Builtins/2017-08-28-11-51-29.bpo-31291.t8QggK.rst
new file mode 100644
index 00000000000..0576785c7df
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2017-08-28-11-51-29.bpo-31291.t8QggK.rst	
@@ -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.
diff --git a/Modules/zipimport.c b/Modules/zipimport.c
index c01ee55840b..d710360f95d 100644
--- a/Modules/zipimport.c
+++ b/Modules/zipimport.c
@@ -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);
     if (!path)
         return NULL;
 #else



More information about the Python-checkins mailing list