]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Bug #1379994: Fix *unicode_escape codecs to encode r'\' as r'\\'
authorHye-Shik Chang <hyeshik@gmail.com>
Sat, 17 Dec 2005 04:38:31 +0000 (04:38 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Sat, 17 Dec 2005 04:38:31 +0000 (04:38 +0000)
just like string codecs.

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

index 69244f0d6f3d8c6102b0c5a8cf4528debddb9fc3..16356b00957f3f1fefcbafe93946804a202e2297 100644 (file)
@@ -611,20 +611,24 @@ class UnicodeTest(
         self.assertEqual(u'hello'.encode('latin-1'), 'hello')
 
         # Roundtrip safety for BMP (just the first 1024 chars)
-        u = u''.join(map(unichr, xrange(1024)))
-        for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le', 'utf-16-be',
-                         'raw_unicode_escape', 'unicode_escape', 'unicode_internal'):
-            self.assertEqual(unicode(u.encode(encoding),encoding), u)
+        for c in xrange(1024):
+            u = unichr(c)
+            for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le',
+                             'utf-16-be', 'raw_unicode_escape',
+                             'unicode_escape', 'unicode_internal'):
+                self.assertEqual(unicode(u.encode(encoding),encoding), u)
 
         # Roundtrip safety for BMP (just the first 256 chars)
-        u = u''.join(map(unichr, xrange(256)))
-        for encoding in ('latin-1',):
-            self.assertEqual(unicode(u.encode(encoding),encoding), u)
+        for c in xrange(256):
+            u = unichr(c)
+            for encoding in ('latin-1',):
+                self.assertEqual(unicode(u.encode(encoding),encoding), u)
 
         # Roundtrip safety for BMP (just the first 128 chars)
-        u = u''.join(map(unichr, xrange(128)))
-        for encoding in ('ascii',):
-            self.assertEqual(unicode(u.encode(encoding),encoding), u)
+        for c in xrange(128):
+            u = unichr(c)
+            for encoding in ('ascii',):
+                self.assertEqual(unicode(u.encode(encoding),encoding), u)
 
         # Roundtrip safety for non-BMP (just a few chars)
         u = u'\U00010001\U00020002\U00030003\U00040004\U00050005'
index 01ddf7114d1601ad2d7a63b8b7092a6ebbd0597c..18323abbb65b0595f51298148e0abe43c84ea45c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4.3c1?
 Core and builtins
 -----------------
 
+- Bug #1379994: Builtin unicode_escape and raw_unicode_escape codec
+  now encodes backslash correctly.
+
 - Bug #1281408: Py_BuildValue now works correct even with unsigned longs
   and long longs.
 
index d149f0ba2dd18da3920cf0d7153c79885478398e..c766e6c144dd66c93b0e5223a7f5b1023d71c9df 100644 (file)
@@ -1981,9 +1981,9 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
     while (size-- > 0) {
         Py_UNICODE ch = *s++;
 
-        /* Escape quotes */
-        if (quotes &&
-           (ch == (Py_UNICODE) PyString_AS_STRING(repr)[1] || ch == '\\')) {
+        /* Escape quotes and backslashes */
+        if ((quotes &&
+            ch == (Py_UNICODE) PyString_AS_STRING(repr)[1]) || ch == '\\') {
             *p++ = '\\';
             *p++ = (char) ch;
            continue;