Skip to content

Commit 0e8df33

Browse files
Issue #29190: Fixed possible errors in comparing strings in the pickle module.
1 parent 9404dd6 commit 0e8df33

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

Modules/_pickle.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,9 +1548,9 @@ memo_put(PicklerObject *self, PyObject *obj)
15481548
}
15491549

15501550
static PyObject *
1551-
get_dotted_path(PyObject *obj, PyObject *name) {
1551+
get_dotted_path(PyObject *obj, PyObject *name)
1552+
{
15521553
_Py_static_string(PyId_dot, ".");
1553-
_Py_static_string(PyId_locals, "<locals>");
15541554
PyObject *dotted_path;
15551555
Py_ssize_t i, n;
15561556

@@ -1561,12 +1561,7 @@ get_dotted_path(PyObject *obj, PyObject *name) {
15611561
assert(n >= 1);
15621562
for (i = 0; i < n; i++) {
15631563
PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1564-
PyObject *result = PyUnicode_RichCompare(
1565-
subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1566-
int is_equal = (result == Py_True);
1567-
assert(PyBool_Check(result));
1568-
Py_DECREF(result);
1569-
if (is_equal) {
1564+
if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
15701565
if (obj == NULL)
15711566
PyErr_Format(PyExc_AttributeError,
15721567
"Can't pickle local object %R", name);
@@ -3537,13 +3532,12 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
35373532
else if (PyUnicode_Check(name)) {
35383533
if (self->proto >= 4) {
35393534
_Py_IDENTIFIER(__newobj_ex__);
3540-
use_newobj_ex = PyUnicode_Compare(
3541-
name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3535+
use_newobj_ex = _PyUnicode_EqualToASCIIId(
3536+
name, &PyId___newobj_ex__);
35423537
}
35433538
if (!use_newobj_ex) {
35443539
_Py_IDENTIFIER(__newobj__);
3545-
use_newobj = PyUnicode_Compare(
3546-
name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3540+
use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
35473541
}
35483542
}
35493543
Py_XDECREF(name);

0 commit comments

Comments
 (0)