Add PyUnicode_Copy() function, include it to the public API

This commit is contained in:
Victor Stinner 2011-09-30 02:26:44 +02:00
parent b153615008
commit 034f6cf10c
3 changed files with 27 additions and 21 deletions

View file

@ -510,6 +510,11 @@ PyAPI_FUNC(int) _PyUnicode_Ready(
); );
#endif #endif
/* Get a copy of a Unicode string. */
PyAPI_FUNC(PyObject*) PyUnicode_Copy(
PyObject *unicode
);
/* Copy character from one unicode object into another, this function performs /* Copy character from one unicode object into another, this function performs
character conversion when necessary and falls back to memcpy if possible. character conversion when necessary and falls back to memcpy if possible.

View file

@ -729,8 +729,7 @@ convert_to_unicode(PyObject **param)
else if (PyUnicode_Check(*param)) else if (PyUnicode_Check(*param))
/* For a Unicode subtype that's not a Unicode object, /* For a Unicode subtype that's not a Unicode object,
return a true Unicode object with the same data. */ return a true Unicode object with the same data. */
*param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param), *param = PyUnicode_Copy(*param);
PyUnicode_GET_SIZE(*param));
else else
*param = PyUnicode_FromEncodedObject(*param, *param = PyUnicode_FromEncodedObject(*param,
Py_FileSystemDefaultEncoding, Py_FileSystemDefaultEncoding,

View file

@ -1209,6 +1209,20 @@ PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
return NULL; return NULL;
} }
PyObject*
PyUnicode_Copy(PyObject *unicode)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadInternalCall();
return NULL;
}
if (PyUnicode_READY(unicode))
return NULL;
return PyUnicode_FromKindAndData(PyUnicode_KIND(unicode),
PyUnicode_DATA(unicode),
PyUnicode_GET_LENGTH(unicode));
}
/* Widen Unicode objects to larger buffers. /* Widen Unicode objects to larger buffers.
Return NULL if the string is too wide already. */ Return NULL if the string is too wide already. */
@ -9061,9 +9075,7 @@ replace(PyObject *self, PyObject *str1,
Py_INCREF(self); Py_INCREF(self);
return (PyObject *) self; return (PyObject *) self;
} }
return PyUnicode_FromKindAndData(PyUnicode_KIND(self), return PyUnicode_Copy(self);
PyUnicode_DATA(self),
PyUnicode_GET_LENGTH(self));
error: error:
if (srelease && sbuf) if (srelease && sbuf)
PyMem_FREE(sbuf); PyMem_FREE(sbuf);
@ -10477,7 +10489,8 @@ PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
return NULL; return NULL;
kind = PyUnicode_KIND(self); kind = PyUnicode_KIND(self);
data = PyUnicode_1BYTE_DATA(self); data = PyUnicode_1BYTE_DATA(self);
return PyUnicode_FromKindAndData(kind, data + PyUnicode_KIND_SIZE(kind, start), return PyUnicode_FromKindAndData(kind,
data + PyUnicode_KIND_SIZE(kind, start),
end-start); end-start);
} }
@ -11267,8 +11280,7 @@ PyObject *unicode_str(PyObject *self)
return self; return self;
} else } else
/* Subtype -- return genuine unicode string with the same value. */ /* Subtype -- return genuine unicode string with the same value. */
return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self), return PyUnicode_Copy(self);
PyUnicode_GET_SIZE(self));
} }
PyDoc_STRVAR(swapcase__doc__, PyDoc_STRVAR(swapcase__doc__,
@ -11453,10 +11465,7 @@ unicode_zfill(PyUnicodeObject *self, PyObject *args)
return (PyObject*) self; return (PyObject*) self;
} }
else else
return PyUnicode_FromUnicode( return PyUnicode_Copy(self);
PyUnicode_AS_UNICODE(self),
PyUnicode_GET_SIZE(self)
);
} }
fill = width - _PyUnicode_LENGTH(self); fill = width - _PyUnicode_LENGTH(self);
@ -11652,16 +11661,9 @@ PyDoc_STRVAR(sizeof__doc__,
"S.__sizeof__() -> size of S in memory, in bytes"); "S.__sizeof__() -> size of S in memory, in bytes");
static PyObject * static PyObject *
unicode_getnewargs(PyUnicodeObject *v) unicode_getnewargs(PyObject *v)
{ {
PyObject *copy; PyObject *copy = PyUnicode_Copy(v);
unsigned char *data;
int kind;
if (PyUnicode_READY(v) == -1)
return NULL;
kind = PyUnicode_KIND(v);
data = PyUnicode_1BYTE_DATA(v);
copy = PyUnicode_FromKindAndData(kind, data, PyUnicode_GET_LENGTH(v));
if (!copy) if (!copy)
return NULL; return NULL;
return Py_BuildValue("(N)", copy); return Py_BuildValue("(N)", copy);