]> granicus.if.org Git - python/commitdiff
Issue #26754: PyUnicode_FSDecoder() accepted a filename argument encoded as
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 18 Jun 2016 10:53:36 +0000 (13:53 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 18 Jun 2016 10:53:36 +0000 (13:53 +0300)
an iterable of integers. Now only strings and byte-like objects are accepted.

Lib/test/test_compile.py
Lib/test/test_parser.py
Lib/test/test_symtable.py
Lib/test/test_zipimport.py
Misc/NEWS
Objects/unicodeobject.c

index e0fdee3349893e7bba6d078e68467810b7766277..824e843914c1231a5c2ae23f1337b217bab8e3ad 100644 (file)
@@ -472,6 +472,13 @@ if 1:
         d = {f(): f(), f(): f()}
         self.assertEqual(d, {1: 2, 3: 4})
 
+    def test_compile_filename(self):
+        for filename in ('file.py', b'file.py',
+                         bytearray(b'file.py'), memoryview(b'file.py')):
+            code = compile('pass', filename, 'exec')
+            self.assertEqual(code.co_filename, 'file.py')
+        self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
+
     @support.cpython_only
     def test_same_filename_used(self):
         s = """def f(): pass\ndef g(): pass"""
index 3d301b4924f5c3c5fed533ee416ba269109fb864..ab6577f44d7e15303419ff2940cd1e83eb4364a6 100644 (file)
@@ -627,6 +627,22 @@ class CompileTestCase(unittest.TestCase):
         code2 = parser.compilest(st)
         self.assertEqual(eval(code2), -3)
 
+    def test_compile_filename(self):
+        st = parser.expr('a + 5')
+        code = parser.compilest(st)
+        self.assertEqual(code.co_filename, '<syntax-tree>')
+        code = st.compile()
+        self.assertEqual(code.co_filename, '<syntax-tree>')
+        for filename in ('file.py', b'file.py',
+                         bytearray(b'file.py'), memoryview(b'file.py')):
+            code = parser.compilest(st, filename)
+            self.assertEqual(code.co_filename, 'file.py')
+            code = st.compile(filename)
+            self.assertEqual(code.co_filename, 'file.py')
+        self.assertRaises(TypeError, parser.compilest, st, list(b'file.py'))
+        self.assertRaises(TypeError, st.compile, list(b'file.py'))
+
+
 class ParserStackLimitTestCase(unittest.TestCase):
     """try to push the parser to/over its limits.
     see http://bugs.python.org/issue1881 for a discussion
index e5e7b83d2148d6556688650a264badb51d0b08d3..c5d7facfdbd1849ef41d6d702c5222268711855c 100644 (file)
@@ -157,6 +157,12 @@ class SymtableTest(unittest.TestCase):
                 self.fail("no SyntaxError for %r" % (brokencode,))
         checkfilename("def f(x): foo)(")  # parse-time
         checkfilename("def f(x): global x")  # symtable-build-time
+        symtable.symtable("pass", b"spam", "exec")
+        with self.assertRaises(TypeError):
+            symtable.symtable("pass", bytearray(b"spam"), "exec")
+        symtable.symtable("pass", memoryview(b"spam"), "exec")
+        with self.assertRaises(TypeError):
+            symtable.symtable("pass", list(b"spam"), "exec")
 
     def test_eval(self):
         symbols = symtable.symtable("42", "?", "eval")
index 0da5906f2833519615c53d5c097d2af3398cd908..8e5e12d4377dc778df40a55c22c5bfd5057b5634 100644 (file)
@@ -600,6 +600,19 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
         finally:
             os.remove(filename)
 
+    def testBytesPath(self):
+        filename = support.TESTFN + ".zip"
+        self.addCleanup(support.unlink, filename)
+        with ZipFile(filename, "w") as z:
+            zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW))
+            zinfo.compress_type = self.compression
+            z.writestr(zinfo, test_src)
+
+        zipimport.zipimporter(filename)
+        zipimport.zipimporter(os.fsencode(filename))
+        zipimport.zipimporter(bytearray(os.fsencode(filename)))
+        zipimport.zipimporter(memoryview(os.fsencode(filename)))
+
 
 @support.requires_zlib
 class CompressedZipImportTestCase(UncompressedZipImportTestCase):
@@ -620,6 +633,8 @@ class BadFileZipImportTestCase(unittest.TestCase):
     def testBadArgs(self):
         self.assertRaises(TypeError, zipimport.zipimporter, None)
         self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None)
+        self.assertRaises(TypeError, zipimport.zipimporter,
+                          list(os.fsencode(TESTMOD)))
 
     def testFilenameTooLong(self):
         self.assertZipFailure('A' * 33000)
index 5dff4dbdf5c70acb99e9fe5c2b2f229c8a242341..3c6d3aed716197244e3adb8739eac4098ce28841 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #26754: Some functions (compile() etc) accepted a filename argument
+  encoded as an iterable of integers. Now only strings and byte-like objects
+  are accepted.
+
 - Issue #27048: Prevents distutils failing on Windows when environment
   variables contain non-ASCII characters
 
@@ -43,6 +47,13 @@ Library
 
 - Issue #26930: Update Windows builds to use OpenSSL 1.0.2h.
 
+C API
+-----
+
+- Issue #26754: PyUnicode_FSDecoder() accepted a filename argument encoded as
+  an iterable of integers. Now only strings and byte-like objects are accepted.
+
+
 What's New in Python 3.5.2 final?
 =================================
 
index f11a0825262b8df7dd1c43b13ef8cfc42437ddad..1fcc83e63a3240e4405109e91e224c3ddcb9a72e 100644 (file)
@@ -3666,7 +3666,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
         output = arg;
         Py_INCREF(output);
     }
-    else {
+    else if (PyObject_CheckBuffer(arg)) {
         arg = PyBytes_FromObject(arg);
         if (!arg)
             return 0;
@@ -3681,6 +3681,12 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
             return 0;
         }
     }
+    else {
+        PyErr_Format(PyExc_TypeError,
+                     "path should be string or bytes, not %.200s",
+                     Py_TYPE(arg)->tp_name);
+        return 0;
+    }
     if (PyUnicode_READY(output) == -1) {
         Py_DECREF(output);
         return 0;