@@ -1209,6 +1209,20 @@ PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
12091209 return NULL ;
12101210}
12111211
1212+ PyObject *
1213+ PyUnicode_Copy (PyObject * unicode )
1214+ {
1215+ if (!PyUnicode_Check (unicode )) {
1216+ PyErr_BadInternalCall ();
1217+ return NULL ;
1218+ }
1219+ if (PyUnicode_READY (unicode ))
1220+ return NULL ;
1221+ return PyUnicode_FromKindAndData (PyUnicode_KIND (unicode ),
1222+ PyUnicode_DATA (unicode ),
1223+ PyUnicode_GET_LENGTH (unicode ));
1224+ }
1225+
12121226
12131227/* Widen Unicode objects to larger buffers.
12141228 Return NULL if the string is too wide already. */
@@ -9061,9 +9075,7 @@ replace(PyObject *self, PyObject *str1,
90619075 Py_INCREF (self );
90629076 return (PyObject * ) self ;
90639077 }
9064- return PyUnicode_FromKindAndData (PyUnicode_KIND (self ),
9065- PyUnicode_DATA (self ),
9066- PyUnicode_GET_LENGTH (self ));
9078+ return PyUnicode_Copy (self );
90679079 error :
90689080 if (srelease && sbuf )
90699081 PyMem_FREE (sbuf );
@@ -10477,7 +10489,8 @@ PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
1047710489 return NULL ;
1047810490 kind = PyUnicode_KIND (self );
1047910491 data = PyUnicode_1BYTE_DATA (self );
10480- return PyUnicode_FromKindAndData (kind , data + PyUnicode_KIND_SIZE (kind , start ),
10492+ return PyUnicode_FromKindAndData (kind ,
10493+ data + PyUnicode_KIND_SIZE (kind , start ),
1048110494 end - start );
1048210495}
1048310496
@@ -11267,8 +11280,7 @@ PyObject *unicode_str(PyObject *self)
1126711280 return self ;
1126811281 } else
1126911282 /* Subtype -- return genuine unicode string with the same value. */
11270- return PyUnicode_FromUnicode (PyUnicode_AS_UNICODE (self ),
11271- PyUnicode_GET_SIZE (self ));
11283+ return PyUnicode_Copy (self );
1127211284}
1127311285
1127411286PyDoc_STRVAR (swapcase__doc__ ,
@@ -11453,10 +11465,7 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
1145311465 return (PyObject * ) self ;
1145411466 }
1145511467 else
11456- return PyUnicode_FromUnicode (
11457- PyUnicode_AS_UNICODE (self ),
11458- PyUnicode_GET_SIZE (self )
11459- );
11468+ return PyUnicode_Copy (self );
1146011469 }
1146111470
1146211471 fill = width - _PyUnicode_LENGTH (self );
@@ -11652,16 +11661,9 @@ PyDoc_STRVAR(sizeof__doc__,
1165211661 "S.__sizeof__() -> size of S in memory, in bytes" );
1165311662
1165411663static PyObject *
11655- unicode_getnewargs (PyUnicodeObject * v )
11664+ unicode_getnewargs (PyObject * v )
1165611665{
11657- PyObject * copy ;
11658- unsigned char * data ;
11659- int kind ;
11660- if (PyUnicode_READY (v ) == -1 )
11661- return NULL ;
11662- kind = PyUnicode_KIND (v );
11663- data = PyUnicode_1BYTE_DATA (v );
11664- copy = PyUnicode_FromKindAndData (kind , data , PyUnicode_GET_LENGTH (v ));
11666+ PyObject * copy = PyUnicode_Copy (v );
1166511667 if (!copy )
1166611668 return NULL ;
1166711669 return Py_BuildValue ("(N)" , copy );
0 commit comments