]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31291: Fixed an assertion failure in zipimport.zipimporter.get_data() ...
authorOren Milman <orenmn@gmail.com>
Wed, 30 Aug 2017 11:08:39 +0000 (14:08 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 30 Aug 2017 11:08:39 +0000 (14:08 +0300)
if pathname.replace('/', '\\') returns non-string.
(cherry picked from commit 631fdee6e61b4ba8ce800f827fecdd536bfb04f3)

Lib/test/test_zipimport.py
Misc/NEWS.d/next/Core and Builtins/2017-08-28-11-51-29.bpo-31291.t8QggK.rst [new file with mode: 0644]
Modules/zipimport.c

index 7ddbc509f9bb332040078b06e946434f8a382705..daa5138751b929548fefe344a535cbd6d395d678 100644 (file)
@@ -521,6 +521,23 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
             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 (file)
index 0000000..0576785
--- /dev/null
@@ -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.
index cccc033d2d9fababac1e3c5e9633a2d7d3fd9523..5bf31cc0605451175e2db70727c30c2f3c2a5be9 100644 (file)
@@ -580,7 +580,8 @@ zipimporter_get_data(PyObject *obj, PyObject *args)
         return NULL;
 
 #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