Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 034f6cf

Browse files
author
Victor Stinner
committed
Add PyUnicode_Copy() function, include it to the public API
1 parent b153615 commit 034f6cf

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

Include/unicodeobject.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ PyAPI_FUNC(int) _PyUnicode_Ready(
510510
);
511511
#endif
512512

513+
/* Get a copy of a Unicode string. */
514+
PyAPI_FUNC(PyObject*) PyUnicode_Copy(
515+
PyObject *unicode
516+
);
517+
513518
/* Copy character from one unicode object into another, this function performs
514519
character conversion when necessary and falls back to memcpy if possible.
515520

Modules/posixmodule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,7 @@ convert_to_unicode(PyObject **param)
729729
else if (PyUnicode_Check(*param))
730730
/* For a Unicode subtype that's not a Unicode object,
731731
return a true Unicode object with the same data. */
732-
*param = PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(*param),
733-
PyUnicode_GET_SIZE(*param));
732+
*param = PyUnicode_Copy(*param);
734733
else
735734
*param = PyUnicode_FromEncodedObject(*param,
736735
Py_FileSystemDefaultEncoding,

Objects/unicodeobject.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1127411286
PyDoc_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

1165411663
static 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

Comments
 (0)