[Python-checkins] cpython: Fix fixup() for unchanged unicode subtype

victor.stinner python-checkins at python.org
Mon Dec 12 01:25:56 CET 2011


http://hg.python.org/cpython/rev/7a6ff1275b16
changeset:   73937:7a6ff1275b16
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun Dec 11 22:22:39 2011 +0100
summary:
  Fix fixup() for unchanged unicode subtype

If maxchar_new == 0 and self is a unicode subtype, return u instead of duplicating u.

files:
  Objects/unicodeobject.c |  70 ++++++++++++++--------------
  1 files changed, 35 insertions(+), 35 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -9211,6 +9211,7 @@
 {
     PyObject *u;
     Py_UCS4 maxchar_old, maxchar_new = 0;
+    PyObject *v;
 
     u = PyUnicode_Copy(self);
     if (u == NULL)
@@ -9222,9 +9223,19 @@
        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 @@
     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;
-    }
-    else if (maxchar_new == maxchar_old) {
-        return 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 {
-        /* 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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list