mirror of
https://github.com/python/cpython.git
synced 2025-07-15 23:35:23 +00:00
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.
This commit is contained in:
parent
14176a56d3
commit
1ab8330827
49 changed files with 385 additions and 255 deletions
|
@ -5854,6 +5854,29 @@ onError:
|
|||
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 @@ static
|
|||
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 @@ PyObject *unicode_repr(PyObject *unicode)
|
|||
escape.
|
||||
*/
|
||||
|
||||
repr = PyString_FromStringAndSize(NULL,
|
||||
repr = PyUnicode_FromUnicode(NULL,
|
||||
2 /* quotes */
|
||||
#ifdef Py_UNICODE_WIDE
|
||||
+ 10*size
|
||||
|
@ -6782,7 +6805,7 @@ PyObject *unicode_repr(PyObject *unicode)
|
|||
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 @@ PyObject *unicode_repr(PyObject *unicode)
|
|||
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 @@ PyObject *unicode_repr(PyObject *unicode)
|
|||
*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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue