Issue #9319: Fix a crash on parsing a Python source code without encoding
authorVictor Stinner <victor.stinner@haypocalc.com>
Fri, 22 Apr 2011 22:41:19 +0000 (00:41 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Fri, 22 Apr 2011 22:41:19 +0000 (00:41 +0200)
cookie and not valid in UTF-8: use "<file>" as the filename instead of
reading from NULL.

Lib/test/test_imp.py
Parser/tokenizer.c

index d745ae9cc30687dde2ea9770b26c77d879f7d66e..6f5b06aa1b180d97f0aaa3a6229b626d1136484b 100644 (file)
@@ -170,6 +170,9 @@ class ImportTests(unittest.TestCase):
                 support.unlink(init_file_name + ext)
             support.rmtree(test_package_name)
 
+    def test_issue9319(self):
+        imp.find_module("test/badsyntax_pep3120")
+
 
 class ReloadTests(unittest.TestCase):
 
index 3f6be2f640eca7a3ecccbaa4c2878367a9b0309c..5ba12a409d4e8f8cd65dfc73ec4be5e6eb2b977a 100644 (file)
@@ -586,7 +586,10 @@ decoding_fgets(char *s, int size, struct tok_state *tok)
     if (badchar) {
         /* Need to add 1 to the line number, since this line
            has not been counted, yet.  */
-        filename = PyUnicode_DecodeFSDefault(tok->filename);
+        if (tok->filename != NULL)
+            filename = PyUnicode_DecodeFSDefault(tok->filename);
+        else
+            filename = PyUnicode_FromString("<file>");
         if (filename != NULL) {
             PyErr_Format(PyExc_SyntaxError,
                     "Non-UTF-8 code starting with '\\x%.2x' "