mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +00:00
Change PyUnicode_EncodeRawUnicodeEscape() to return bytes
objects (PyUnicode_AsRawUnicodeEscapeString() still returns str8 objects).
This commit is contained in:
parent
db5d33e4ee
commit
711005d339
1 changed files with 19 additions and 8 deletions
|
@ -2363,16 +2363,16 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
|
||||||
char *q;
|
char *q;
|
||||||
|
|
||||||
#ifdef Py_UNICODE_WIDE
|
#ifdef Py_UNICODE_WIDE
|
||||||
repr = PyString_FromStringAndSize(NULL, 10 * size);
|
repr = PyBytes_FromStringAndSize(NULL, 10 * size);
|
||||||
#else
|
#else
|
||||||
repr = PyString_FromStringAndSize(NULL, 6 * size);
|
repr = PyBytes_FromStringAndSize(NULL, 6 * size);
|
||||||
#endif
|
#endif
|
||||||
if (repr == NULL)
|
if (repr == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return repr;
|
return repr;
|
||||||
|
|
||||||
p = q = PyString_AS_STRING(repr);
|
p = q = PyBytes_AS_STRING(repr);
|
||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Py_UNICODE ch = *s++;
|
Py_UNICODE ch = *s++;
|
||||||
#ifdef Py_UNICODE_WIDE
|
#ifdef Py_UNICODE_WIDE
|
||||||
|
@ -2405,18 +2405,29 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
|
||||||
*p++ = (char) ch;
|
*p++ = (char) ch;
|
||||||
}
|
}
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
_PyString_Resize(&repr, p - q);
|
if (PyBytes_Resize(repr, p - q)) {
|
||||||
|
Py_DECREF(repr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return repr;
|
return repr;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
|
PyObject *PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
|
||||||
{
|
{
|
||||||
|
PyObject *s, *result;
|
||||||
if (!PyUnicode_Check(unicode)) {
|
if (!PyUnicode_Check(unicode)) {
|
||||||
PyErr_BadArgument();
|
PyErr_BadArgument();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
|
s = PyUnicode_EncodeRawUnicodeEscape(PyUnicode_AS_UNICODE(unicode),
|
||||||
PyUnicode_GET_SIZE(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 ------------------------------------------- */
|
/* --- Unicode Internal Codec ------------------------------------------- */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue