Use PyUnicode_WCHAR_KIND to check if a string is a wstr string

Simplify the test in wstr pointer in unicode_sizeof().
This commit is contained in:
Victor Stinner 2011-10-03 02:16:37 +02:00
parent 910337b42e
commit a3be613a56

View file

@ -1181,18 +1181,23 @@ unicode_dealloc(register PyUnicodeObject *unicode)
static int static int
unicode_resizable(PyObject *unicode) unicode_resizable(PyObject *unicode)
{ {
Py_ssize_t len;
if (Py_REFCNT(unicode) != 1) if (Py_REFCNT(unicode) != 1)
return 0; return 0;
if (PyUnicode_CHECK_INTERNED(unicode)) if (PyUnicode_CHECK_INTERNED(unicode))
return 0; return 0;
if (unicode == unicode_empty) if (unicode == unicode_empty)
return 0; 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; Py_UCS4 ch;
if (PyUnicode_IS_COMPACT(unicode)) if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND)
ch = PyUnicode_READ_CHAR(unicode, 0);
else
ch = _PyUnicode_WSTR(unicode)[0]; ch = _PyUnicode_WSTR(unicode)[0];
else
ch = PyUnicode_READ_CHAR(unicode, 0);
if (ch < 256 && unicode_latin1[ch] == unicode) if (ch < 256 && unicode_latin1[ch] == unicode)
return 0; return 0;
} }
@ -11969,12 +11974,9 @@ unicode__sizeof__(PyUnicodeObject *v)
PyUnicode_CHARACTER_SIZE(v); PyUnicode_CHARACTER_SIZE(v);
} }
/* If the wstr pointer is present, account for it unless it is shared /* 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 with the data pointer. Check if the data is not shared. */
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. */
if (_PyUnicode_WSTR(v) && 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); size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t);
if (_PyUnicode_HAS_UTF8_MEMORY(v)) if (_PyUnicode_HAS_UTF8_MEMORY(v))
size += PyUnicode_UTF8_LENGTH(v) + 1; size += PyUnicode_UTF8_LENGTH(v) + 1;