]> granicus.if.org Git - python/commitdiff
- Fix segfault with invalid coding.
authorNeal Norwitz <nnorwitz@gmail.com>
Sun, 2 Oct 2005 01:48:49 +0000 (01:48 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sun, 2 Oct 2005 01:48:49 +0000 (01:48 +0000)
- SF Bug #772896, unknown encoding results in MemoryError, which is not helpful

I will only backport the segfault fix.  I'll let Anthony decide if he wants
the other changes backported.  I will do the backport if asked.

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

diff --git a/Lib/test/bad_coding.py b/Lib/test/bad_coding.py
new file mode 100644 (file)
index 0000000..971b0a8
--- /dev/null
@@ -0,0 +1 @@
+# -*- coding: uft-8 -*-
diff --git a/Lib/test/test_coding.py b/Lib/test/test_coding.py
new file mode 100644 (file)
index 0000000..aa7241d
--- /dev/null
@@ -0,0 +1,21 @@
+
+import test.test_support, unittest
+import os
+
+class CodingTest(unittest.TestCase):
+    def test_bad_coding(self):
+        module_name = 'bad_coding'
+        self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
+
+        path = os.path.dirname(__file__)
+        filename = os.path.join(path, module_name + '.py')
+        fp = open(filename)
+        text = fp.read()
+        fp.close()
+        self.assertRaises(SyntaxError, compile, text, filename, 'exec')
+
+def test_main():
+    test.test_support.run_unittest(CodingTest)
+
+if __name__ == "__main__":
+    test_main()
index 2c58dae2a8f474e1885cf96c4d7749d1e71f2c63..bf40e9de0a5026274c18f4dcc9be2e4a9f34be4a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.5 alpha 1?
 Core and builtins
 -----------------
 
+- Fix segfault with invalid coding.
+
+- SF bug #772896: unknown encoding results in MemoryError.
+
 - All iterators now have a Boolean value of true.  Formerly, some iterators
   supported a __len__() method which evaluated to False when the iterator
   was empty.
index 1fa9739308c4597279fe26e3d76f92286b8cb2fa..1d25437f4cb2f005632c8363902113185eb32865 100644 (file)
@@ -42,7 +42,7 @@ PyParser_ParseStringFlagsFilename(const char *s, const char *filename,
        initerr(err_ret, filename);
 
        if ((tok = PyTokenizer_FromString(s)) == NULL) {
-               err_ret->error = E_NOMEM;
+               err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
                return NULL;
        }
 
index 64485eb9dfe5e84b69cb080c13c6c6d6585a31d4..695e2b78802c1dbf08f8740435592c97a133bf79 100644 (file)
@@ -116,6 +116,13 @@ getgrammar(char *filename)
        return g;
 }
 
+/* Can't happen in pgen */
+PyObject*
+PyErr_Occurred()
+{
+       return 0;
+}
+
 void
 Py_FatalError(const char *msg)
 {
index 6957cc95932fdde5928f1b610e6176e203f33f1b..ce613228dc8a5906ee72d5fc9fd59cb498fec9d7 100644 (file)
@@ -603,8 +603,11 @@ decode_str(const char *str, struct tok_state *tok)
        if (tok->enc != NULL) {
                assert(utf8 == NULL);
                utf8 = translate_into_utf8(str, tok->enc);
-               if (utf8 == NULL)
+               if (utf8 == NULL) {
+                       PyErr_Format(PyExc_SyntaxError,
+                               "unknown encoding: %s", tok->enc);
                        return NULL;
+               }
                str = PyString_AsString(utf8);
        }
 #endif
index 68948fc34cd945fceed357e777664a33f0b5cdcf..e007f98a9bfb3954553f2f08b1775fa95e576312 100644 (file)
@@ -1487,7 +1487,7 @@ err_input(perrdetail *err)
                        msg = "unknown decode error";
                Py_DECREF(type);
                Py_DECREF(value);
-               Py_DECREF(tb);
+               Py_XDECREF(tb);
                break;
        }
        case E_LINECONT: