@@ -6893,46 +6893,36 @@ PyUnicode_DecodeUnicodeEscape(const char *s,
6893
6893
PyObject *
6894
6894
PyUnicode_AsUnicodeEscapeString (PyObject * unicode )
6895
6895
{
6896
- Py_ssize_t i , len ;
6897
- PyObject * repr ;
6898
- char * p ;
6899
- int kind ;
6900
- const void * data ;
6901
- Py_ssize_t expandsize ;
6902
-
6903
- /* Initial allocation is based on the longest-possible character
6904
- escape.
6905
-
6906
- For UCS1 strings it's '\xxx', 4 bytes per source character.
6907
- For UCS2 strings it's '\uxxxx', 6 bytes per source character.
6908
- For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character.
6909
- */
6910
-
6911
6896
if (!PyUnicode_Check (unicode )) {
6912
6897
PyErr_BadArgument ();
6913
6898
return NULL ;
6914
6899
}
6915
6900
6916
- len = PyUnicode_GET_LENGTH (unicode );
6901
+ Py_ssize_t len = PyUnicode_GET_LENGTH (unicode );
6917
6902
if (len == 0 ) {
6918
6903
return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
6919
6904
}
6905
+ int kind = PyUnicode_KIND (unicode );
6906
+ const void * data = PyUnicode_DATA (unicode );
6920
6907
6921
- kind = PyUnicode_KIND (unicode );
6922
- data = PyUnicode_DATA (unicode );
6923
- /* 4 byte characters can take up 10 bytes, 2 byte characters can take up 6
6924
- bytes, and 1 byte characters 4. */
6925
- expandsize = kind * 2 + 2 ;
6908
+ /* Initial allocation is based on the longest-possible character
6909
+ * escape.
6910
+ *
6911
+ * For UCS1 strings it's '\xxx', 4 bytes per source character.
6912
+ * For UCS2 strings it's '\uxxxx', 6 bytes per source character.
6913
+ * For UCS4 strings it's '\U00xxxxxx', 10 bytes per source character. */
6914
+ Py_ssize_t expandsize = kind * 2 + 2 ;
6926
6915
if (len > PY_SSIZE_T_MAX / expandsize ) {
6927
6916
return PyErr_NoMemory ();
6928
6917
}
6929
- repr = PyBytes_FromStringAndSize (NULL , expandsize * len );
6930
- if (repr == NULL ) {
6918
+
6919
+ PyBytesWriter * writer = PyBytesWriter_Create (expandsize * len );
6920
+ if (writer == NULL ) {
6931
6921
return NULL ;
6932
6922
}
6923
+ char * p = PyBytesWriter_GetData (writer );
6933
6924
6934
- p = PyBytes_AS_STRING (repr );
6935
- for (i = 0 ; i < len ; i ++ ) {
6925
+ for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
6936
6926
Py_UCS4 ch = PyUnicode_READ (kind , data , i );
6937
6927
6938
6928
/* U+0000-U+00ff range */
@@ -6998,11 +6988,7 @@ PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
6998
6988
}
6999
6989
}
7000
6990
7001
- assert (p - PyBytes_AS_STRING (repr ) > 0 );
7002
- if (_PyBytes_Resize (& repr , p - PyBytes_AS_STRING (repr )) < 0 ) {
7003
- return NULL ;
7004
- }
7005
- return repr ;
6991
+ return PyBytesWriter_FinishWithPointer (writer , p );
7006
6992
}
7007
6993
7008
6994
/* --- Raw Unicode Escape Codec ------------------------------------------- */
0 commit comments