]> granicus.if.org Git - python/commitdiff
Fix Bug #1378022, UTF-8 files with a leading BOM crashed the interpreter.
authorNeal Norwitz <nnorwitz@gmail.com>
Sun, 18 Dec 2005 05:29:30 +0000 (05:29 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sun, 18 Dec 2005 05:29:30 +0000 (05:29 +0000)
Needs backport.

Lib/test/bad_coding2.py [new file with mode: 0644]
Lib/test/test_coding.py
Misc/NEWS
Parser/tokenizer.c
Python/pythonrun.c

diff --git a/Lib/test/bad_coding2.py b/Lib/test/bad_coding2.py
new file mode 100644 (file)
index 0000000..604b122
--- /dev/null
@@ -0,0 +1,2 @@
+#coding: utf8\r
+print '我'
\ No newline at end of file
index aa7241d947045eba2e7ed6b734ad28c520a80be6..e83015e543128292fc5b35068830a3d216cb45f5 100644 (file)
@@ -5,6 +5,13 @@ import os
 class CodingTest(unittest.TestCase):
     def test_bad_coding(self):
         module_name = 'bad_coding'
+        self.verify_bad_module(module_name)
+
+    def test_bad_coding2(self):
+        module_name = 'bad_coding2'
+        self.verify_bad_module(module_name)
+
+    def verify_bad_module(self, module_name):
         self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
 
         path = os.path.dirname(__file__)
index 1db35f44b90d33b510c4a364608a1828d1bbeb34..7b27e49cb00acb987c754702dfa97d9708bf8530 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.5 alpha 1?
 Core and builtins
 -----------------
 
+- Bug #1378022, UTF-8 files with a leading BOM crashed the interpreter.
+
 - Support for converting hex strings to floats no longer works.
   This was not portable.  float('0x3') now raises a ValueError.
 
index 37e6c3349bac6d48917b64dc77574e7a95f81fad..a79ea811294f36b6846e6ab820ef33fad7ffed16 100644 (file)
@@ -292,6 +292,12 @@ check_coding_spec(const char* line, int size, struct tok_state *tok,
                        PyMem_DEL(cs);
                }
        }
+       if (!r) {
+               cs = tok->encoding;
+               if (!cs)
+                       cs = "with BOM";
+               PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
+       }
        return r;
 }
 
index a7f4fe7ceed801f65e36c84dd94dfdfe2db55159..30cb518a65dc9c321a9381383a4be7a11c6745d1 100644 (file)
@@ -1439,8 +1439,8 @@ err_input(perrdetail *err)
                }
                if (msg == NULL)
                        msg = "unknown decode error";
-               Py_DECREF(type);
-               Py_DECREF(value);
+               Py_XDECREF(type);
+               Py_XDECREF(value);
                Py_XDECREF(tb);
                break;
        }