]> granicus.if.org Git - python/commitdiff
#6373: SystemError in str.encode('latin1', 'surrogateescape')
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Mon, 29 Jun 2009 22:36:49 +0000 (22:36 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Mon, 29 Jun 2009 22:36:49 +0000 (22:36 +0000)
if the string contains unpaired surrogates.
(In debug build, crash in assert())

This can happen with normal processing, if python starts with utf-8,
then calls sys.setfilesystemencoding('latin-1')

Lib/test/test_codecs.py
Misc/NEWS
Objects/unicodeobject.c

index 4ec7b5865cc590a426f354e90deb1d4eec4bc36b..e060471d4b0fa439899e9bd7ac865dc5af575fa2 100644 (file)
@@ -1549,6 +1549,11 @@ class SurrogateEscapeTest(unittest.TestCase):
         self.assertEqual("foo\udca5bar".encode("iso-8859-3", "surrogateescape"),
                          b"foo\xa5bar")
 
+    def test_latin1(self):
+        # Issue6373
+        self.assertEqual("\udce4\udceb\udcef\udcf6\udcfc".encode("latin1", "surrogateescape"),
+                         b"\xe4\xeb\xef\xf6\xfc")
+
 
 def test_main():
     support.run_unittest(
index 2381844249c52a337a7a8a493bc6820836385ecd..98539e53173c044d9579c8be2c5910903d47fd76 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #6373: Fixed a RuntimeError when encoding with the latin-1 codec and
+  the 'surrogateescape' error handler, a string which contains unpaired
+  surrogates.
+
 - Issue #4856: Remove checks for win NT.
 
 Library
index 0d4a3ddd806221010ff4daa2e6f10805e43c716d..305289bc78cf3c6db6994240b89017c53b812b7b 100644 (file)
@@ -4201,10 +4201,12 @@ static PyObject *unicode_encode_ucs1(const Py_UNICODE *p,
                     repsize = PyBytes_Size(repunicode);
                     if (repsize > 1) {
                         /* Make room for all additional bytes. */
+                        respos = str - PyBytes_AS_STRING(res);
                         if (_PyBytes_Resize(&res, ressize+repsize-1)) {
                             Py_DECREF(repunicode);
                             goto onError;
                         }
+                        str = PyBytes_AS_STRING(res) + respos;
                         ressize += repsize-1;
                     }
                     memcpy(str, PyBytes_AsString(repunicode), repsize);