[Python-checkins] cpython: Use PyUnicode_WCHAR_KIND to check if a string is a wstr string

victor.stinner python-checkins at python.org
Mon Oct 3 04:02:39 CEST 2011


http://hg.python.org/cpython/rev/2f9ac1eb1a99
changeset:   72604:2f9ac1eb1a99
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Oct 03 02:16:37 2011 +0200
summary:
  Use PyUnicode_WCHAR_KIND to check if a string is a wstr string

Simplify the test in wstr pointer in unicode_sizeof().

files:
  Objects/unicodeobject.c |  20 +++++++++++---------
  1 files changed, 11 insertions(+), 9 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1181,18 +1181,23 @@
 static int
 unicode_resizable(PyObject *unicode)
 {
+    Py_ssize_t len;
     if (Py_REFCNT(unicode) != 1)
         return 0;
     if (PyUnicode_CHECK_INTERNED(unicode))
         return 0;
     if (unicode == unicode_empty)
         return 0;
-    if (PyUnicode_WSTR_LENGTH(unicode) == 1) {
+    if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND)
+        len = PyUnicode_WSTR_LENGTH(unicode);
+    else
+        len = PyUnicode_GET_LENGTH(unicode);
+    if (len == 1) {
         Py_UCS4 ch;
-        if (PyUnicode_IS_COMPACT(unicode))
+        if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND)
+            ch = _PyUnicode_WSTR(unicode)[0];
+        else
             ch = PyUnicode_READ_CHAR(unicode, 0);
-        else
-            ch = _PyUnicode_WSTR(unicode)[0];
         if (ch < 256 && unicode_latin1[ch] == unicode)
             return 0;
     }
@@ -11969,12 +11974,9 @@
                 PyUnicode_CHARACTER_SIZE(v);
     }
     /* If the wstr pointer is present, account for it unless it is shared
-       with the data pointer. Since PyUnicode_DATA will crash if the object
-       is not ready, check whether it's either not ready (in which case the
-       data is entirely in wstr) or if the data is not shared. */
+       with the data pointer. Check if the data is not shared. */
     if (_PyUnicode_WSTR(v) &&
-        (!PyUnicode_IS_READY(v) ||
-         (PyUnicode_DATA(v) != _PyUnicode_WSTR(v))))
+        (PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))
         size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t);
     if (_PyUnicode_HAS_UTF8_MEMORY(v))
         size += PyUnicode_UTF8_LENGTH(v) + 1;

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


More information about the Python-checkins mailing list