[Python-checkins] cpython: Fix resize_compact() and resize_inplace(); reenable full resize optimizations

victor.stinner python-checkins at python.org
Mon Oct 3 12:52:05 CEST 2011


http://hg.python.org/cpython/rev/78183a564462
changeset:   72612:78183a564462
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Oct 03 12:52:27 2011 +0200
summary:
  Fix resize_compact() and resize_inplace(); reenable full resize optimizations

 * resize_compact() updates also wstr_len for non-ascii strings sharing wstr
 * resize_inplace() updates also utf8_len/wstr_len for strings sharing
   utf8/wstr

files:
  Objects/unicodeobject.c |  31 +++++++++++++++++++---------
  1 files changed, 21 insertions(+), 10 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -130,6 +130,14 @@
      (PyUnicode_IS_READY(op) ?                          \
       0 : _PyUnicode_Ready((PyObject *)(op))))
 
+#define _PyUnicode_SHARE_UTF8(op)                       \
+    (assert(_PyUnicode_CHECK(op)),                      \
+     assert(!PyUnicode_IS_COMPACT_ASCII(op)),           \
+     (_PyUnicode_UTF8(op) == PyUnicode_DATA(op)))
+#define _PyUnicode_SHARE_WSTR(op)                       \
+    (assert(_PyUnicode_CHECK(op)),                      \
+     (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(op)))
+
 /* true if the Unicode object has an allocated UTF-8 memory block
    (not shared with other data) */
 #define _PyUnicode_HAS_UTF8_MEMORY(op)                  \
@@ -398,7 +406,7 @@
         struct_size = sizeof(PyASCIIObject);
     else
         struct_size = sizeof(PyCompactUnicodeObject);
-    share_wstr = (_PyUnicode_WSTR(unicode) == PyUnicode_DATA(unicode));
+    share_wstr = _PyUnicode_SHARE_WSTR(unicode);
 
     _Py_DEC_REFTOTAL;
     _Py_ForgetReference(unicode);
@@ -417,8 +425,11 @@
     }
     _Py_NewReference(unicode);
     _PyUnicode_LENGTH(unicode) = length;
-    if (share_wstr)
+    if (share_wstr) {
         _PyUnicode_WSTR(unicode) = PyUnicode_DATA(unicode);
+        if (!PyUnicode_IS_COMPACT_ASCII(unicode))
+            _PyUnicode_WSTR_LENGTH(unicode) = length;
+    }
     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
                     length, 0);
     return unicode;
@@ -449,8 +460,8 @@
         data = _PyUnicode_DATA_ANY(unicode);
         assert(data != NULL);
         char_size = PyUnicode_CHARACTER_SIZE(unicode);
-        share_wstr = (_PyUnicode_WSTR(unicode) == data);
-        share_utf8 = (_PyUnicode_UTF8(unicode) == data);
+        share_wstr = _PyUnicode_SHARE_WSTR(unicode);
+        share_utf8 = _PyUnicode_SHARE_UTF8(unicode);
 
         if (length > (PY_SSIZE_T_MAX / char_size - 1)) {
             PyErr_NoMemory();
@@ -464,10 +475,14 @@
             return -1;
         }
         _PyUnicode_DATA_ANY(unicode) = data;
-        if (share_wstr)
+        if (share_wstr) {
             _PyUnicode_WSTR(unicode) = data;
-        if (share_utf8)
+            _PyUnicode_WSTR_LENGTH(unicode) = length;
+        }
+        if (share_utf8) {
             _PyUnicode_UTF8(unicode) = data;
+            _PyUnicode_UTF8_LENGTH(unicode) = length;
+        }
         _PyUnicode_LENGTH(unicode) = length;
         PyUnicode_WRITE(PyUnicode_KIND(unicode), data, length, 0);
         if (share_wstr)
@@ -1189,10 +1204,6 @@
 unicode_resizable(PyObject *unicode)
 {
     Py_ssize_t len;
-#if SIZEOF_WCHAR_T == 2
-    /* FIXME: unicode_resize() is buggy on Windows */
-    return 0;
-#endif
     if (Py_REFCNT(unicode) != 1)
         return 0;
     if (PyUnicode_CHECK_INTERNED(unicode))

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


More information about the Python-checkins mailing list