diff Objects/unicodeobject.c @ 41705:917040dfec58

Change PyUnicode_EncodeRawUnicodeEscape() to return bytes objects (PyUnicode_AsRawUnicodeEscapeString() still returns str8 objects).
author Walter Dörwald <walter@livinglogic.de>
date Sat, 12 May 2007 12:03:26 +0000
parents d237f47f293f
children 6026b8a26c05
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Sat May 12 11:13:47 2007 +0000
+++ b/Objects/unicodeobject.c	Sat May 12 12:03:26 2007 +0000
@@ -2363,16 +2363,16 @@
     char *q;
 
 #ifdef Py_UNICODE_WIDE
-    repr = PyString_FromStringAndSize(NULL, 10 * size);
+    repr = PyBytes_FromStringAndSize(NULL, 10 * size);
 #else
-    repr = PyString_FromStringAndSize(NULL, 6 * size);
+    repr = PyBytes_FromStringAndSize(NULL, 6 * size);
 #endif
     if (repr == NULL)
         return NULL;
     if (size == 0)
 	return repr;
 
-    p = q = PyString_AS_STRING(repr);
+    p = q = PyBytes_AS_STRING(repr);
     while (size-- > 0) {
         Py_UNICODE ch = *s++;
 #ifdef Py_UNICODE_WIDE
@@ -2405,18 +2405,29 @@
             *p++ = (char) ch;
     }
     *p = '\0';
-    _PyString_Resize(&repr, p - q);
+    if (PyBytes_Resize(repr, p - q)) {
+        Py_DECREF(repr);
+        return NULL;
+    }
     return repr;
 }
 
 PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
 {
+    PyObject *s, *result;
     if (!PyUnicode_Check(unicode)) {
-	PyErr_BadArgument();
-	return NULL;
-    }
-    return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
-					    PyUnicode_GET_SIZE(unicode));
+        PyErr_BadArgument();
+        return NULL;
+    }
+    s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
+                                         PyUnicode_GET_SIZE(unicode));
+
+    if (!s)
+        return NULL;
+    result = PyString_FromStringAndSize(PyBytes_AS_STRING(s),
+                                        PyBytes_GET_SIZE(s));
+    Py_DECREF(s);
+    return result;
 }
 
 /* --- Unicode Internal Codec ------------------------------------------- */