bpo-31393: Fix the use of PyUnicode_READY(). (#3451) · python/cpython@e3b2b4b (original) (raw)

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1467,7 +1467,10 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
1467 1467 len = PyByteArray_Size(obj);
1468 1468 }
1469 1469 else if (PyUnicode_Check(obj)) {
1470 -if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
1470 +if (PyUnicode_READY(obj) == -1) {
1471 +return 0;
1472 + }
1473 +if (PyUnicode_IS_COMPACT_ASCII(obj)) {
1471 1474 data->buf = PyUnicode_DATA(obj);
1472 1475 len = PyUnicode_GET_LENGTH(obj);
1473 1476 }
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ all_name_chars(PyObject *o)
27 27 };
28 28 const unsigned char *s, *e;
29 29
30 -if (PyUnicode_READY(o) == -1 |
30 +if (!PyUnicode_IS_ASCII(o))
31 31 return 0;
32 32
33 33 s = PyUnicode_1BYTE_DATA(o);
@@ -63,6 +63,10 @@ intern_string_constants(PyObject *tuple)
63 63 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
64 64 PyObject *v = PyTuple_GET_ITEM(tuple, i);
65 65 if (PyUnicode_CheckExact(v)) {
66 +if (PyUnicode_READY(v) == -1) {
67 +PyErr_Clear();
68 +continue;
69 + }
66 70 if (all_name_chars(v)) {
67 71 PyObject *w = v;
68 72 PyUnicode_InternInPlace(&v);
Original file line number Diff line number Diff line change
@@ -30,9 +30,9 @@ class object "PyObject *" "&PyBaseObject_Type"
30 30 #define MCACHE_HASH_METHOD(type, name) \
31 31 MCACHE_HASH((type)->tp_version_tag, \
32 32 ((PyASCIIObject *)(name))->hash)
33 -#define MCACHE_CACHEABLE_NAME(name) \
34 - PyUnicode_CheckExact(name) && \
35 -PyUnicode_READY(name) != -1 && \
33 +#define MCACHE_CACHEABLE_NAME(name) \
34 + PyUnicode_CheckExact(name) && \
35 +PyUnicode_IS_READY(name) && \
36 36 PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE
37 37
38 38 struct method_cache_entry {
Original file line number Diff line number Diff line change
@@ -4185,10 +4185,13 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
4185 4185 void *data;
4186 4186 int kind;
4187 4187
4188 -if (!PyUnicode_Check(unicode) |
4188 +if (!PyUnicode_Check(unicode)) {
4189 4189 PyErr_BadArgument();
4190 4190 return (Py_UCS4)-1;
4191 4191 }
4192 +if (PyUnicode_READY(unicode) == -1) {
4193 +return (Py_UCS4)-1;
4194 + }
4192 4195 if (index < 0 |
4193 4196 PyErr_SetString(PyExc_IndexError, "string index out of range");
4194 4197 return (Py_UCS4)-1;
@@ -11668,10 +11671,13 @@ unicode_getitem(PyObject *self, Py_ssize_t index)
11668 11671 enum PyUnicode_Kind kind;
11669 11672 Py_UCS4 ch;
11670 11673
11671 -if (!PyUnicode_Check(self) |
11674 +if (!PyUnicode_Check(self)) {
11672 11675 PyErr_BadArgument();
11673 11676 return NULL;
11674 11677 }
11678 +if (PyUnicode_READY(self) == -1) {
11679 +return NULL;
11680 + }
11675 11681 if (index < 0 |
11676 11682 PyErr_SetString(PyExc_IndexError, "string index out of range");
11677 11683 return NULL;
Original file line number Diff line number Diff line change
@@ -5017,13 +5017,16 @@ import_all_from(PyObject *locals, PyObject *v)
5017 5017 PyErr_Clear();
5018 5018 break;
5019 5019 }
5020 -if (skip_leading_underscores &&
5021 -PyUnicode_Check(name) &&
5022 -PyUnicode_READY(name) != -1 &&
5023 -PyUnicode_READ_CHAR(name, 0) == '_')
5024 - {
5025 -Py_DECREF(name);
5026 -continue;
5020 +if (skip_leading_underscores && PyUnicode_Check(name)) {
5021 +if (PyUnicode_READY(name) == -1) {
5022 +Py_DECREF(name);
5023 +err = -1;
5024 +break;
5025 + }
5026 +if (PyUnicode_READ_CHAR(name, 0) == '_') {
5027 +Py_DECREF(name);
5028 +continue;
5029 + }
5027 5030 }
5028 5031 value = PyObject_GetAttr(v, name);
5029 5032 if (value == NULL)