diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-09-01 23:43:53 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-09-01 23:43:53 (GMT) |
commit | 71133ff36848e6dddc541d752ad86d9fbd65bfd4 (patch) | |
tree | 9cb23f97ed1fed9bd543e8c0213c362e344bd2a8 /Objects | |
parent | c4eb765fc1c1fb90c9de90917e167d2e0f907b8d (diff) | |
download | cpython-71133ff36848e6dddc541d752ad86d9fbd65bfd4.zip cpython-71133ff36848e6dddc541d752ad86d9fbd65bfd4.tar.gz cpython-71133ff36848e6dddc541d752ad86d9fbd65bfd4.tar.bz2 |
Create PyUnicode_strdup() function
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 95823ad..80e2e63 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -10014,6 +10014,28 @@ Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c) return NULL; } +Py_UNICODE* +PyUnicode_strdup(PyObject *object) +{ + PyUnicodeObject *unicode = (PyUnicodeObject *)object; + Py_UNICODE *copy; + Py_ssize_t size; + + /* Ensure we won't overflow the size. */ + if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) { + PyErr_NoMemory(); + return NULL; + } + size = PyUnicode_GET_SIZE(unicode) + 1; /* copy the nul character */ + size *= sizeof(Py_UNICODE); + copy = PyMem_Malloc(size); + if (copy == NULL) { + PyErr_NoMemory(); + return NULL; + } + memcpy(copy, PyUnicode_AS_UNICODE(unicode), size); + return copy; +} #ifdef __cplusplus } |