diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-12-11 21:22:39 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-12-11 21:22:39 (GMT) |
commit | eaab604829b5b436623b802b1da933b4c49b630e (patch) | |
tree | b6d95dc22ce1cbb7d0567b77c38bb54912e6b50b | |
parent | e6b2d4407a120e62cf3f658d46dc5f8f6785977d (diff) | |
download | cpython-eaab604829b5b436623b802b1da933b4c49b630e.zip cpython-eaab604829b5b436623b802b1da933b4c49b630e.tar.gz cpython-eaab604829b5b436623b802b1da933b4c49b630e.tar.bz2 |
Fix fixup() for unchanged unicode subtype
If maxchar_new == 0 and self is a unicode subtype, return u instead of duplicating u.
-rw-r--r-- | Objects/unicodeobject.c | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d6a250e..c81c319 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9211,6 +9211,7 @@ fixup(PyObject *self, { PyObject *u; Py_UCS4 maxchar_old, maxchar_new = 0; + PyObject *v; u = PyUnicode_Copy(self); if (u == NULL) @@ -9222,9 +9223,19 @@ fixup(PyObject *self, everything is fine. Otherwise we need to change the string kind and re-run the fix function. */ maxchar_new = fixfct(u); - if (maxchar_new == 0) - /* do nothing, keep maxchar_new at 0 which means no changes. */; - else if (maxchar_new <= 127) + + if (maxchar_new == 0) { + /* no changes */; + if (PyUnicode_CheckExact(self)) { + Py_DECREF(u); + Py_INCREF(self); + return self; + } + else + return u; + } + + if (maxchar_new <= 127) maxchar_new = 127; else if (maxchar_new <= 255) maxchar_new = 255; @@ -9233,41 +9244,30 @@ fixup(PyObject *self, else maxchar_new = MAX_UNICODE; - if (!maxchar_new && PyUnicode_CheckExact(self)) { - /* fixfct should return TRUE if it modified the buffer. If - FALSE, return a reference to the original buffer instead - (to save space, not time) */ - Py_INCREF(self); + if (maxchar_new == maxchar_old) + return u; + + /* In case the maximum character changed, we need to + convert the string to the new category. */ + v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); + if (v == NULL) { Py_DECREF(u); - return self; + return NULL; } - else if (maxchar_new == maxchar_old) { - return u; + if (maxchar_new > maxchar_old) { + /* If the maxchar increased so that the kind changed, not all + characters are representable anymore and we need to fix the + string again. This only happens in very few cases. */ + copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); + maxchar_old = fixfct(v); + assert(maxchar_old > 0 && maxchar_old <= maxchar_new); } else { - /* In case the maximum character changed, we need to - convert the string to the new category. */ - PyObject *v = PyUnicode_New(PyUnicode_GET_LENGTH(self), maxchar_new); - if (v == NULL) { - Py_DECREF(u); - return NULL; - } - if (maxchar_new > maxchar_old) { - /* If the maxchar increased so that the kind changed, not all - characters are representable anymore and we need to fix the - string again. This only happens in very few cases. */ - copy_characters(v, 0, self, 0, PyUnicode_GET_LENGTH(self)); - maxchar_old = fixfct(v); - assert(maxchar_old > 0 && maxchar_old <= maxchar_new); - } - else { - copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); - } - - Py_DECREF(u); - assert(_PyUnicode_CheckConsistency(v, 1)); - return v; + copy_characters(v, 0, u, 0, PyUnicode_GET_LENGTH(self)); } + Py_DECREF(u); + assert(_PyUnicode_CheckConsistency(v, 1)); + return v; } static Py_UCS4 |