[Python-checkins] cpython: Add PyUnicode_Copy() function, include it to the public API

victor.stinner python-checkins at python.org
Fri Sep 30 02:26:41 CEST 2011


http://hg.python.org/cpython/rev/c75b04996f54
changeset: 72546:c75b04996f54
user: Victor Stinner <victor.stinner at haypocalc.com>
date: Fri Sep 30 02:26:44 2011 +0200
summary:
 Add PyUnicode_Copy() function, include it to the public API
files:
 Include/unicodeobject.h | 5 +++
 Modules/posixmodule.c | 3 +-
 Objects/unicodeobject.c | 42 +++++++++++++++-------------
 3 files changed, 28 insertions(+), 22 deletions(-)
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -510,6 +510,11 @@
 );
 #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
 character conversion when necessary and falls back to memcpy if possible.
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -729,8 +729,7 @@
 else if (PyUnicode_Check(*param))
 /* For a Unicode subtype that's not a Unicode object,
 return a true Unicode object with the same data. */
- *param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
- PyUnicode_GET_SIZE(*param));
+ *param = PyUnicode_Copy(*param);
 else
 *param = PyUnicode_FromEncodedObject(*param,
 Py_FileSystemDefaultEncoding,
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1209,6 +1209,20 @@
 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.
 Return NULL if the string is too wide already. */
@@ -9061,9 +9075,7 @@
 Py_INCREF(self);
 return (PyObject *) self;
 }
- return PyUnicode_FromKindAndData(PyUnicode_KIND(self),
- PyUnicode_DATA(self),
- PyUnicode_GET_LENGTH(self));
+ return PyUnicode_Copy(self);
 error:
 if (srelease && sbuf)
 PyMem_FREE(sbuf);
@@ -10477,7 +10489,8 @@
 return NULL;
 kind = PyUnicode_KIND(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);
 }
 
@@ -11267,8 +11280,7 @@
 return self;
 } else
 /* Subtype -- return genuine unicode string with the same value. */
- return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(self),
- PyUnicode_GET_SIZE(self));
+ return PyUnicode_Copy(self);
 }
 
 PyDoc_STRVAR(swapcase__doc__,
@@ -11453,10 +11465,7 @@
 return (PyObject*) self;
 }
 else
- return PyUnicode_FromUnicode(
- PyUnicode_AS_UNICODE(self),
- PyUnicode_GET_SIZE(self)
- );
+ return PyUnicode_Copy(self);
 }
 
 fill = width - _PyUnicode_LENGTH(self);
@@ -11652,16 +11661,9 @@
 "S.__sizeof__() -> size of S in memory, in bytes");
 
 static PyObject *
-unicode_getnewargs(PyUnicodeObject *v)
-{
- PyObject *copy;
- 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));
+unicode_getnewargs(PyObject *v)
+{
+ PyObject *copy = PyUnicode_Copy(v);
 if (!copy)
 return NULL;
 return Py_BuildValue("(N)", copy);
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /