]> granicus.if.org Git - python/commitdiff
PyObject_Dump() encodes unicode objects to utf8 with backslashreplace (instead
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 17 May 2010 01:26:01 +0000 (01:26 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 17 May 2010 01:26:01 +0000 (01:26 +0000)
of strict) error handler to escape surrogates

Lib/test/test_sys.py
Misc/NEWS
Objects/object.c

index abbd7592c72a3c949a1c7fc6f3c84ae1c3da2452..ecbc9db2ec8e49b5f17592decce7f0fc431941d3 100644 (file)
@@ -145,6 +145,16 @@ class SysModuleTest(unittest.TestCase):
                               "raise SystemExit(47)"])
         self.assertEqual(rc, 47)
 
+        # test that the exit message is written with backslashreplace error
+        # handler to stderr
+        import subprocess
+        code = r'import sys; sys.exit("surrogates:\uDCFF")'
+        process = subprocess.Popen([sys.executable, "-c", code],
+                                   stderr=subprocess.PIPE)
+        stdout, stderr = process.communicate()
+        self.assertEqual(process.returncode, 1)
+        self.assertTrue(stderr.startswith(b"surrogates:\\udcff"), stderr)
+
     def test_getdefaultencoding(self):
         self.assertRaises(TypeError, sys.getdefaultencoding, 42)
         # can't check more than the type, as the user might have changed it
index f801e3e39c647e3713520fb421ca1afc816319e3..6b3842510dbc595f71ab4bbfb014e55f0dca1c39 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- PyObject_Dump() encodes unicode objects to utf8 with backslashreplace
+  (instead of strict) error handler to escape surrogates
+
 - Issue #8715: Create PyUnicode_EncodeFSDefault() function: Encode a Unicode
   object to Py_FileSystemDefaultEncoding with the "surrogateescape" error
   handler, and return bytes. If Py_FileSystemDefaultEncoding is not set, fall
index 8ddc7ec78c0ed69b3d0e180e3ae8ab13c911a39a..1f4e3dd445ffcc3d41895abd7ebc02aef6ce1dc8 100644 (file)
@@ -303,7 +303,9 @@ internal_print(PyObject *op, FILE *fp, int flags, int nesting)
             }
             else if (PyUnicode_Check(s)) {
                 PyObject *t;
-                t = _PyUnicode_AsDefaultEncodedString(s, NULL);
+                t = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(s),
+                                         PyUnicode_GET_SIZE(s),
+                                         "backslashreplace");
                 if (t == NULL)
                     ret = 0;
                 else {