Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
PyString_Concat() and PyString_ConcatAndDel() (the name PyUnicode_Concat()
was already taken).
Change PyObject_Repr() to always return a unicode object.
Update all repr implementations to return unicode objects.
Add a function PyObject_ReprStr8() that calls PyObject_Repr() and converts
the result to an 8bit string.
Use PyObject_ReprStr8() where using PyObject_Repr() can't be done
straightforward.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index e77b65d..b46093e 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -5854,6 +5854,29 @@
return NULL;
}
+void
+PyUnicode_Append(PyObject **pleft, PyObject *right)
+{
+ PyObject *new;
+ if (*pleft == NULL)
+ return;
+ if (right == NULL || !PyUnicode_Check(*pleft)) {
+ Py_DECREF(*pleft);
+ *pleft = NULL;
+ return;
+ }
+ new = PyUnicode_Concat(*pleft, right);
+ Py_DECREF(*pleft);
+ *pleft = new;
+}
+
+void
+PyUnicode_AppendAndDel(PyObject **pleft, PyObject *right)
+{
+ PyUnicode_Append(pleft, right);
+ Py_XDECREF(right);
+}
+
PyDoc_STRVAR(count__doc__,
"S.count(sub[, start[, end]]) -> int\n\
\n\
@@ -6749,7 +6772,7 @@
PyObject *unicode_repr(PyObject *unicode)
{
PyObject *repr;
- char *p;
+ Py_UNICODE *p;
Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode);
Py_ssize_t size = PyUnicode_GET_SIZE(unicode);
@@ -6771,7 +6794,7 @@
escape.
*/
- repr = PyString_FromStringAndSize(NULL,
+ repr = PyUnicode_FromUnicode(NULL,
2 /* quotes */
#ifdef Py_UNICODE_WIDE
+ 10*size
@@ -6782,7 +6805,7 @@
if (repr == NULL)
return NULL;
- p = PyString_AS_STRING(repr);
+ p = PyUnicode_AS_UNICODE(repr);
/* Add quote */
*p++ = (findchar(s, size, '\'') &&
@@ -6791,9 +6814,9 @@
Py_UNICODE ch = *s++;
/* Escape quotes and backslashes */
- if ((ch == (Py_UNICODE) PyString_AS_STRING(repr)[0]) || (ch == '\\')) {
+ if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) {
*p++ = '\\';
- *p++ = (char) ch;
+ *p++ = ch;
continue;
}
@@ -6877,10 +6900,10 @@
*p++ = (char) ch;
}
/* Add quote */
- *p++ = PyString_AS_STRING(repr)[0];
+ *p++ = PyUnicode_AS_UNICODE(repr)[0];
*p = '\0';
- _PyString_Resize(&repr, p - PyString_AS_STRING(repr));
+ _PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr));
return repr;
}