Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3be613

Browse files
author
Victor Stinner
committed
Use PyUnicode_WCHAR_KIND to check if a string is a wstr string
Simplify the test in wstr pointer in unicode_sizeof().
1 parent 910337b commit a3be613

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

Objects/unicodeobject.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,18 +1181,23 @@ unicode_dealloc(register PyUnicodeObject *unicode)
11811181
static int
11821182
unicode_resizable(PyObject *unicode)
11831183
{
1184+
Py_ssize_t len;
11841185
if (Py_REFCNT(unicode) != 1)
11851186
return 0;
11861187
if (PyUnicode_CHECK_INTERNED(unicode))
11871188
return 0;
11881189
if (unicode == unicode_empty)
11891190
return 0;
1190-
if (PyUnicode_WSTR_LENGTH(unicode) == 1) {
1191+
if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND)
1192+
len = PyUnicode_WSTR_LENGTH(unicode);
1193+
else
1194+
len = PyUnicode_GET_LENGTH(unicode);
1195+
if (len == 1) {
11911196
Py_UCS4 ch;
1192-
if (PyUnicode_IS_COMPACT(unicode))
1193-
ch = PyUnicode_READ_CHAR(unicode, 0);
1194-
else
1197+
if (_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND)
11951198
ch = _PyUnicode_WSTR(unicode)[0];
1199+
else
1200+
ch = PyUnicode_READ_CHAR(unicode, 0);
11961201
if (ch < 256 && unicode_latin1[ch] == unicode)
11971202
return 0;
11981203
}
@@ -11969,12 +11974,9 @@ unicode__sizeof__(PyUnicodeObject *v)
1196911974
PyUnicode_CHARACTER_SIZE(v);
1197011975
}
1197111976
/* If the wstr pointer is present, account for it unless it is shared
11972-
with the data pointer. Since PyUnicode_DATA will crash if the object
11973-
is not ready, check whether it's either not ready (in which case the
11974-
data is entirely in wstr) or if the data is not shared. */
11977+
with the data pointer. Check if the data is not shared. */
1197511978
if (_PyUnicode_WSTR(v) &&
11976-
(!PyUnicode_IS_READY(v) ||
11977-
(PyUnicode_DATA(v) != _PyUnicode_WSTR(v))))
11979+
(PyUnicode_DATA(v) != _PyUnicode_WSTR(v)))
1197811980
size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t);
1197911981
if (_PyUnicode_HAS_UTF8_MEMORY(v))
1198011982
size += PyUnicode_UTF8_LENGTH(v) + 1;

0 commit comments

Comments
 (0)