[Python-checkins] cpython: Cleanup PyUnicode_Append()

victor.stinner python-checkins at python.org
Sun Apr 14 19:31:46 CEST 2013


http://hg.python.org/cpython/rev/9744b2df134c
changeset:   83386:9744b2df134c
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sun Apr 14 19:13:03 2013 +0200
summary:
  Cleanup PyUnicode_Append()

 * Check also that right is a Unicode object
 * call directly resize_compact() instead of unicode_resize() for a more
   explicit error handling, and to avoid testing some properties twice
   (ex: unicode_modifiable())

files:
  Objects/unicodeobject.c |  34 ++++++++++++----------------
  1 files changed, 15 insertions(+), 19 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -10671,7 +10671,8 @@
         return;
     }
     left = *p_left;
-    if (right == NULL || left == NULL || !PyUnicode_Check(left)) {
+    if (right == NULL || left == NULL
+        || !PyUnicode_Check(left) || !PyUnicode_Check(right)) {
         if (!PyErr_Occurred())
             PyErr_BadInternalCall();
         goto error;
@@ -10711,17 +10712,12 @@
         && !(PyUnicode_IS_ASCII(left) && !PyUnicode_IS_ASCII(right)))
     {
         /* append inplace */
-        if (unicode_resize(p_left, new_len) != 0) {
-            /* XXX if _PyUnicode_Resize() fails, 'left' has been
-             * deallocated so it cannot be put back into
-             * 'variable'.  The MemoryError is raised when there
-             * is no value in 'variable', which might (very
-             * remotely) be a cause of incompatibilities.
-             */
+        res = resize_compact(left, new_len);
+        if (res == NULL)
             goto error;
-        }
-        /* copy 'right' into the newly allocated area of 'left' */
-        _PyUnicode_FastCopyCharacters(*p_left, left_len, right, 0, right_len);
+
+        /* copy 'right' into the newly allocated area of 'res' (left) */
+        _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len);
     }
     else {
         maxchar = PyUnicode_MAX_CHAR_VALUE(left);
@@ -10735,8 +10731,8 @@
         _PyUnicode_FastCopyCharacters(res, 0, left, 0, left_len);
         _PyUnicode_FastCopyCharacters(res, left_len, right, 0, right_len);
         Py_DECREF(left);
-        *p_left = res;
-    }
+    }
+    *p_left = res;
     assert(_PyUnicode_CheckConsistency(*p_left, 1));
     return;
 
@@ -14520,12 +14516,12 @@
     t = PyDict_GetItem(interned, s);
     Py_END_ALLOW_RECURSION
 
-        if (t) {
-            Py_INCREF(t);
-            Py_DECREF(*p);
-            *p = t;
-            return;
-        }
+    if (t) {
+        Py_INCREF(t);
+        Py_DECREF(*p);
+        *p = t;
+        return;
+    }
 
     PyThreadState_GET()->recursion_critical = 1;
     if (PyDict_SetItem(interned, s, s) < 0) {

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


More information about the Python-checkins mailing list