diff -r 817a003ecdc6 Include/unicodeobject.h --- a/Include/unicodeobject.h Sun Nov 13 23:36:30 2016 +0000 +++ b/Include/unicodeobject.h Mon Nov 14 13:20:02 2016 +0800 @@ -2037,7 +2037,7 @@ ); #ifndef Py_LIMITED_API -PyAPI_FUNC(int) _PyUnicode_CompareWithId( +PyAPI_FUNC(int) _PyUnicode_EqualToId( PyObject *left, /* Left string */ _Py_Identifier *right /* Right identifier */ ); diff -r 817a003ecdc6 Objects/typeobject.c --- a/Objects/typeobject.c Sun Nov 13 23:36:30 2016 +0000 +++ b/Objects/typeobject.c Mon Nov 14 13:20:02 2016 +0800 @@ -863,7 +863,7 @@ return NULL; } - if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) + if (mod != NULL && !_PyUnicode_EqualToId(mod, &PyId_builtins)) rtn = PyUnicode_FromFormat("", mod, name); else rtn = PyUnicode_FromFormat("", type->tp_name); @@ -2403,7 +2403,7 @@ if (!valid_identifier(tmp)) goto error; assert(PyUnicode_Check(tmp)); - if (_PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) { + if (_PyUnicode_EqualToId(tmp, &PyId___dict__)) { if (!may_add_dict || add_dict) { PyErr_SetString(PyExc_TypeError, "__dict__ slot disallowed: " @@ -2434,7 +2434,7 @@ for (i = j = 0; i < nslots; i++) { tmp = PyTuple_GET_ITEM(slots, i); if ((add_dict && - _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) || + _PyUnicode_EqualToId(tmp, &PyId___dict__)) || (add_weak && PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) continue; @@ -3538,11 +3538,10 @@ Py_XDECREF(mod); return NULL; } - if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins)) + if (mod != NULL && !_PyUnicode_EqualToId(mod, &PyId_builtins)) rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); else - rtn = PyUnicode_FromFormat("<%s object at %p>", - type->tp_name, self); + rtn = PyUnicode_FromFormat("<%s object at %p>", type->tp_name, self); Py_XDECREF(mod); Py_DECREF(name); return rtn; @@ -7238,7 +7237,7 @@ (i.e. super, or a subclass), not the class of su->obj. */ if (PyUnicode_Check(name) && PyUnicode_GET_LENGTH(name) == 9 && - _PyUnicode_CompareWithId(name, &PyId___class__) == 0) + _PyUnicode_EqualToId(name, &PyId___class__)) goto skip; mro = starttype->tp_mro; @@ -7450,7 +7449,7 @@ for (i = 0; i < n; i++) { PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); assert(PyUnicode_Check(name)); - if (!_PyUnicode_CompareWithId(name, &PyId___class__)) { + if (_PyUnicode_EqualToId(name, &PyId___class__)) { Py_ssize_t index = co->co_nlocals + PyTuple_GET_SIZE(co->co_cellvars) + i; PyObject *cell = f->f_localsplus[index]; diff -r 817a003ecdc6 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sun Nov 13 23:36:30 2016 +0000 +++ b/Objects/unicodeobject.c Mon Nov 14 13:20:02 2016 +0800 @@ -10992,13 +10992,32 @@ return -1; } +/* Test whether a unicode is equal to an identifier. Return 1 if true + otherwise 0. Any error occurs inside is treated as not equal and + will be cleared before return. */ int -_PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right) -{ - PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */ - if (right_str == NULL) - return -1; - return PyUnicode_Compare(left, right_str); +_PyUnicode_EqualToId(PyObject *left, _Py_Identifier *right) +{ + if (!PyUnicode_Check(left)) { + return 0; + } + + if (PyUnicode_READY(left) == -1) { + PyErr_Clear(); + return 0; + } + + PyObject *right_uni = _PyUnicode_FromId(right); /* borrowed */ + if (right_uni == NULL) { + PyErr_Clear(); + return (PyUnicode_CompareWithASCIIString(left, right->string) == 0); + } + + if (left == right_uni) { + return 1; + } + + return unicode_compare_eq(left, right_uni); } int diff -r 817a003ecdc6 Python/errors.c --- a/Python/errors.c Sun Nov 13 23:36:30 2016 +0000 +++ b/Python/errors.c Mon Nov 14 13:20:02 2016 +0800 @@ -984,7 +984,7 @@ goto done; } else { - if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) { + if (!_PyUnicode_EqualToId(moduleName, &PyId_builtins)) { if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0) goto done; if (PyFile_WriteString(".", f) < 0) diff -r 817a003ecdc6 Python/pythonrun.c --- a/Python/pythonrun.c Sun Nov 13 23:36:30 2016 +0000 +++ b/Python/pythonrun.c Mon Nov 14 13:20:02 2016 +0800 @@ -751,7 +751,7 @@ err = PyFile_WriteString("", f); } else { - if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) + if (!_PyUnicode_EqualToId(moduleName, &PyId_builtins)) { err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW); err += PyFile_WriteString(".", f);