]> granicus.if.org Git - python/commitdiff
Issue #28701: Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIISt...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 16 Nov 2016 08:17:58 +0000 (10:17 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 16 Nov 2016 08:17:58 +0000 (10:17 +0200)
The latter function is more readable, faster and doesn't raise exceptions.

21 files changed:
Doc/c-api/unicode.rst
Include/unicodeobject.h
Modules/_decimal/_decimal.c
Modules/_elementtree.c
Modules/_io/textio.c
Modules/_lsprof.c
Modules/_pickle.c
Modules/_sqlite/connection.c
Modules/_testcapimodule.c
Modules/pyexpat.c
Objects/longobject.c
Objects/moduleobject.c
Objects/typeobject.c
Objects/unicodeobject.c
Python/_warnings.c
Python/ast.c
Python/compile.c
Python/future.c
Python/getargs.c
Python/import.c
Python/symtable.c

index ef8273c798b8e907ad564013cf0d07a9b3ed8b1f..cc23060a4d1b31795a9ac922c4c98fb744eb032e 100644 (file)
@@ -1638,6 +1638,9 @@ They all return *NULL* or ``-1`` if an exception occurs.
    Compare two strings and return ``-1``, ``0``, ``1`` for less than, equal, and greater than,
    respectively.
 
+   This function returns ``-1`` upon failure, so one should call
+   :c:func:`PyErr_Occurred` to check for errors.
+
 
 .. c:function:: int PyUnicode_CompareWithASCIIString(PyObject *uni, const char *string)
 
@@ -1646,6 +1649,9 @@ They all return *NULL* or ``-1`` if an exception occurs.
    ASCII-encoded strings, but the function interprets the input string as
    ISO-8859-1 if it contains non-ASCII characters.
 
+   This function returns ``-1`` upon failure, so one should call
+   :c:func:`PyErr_Occurred` to check for errors.
+
 
 .. c:function:: PyObject* PyUnicode_RichCompare(PyObject *left,  PyObject *right,  int op)
 
index 2c496eaada4aa193b5db1acf7fad183a0a37b76f..007c15bd47e3f5e7471f92744c0eb21a1fa6c4df 100644 (file)
@@ -2011,6 +2011,17 @@ PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
     const char *right           /* ASCII-encoded string */
     );
 
+#ifndef Py_LIMITED_API
+/* Test whether a unicode is equal to ASCII string.  Return 1 if true,
+   0 otherwise.  Return 0 if any argument contains non-ASCII characters.
+   Any error occurs inside will be cleared before return. */
+
+PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
+    PyObject *left,
+    const char *right           /* ASCII-encoded string */
+    );
+#endif
+
 /* Rich compare two strings and return one of the following:
 
    - NULL in case an exception was raised
index e15941aec8e175798538bbad948b422ae66067a0..6efdc91e6716a6a5674320522061e07f87c66685 100644 (file)
@@ -1119,12 +1119,12 @@ context_getattr(PyObject *self, PyObject *name)
     PyObject *retval;
 
     if (PyUnicode_Check(name)) {
-        if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, "traps")) {
             retval = ((PyDecContextObject *)self)->traps;
             Py_INCREF(retval);
             return retval;
         }
-        if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, "flags")) {
             retval = ((PyDecContextObject *)self)->flags;
             Py_INCREF(retval);
             return retval;
@@ -1144,10 +1144,10 @@ context_setattr(PyObject *self, PyObject *name, PyObject *value)
     }
 
     if (PyUnicode_Check(name)) {
-        if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, "traps")) {
             return context_settraps_dict(self, value);
         }
-        if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, "flags")) {
             return context_setstatus_dict(self, value);
         }
     }
@@ -2446,14 +2446,14 @@ dectuple_as_str(PyObject *dectuple)
     tmp = PyTuple_GET_ITEM(dectuple, 2);
     if (PyUnicode_Check(tmp)) {
         /* special */
-        if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) {
+        if (_PyUnicode_EqualToASCIIString(tmp, "F")) {
             strcat(sign_special, "Inf");
             is_infinite = 1;
         }
-        else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) {
+        else if (_PyUnicode_EqualToASCIIString(tmp, "n")) {
             strcat(sign_special, "NaN");
         }
-        else if (PyUnicode_CompareWithASCIIString(tmp, "N") == 0) {
+        else if (_PyUnicode_EqualToASCIIString(tmp, "N")) {
             strcat(sign_special, "sNaN");
         }
         else {
index 85ffca206bab2d60a6ec80727fd73dd12f8fac4a..1cdddaf0fbed000ef158ece66d44a9a7b16506fb 100644 (file)
@@ -3686,11 +3686,11 @@ xmlparser_getattro(XMLParserObject* self, PyObject* nameobj)
 {
     if (PyUnicode_Check(nameobj)) {
         PyObject* res;
-        if (PyUnicode_CompareWithASCIIString(nameobj, "entity") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "entity"))
             res = self->entity;
-        else if (PyUnicode_CompareWithASCIIString(nameobj, "target") == 0)
+        else if (_PyUnicode_EqualToASCIIString(nameobj, "target"))
             res = self->target;
-        else if (PyUnicode_CompareWithASCIIString(nameobj, "version") == 0) {
+        else if (_PyUnicode_EqualToASCIIString(nameobj, "version")) {
             return PyUnicode_FromFormat(
                 "Expat %d.%d.%d", XML_MAJOR_VERSION,
                 XML_MINOR_VERSION, XML_MICRO_VERSION);
index 5d7f9ab885c9c73890caa63fb6fbe1b70121b522..46c99b5a1d4b19694a981b92a212d01a8da37131 100644 (file)
@@ -1023,7 +1023,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
         else if (PyUnicode_Check(res)) {
             encodefuncentry *e = encodefuncs;
             while (e->name != NULL) {
-                if (!PyUnicode_CompareWithASCIIString(res, e->name)) {
+                if (_PyUnicode_EqualToASCIIString(res, e->name)) {
                     self->encodefunc = e->encodefunc;
                     break;
                 }
index 66e534f2a843b4f9cd8c7d83e42aa5966704f38a..7876e714b76e7d31bcc6191313dd1cd6aef096d0 100644 (file)
@@ -185,7 +185,7 @@ normalizeUserObj(PyObject *obj)
             }
         }
         if (modname != NULL) {
-            if (PyUnicode_CompareWithASCIIString(modname, "builtins") != 0) {
+            if (!_PyUnicode_EqualToASCIIString(modname, "builtins")) {
                 PyObject *result;
                 result = PyUnicode_FromFormat("<%U.%s>", modname,
                                               fn->m_ml->ml_name);
index 3c21b6aac415db845201182dd9f5903eae1433a9..ac7f5c4c51167bf55f68b1d42d6adff6398ea804 100644 (file)
@@ -1675,7 +1675,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
     while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
         PyObject *candidate;
         if (PyUnicode_Check(module_name) &&
-            !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
+            _PyUnicode_EqualToASCIIString(module_name, "__main__"))
             continue;
         if (module == Py_None)
             continue;
index db979f521e72ee998433a59c8f102b92b8a215a6..70d0995f83217aeca75b473e04bbec9a62ef41ea 100644 (file)
@@ -1216,7 +1216,7 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
             return -1;
         }
         for (candidate = begin_statements; *candidate; candidate++) {
-            if (!PyUnicode_CompareWithASCIIString(uppercase_level, *candidate + 6))
+            if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
                 break;
         }
         Py_DECREF(uppercase_level);
index 4ecd48a0f817787c4014298ef125146c56519f13..29a28a7a34e4cc9b77ba6a9e031729f2a6efe013 100644 (file)
@@ -2310,7 +2310,7 @@ test_string_from_format(PyObject *self, PyObject *args)
     result = PyUnicode_FromFormat(FORMAT, (TYPE)1);                 \
     if (result == NULL)                                             \
         return NULL;                                                \
-    if (PyUnicode_CompareWithASCIIString(result, "1")) {     \
+    if (!_PyUnicode_EqualToASCIIString(result, "1")) {              \
         msg = FORMAT " failed at 1";                                \
         goto Fail;                                                  \
     }                                                               \
index eb322c20d4a6a368a8bc377fae5b34f82f273efa..00c96a16c7895a9906f9bfc82da402646a8a9175 100644 (file)
@@ -1247,8 +1247,7 @@ handlername2int(PyObject *name)
 {
     int i;
     for (i = 0; handler_info[i].name != NULL; i++) {
-        if (PyUnicode_CompareWithASCIIString(
-                name, handler_info[i].name) == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, handler_info[i].name)) {
             return i;
         }
     }
@@ -1286,45 +1285,45 @@ xmlparse_getattro(xmlparseobject *self, PyObject *nameobj)
 
     first_char = PyUnicode_READ_CHAR(nameobj, 0);
     if (first_char == 'E') {
-        if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorCode") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorCode"))
             return PyLong_FromLong((long)
                                   XML_GetErrorCode(self->itself));
-        if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorLineNumber") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorLineNumber"))
             return PyLong_FromLong((long)
                                   XML_GetErrorLineNumber(self->itself));
-        if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorColumnNumber") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorColumnNumber"))
             return PyLong_FromLong((long)
                                   XML_GetErrorColumnNumber(self->itself));
-        if (PyUnicode_CompareWithASCIIString(nameobj, "ErrorByteIndex") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "ErrorByteIndex"))
             return PyLong_FromLong((long)
                                   XML_GetErrorByteIndex(self->itself));
     }
     if (first_char == 'C') {
-        if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentLineNumber") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentLineNumber"))
             return PyLong_FromLong((long)
                                   XML_GetCurrentLineNumber(self->itself));
-        if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentColumnNumber") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentColumnNumber"))
             return PyLong_FromLong((long)
                                   XML_GetCurrentColumnNumber(self->itself));
-        if (PyUnicode_CompareWithASCIIString(nameobj, "CurrentByteIndex") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "CurrentByteIndex"))
             return PyLong_FromLong((long)
                                   XML_GetCurrentByteIndex(self->itself));
     }
     if (first_char == 'b') {
-        if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_size") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_size"))
             return PyLong_FromLong((long) self->buffer_size);
-        if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_text") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_text"))
             return get_pybool(self->buffer != NULL);
-        if (PyUnicode_CompareWithASCIIString(nameobj, "buffer_used") == 0)
+        if (_PyUnicode_EqualToASCIIString(nameobj, "buffer_used"))
             return PyLong_FromLong((long) self->buffer_used);
     }
-    if (PyUnicode_CompareWithASCIIString(nameobj, "namespace_prefixes") == 0)
+    if (_PyUnicode_EqualToASCIIString(nameobj, "namespace_prefixes"))
         return get_pybool(self->ns_prefixes);
-    if (PyUnicode_CompareWithASCIIString(nameobj, "ordered_attributes") == 0)
+    if (_PyUnicode_EqualToASCIIString(nameobj, "ordered_attributes"))
         return get_pybool(self->ordered_attributes);
-    if (PyUnicode_CompareWithASCIIString(nameobj, "specified_attributes") == 0)
+    if (_PyUnicode_EqualToASCIIString(nameobj, "specified_attributes"))
         return get_pybool((long) self->specified_attributes);
-    if (PyUnicode_CompareWithASCIIString(nameobj, "intern") == 0) {
+    if (_PyUnicode_EqualToASCIIString(nameobj, "intern")) {
         if (self->intern == NULL) {
             Py_INCREF(Py_None);
             return Py_None;
@@ -1388,7 +1387,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
         PyErr_SetString(PyExc_RuntimeError, "Cannot delete attribute");
         return -1;
     }
-    if (PyUnicode_CompareWithASCIIString(name, "buffer_text") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "buffer_text")) {
         int b = PyObject_IsTrue(v);
         if (b < 0)
             return -1;
@@ -1410,7 +1409,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
         }
         return 0;
     }
-    if (PyUnicode_CompareWithASCIIString(name, "namespace_prefixes") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "namespace_prefixes")) {
         int b = PyObject_IsTrue(v);
         if (b < 0)
             return -1;
@@ -1418,14 +1417,14 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
         XML_SetReturnNSTriplet(self->itself, self->ns_prefixes);
         return 0;
     }
-    if (PyUnicode_CompareWithASCIIString(name, "ordered_attributes") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "ordered_attributes")) {
         int b = PyObject_IsTrue(v);
         if (b < 0)
             return -1;
         self->ordered_attributes = b;
         return 0;
     }
-    if (PyUnicode_CompareWithASCIIString(name, "specified_attributes") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "specified_attributes")) {
         int b = PyObject_IsTrue(v);
         if (b < 0)
             return -1;
@@ -1433,7 +1432,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
         return 0;
     }
 
-    if (PyUnicode_CompareWithASCIIString(name, "buffer_size") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "buffer_size")) {
       long new_buffer_size;
       if (!PyLong_Check(v)) {
         PyErr_SetString(PyExc_TypeError, "buffer_size must be an integer");
@@ -1479,7 +1478,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
       return 0;
     }
 
-    if (PyUnicode_CompareWithASCIIString(name, "CharacterDataHandler") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "CharacterDataHandler")) {
         /* If we're changing the character data handler, flush all
          * cached data with the old handler.  Not sure there's a
          * "right" thing to do, though, but this probably won't
index 0cc850e1bf61fc84ba0982b44aa17251f1dbc692..8510e9064856dae35c1911886cd6957815ec356b 100644 (file)
@@ -4936,9 +4936,9 @@ long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little"))
+    if (_PyUnicode_EqualToASCIIString(byteorder_str, "little"))
         little_endian = 1;
-    else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big"))
+    else if (_PyUnicode_EqualToASCIIString(byteorder_str, "big"))
         little_endian = 0;
     else {
         PyErr_SetString(PyExc_ValueError,
@@ -5019,9 +5019,9 @@ long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little"))
+    if (_PyUnicode_EqualToASCIIString(byteorder_str, "little"))
         little_endian = 1;
-    else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big"))
+    else if (_PyUnicode_EqualToASCIIString(byteorder_str, "big"))
         little_endian = 0;
     else {
         PyErr_SetString(PyExc_ValueError,
index ac07642cfb0236f39b82dfdd132904e7a36de738..ae72da6e3b52ee046d5c780d630ef94a12d1f7af 100644 (file)
@@ -587,7 +587,7 @@ _PyModule_ClearDict(PyObject *d)
     while (PyDict_Next(d, &pos, &key, &value)) {
         if (value != Py_None && PyUnicode_Check(key)) {
             if (PyUnicode_READ_CHAR(key, 0) != '_' ||
-                PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0)
+                !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
             {
                 if (Py_VerboseFlag > 1) {
                     const char *s = _PyUnicode_AsString(key);
index d65fcde25f7e64ed6f15ec89c85cb97f4f4d0965..28a2db19456039a7029c4624edcf120a03969037 100644 (file)
@@ -2395,7 +2395,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
                 }
                 add_dict++;
             }
-            if (PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0) {
+            if (_PyUnicode_EqualToASCIIString(tmp, "__weakref__")) {
                 if (!may_add_weak || add_weak) {
                     PyErr_SetString(PyExc_TypeError,
                         "__weakref__ slot disallowed: "
@@ -2419,7 +2419,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
             if ((add_dict &&
                  _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) ||
                 (add_weak &&
-                 PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
+                 _PyUnicode_EqualToASCIIString(tmp, "__weakref__")))
                 continue;
             tmp =_Py_Mangle(name, tmp);
             if (!tmp) {
index b7a3fa52715eda96eeb78fb1549983f562827386..86485bdb6a18f3fde7dcf0381324d36a918d4673 100644 (file)
@@ -10834,6 +10834,41 @@ PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
     }
 }
 
+static int
+non_ready_unicode_equal_to_ascii_string(PyObject *unicode, const char *str)
+{
+    size_t i, len;
+    const wchar_t *p;
+    len = (size_t)_PyUnicode_WSTR_LENGTH(unicode);
+    if (strlen(str) != len)
+        return 0;
+    p = _PyUnicode_WSTR(unicode);
+    assert(p);
+    for (i = 0; i < len; i++) {
+        unsigned char c = (unsigned char)str[i];
+        if (c > 128 || p[i] != (wchar_t)c)
+            return 0;
+    }
+    return 1;
+}
+
+int
+_PyUnicode_EqualToASCIIString(PyObject *unicode, const char *str)
+{
+    size_t len;
+    assert(_PyUnicode_CHECK(unicode));
+    if (PyUnicode_READY(unicode) == -1) {
+        /* Memory error or bad data */
+        PyErr_Clear();
+        return non_ready_unicode_equal_to_ascii_string(unicode, str);
+    }
+    if (!PyUnicode_IS_ASCII(unicode))
+        return 0;
+    len = (size_t)PyUnicode_GET_LENGTH(unicode);
+    return strlen(str) == len &&
+           memcmp(PyUnicode_1BYTE_DATA(unicode), str, len) == 0;
+}
+
 
 #define TEST_COND(cond)                         \
     ((cond) ? Py_True : Py_False)
index 978bad135c146bb52044bd24a54f7050729bdbd0..3928153cdb46a0912fdcda02bd59d2bb49112675 100644 (file)
@@ -431,7 +431,7 @@ warn_explicit(PyObject *category, PyObject *message,
     if (action == NULL)
         goto cleanup;
 
-    if (PyUnicode_CompareWithASCIIString(action, "error") == 0) {
+    if (_PyUnicode_EqualToASCIIString(action, "error")) {
         PyErr_SetObject(category, message);
         goto cleanup;
     }
@@ -439,13 +439,13 @@ warn_explicit(PyObject *category, PyObject *message,
     /* Store in the registry that we've been here, *except* when the action
        is "always". */
     rc = 0;
-    if (PyUnicode_CompareWithASCIIString(action, "always") != 0) {
+    if (!_PyUnicode_EqualToASCIIString(action, "always")) {
         if (registry != NULL && registry != Py_None &&
                 PyDict_SetItem(registry, key, Py_True) < 0)
             goto cleanup;
-        else if (PyUnicode_CompareWithASCIIString(action, "ignore") == 0)
+        else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
             goto return_none;
-        else if (PyUnicode_CompareWithASCIIString(action, "once") == 0) {
+        else if (_PyUnicode_EqualToASCIIString(action, "once")) {
             if (registry == NULL || registry == Py_None) {
                 registry = get_once_registry();
                 if (registry == NULL)
@@ -454,12 +454,12 @@ warn_explicit(PyObject *category, PyObject *message,
             /* _once_registry[(text, category)] = 1 */
             rc = update_registry(registry, text, category, 0);
         }
-        else if (PyUnicode_CompareWithASCIIString(action, "module") == 0) {
+        else if (_PyUnicode_EqualToASCIIString(action, "module")) {
             /* registry[(text, category, 0)] = 1 */
             if (registry != NULL && registry != Py_None)
                 rc = update_registry(registry, text, category, 0);
         }
-        else if (PyUnicode_CompareWithASCIIString(action, "default") != 0) {
+        else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
             PyErr_Format(PyExc_RuntimeError,
                         "Unrecognized action (%R) in warnings.filters:\n %R",
                         action, item);
@@ -665,7 +665,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
     }
     else {
         *filename = NULL;
-        if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
+        if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
             PyObject *argv = _PySys_GetObjectId(&PyId_argv);
             /* PyList_Check() is needed because sys.argv is set to None during
                Python finalization */
index 6da33f73a0fcb350d1fcb237dff7bad0c73322ab..5b7ed7c035eb7bc74c352d0dae595b220bfe1075 100644 (file)
@@ -876,14 +876,14 @@ forbidden_name(struct compiling *c, identifier name, const node *n,
                int full_checks)
 {
     assert(PyUnicode_Check(name));
-    if (PyUnicode_CompareWithASCIIString(name, "__debug__") == 0) {
+    if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
         ast_error(c, n, "assignment to keyword");
         return 1;
     }
     if (full_checks) {
         const char **p;
         for (p = FORBIDDEN; *p; p++) {
-            if (PyUnicode_CompareWithASCIIString(name, *p) == 0) {
+            if (_PyUnicode_EqualToASCIIString(name, *p)) {
                 ast_error(c, n, "assignment to keyword");
                 return 1;
             }
index 93f47e0220f6e8a791561d394f50a18929b4f8fe..adc33ac54ce4b45c8b5dd3be436a2c8e50e29bb8 100644 (file)
@@ -1390,7 +1390,7 @@ get_ref_type(struct compiler *c, PyObject *name)
 {
     int scope;
     if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
-        !PyUnicode_CompareWithASCIIString(name, "__class__"))
+        _PyUnicode_EqualToASCIIString(name, "__class__"))
         return CELL;
     scope = PyST_GetScope(c->u->u_ste, name);
     if (scope == 0) {
@@ -2513,7 +2513,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
     }
 
     if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
-        !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
+        _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
         Py_DECREF(level);
         Py_DECREF(names);
         return compiler_error(c, "from __future__ imports must occur "
@@ -2837,9 +2837,9 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
     if (!mangled)
         return 0;
 
-    assert(PyUnicode_CompareWithASCIIString(name, "None") &&
-           PyUnicode_CompareWithASCIIString(name, "True") &&
-           PyUnicode_CompareWithASCIIString(name, "False"));
+    assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
+           !_PyUnicode_EqualToASCIIString(name, "True") &&
+           !_PyUnicode_EqualToASCIIString(name, "False"));
 
     op = 0;
     optype = OP_NAME;
index 163f87f673da5d5c31868849c9b9e45a7c74d024..4de801bfc0a8de03e16f05e2d7d4152e7407522e 100644 (file)
@@ -99,7 +99,7 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, PyObject *filename)
         if (s->kind == ImportFrom_kind) {
             identifier modname = s->v.ImportFrom.module;
             if (modname &&
-                !PyUnicode_CompareWithASCIIString(modname, "__future__")) {
+                _PyUnicode_EqualToASCIIString(modname, "__future__")) {
                 if (done) {
                     PyErr_SetString(PyExc_SyntaxError,
                                     ERR_LATE_FUTURE);
index 8aab067865b81b91dd51b44af463b67ffbb0ac27..b10e7769e87795d92c1cc2c5d977415162245e88 100644 (file)
@@ -1618,7 +1618,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
                 return cleanreturn(0, &freelist);
             }
             for (i = 0; i < len; i++) {
-                if (!PyUnicode_CompareWithASCIIString(key, kwlist[i])) {
+                if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
                     match = 1;
                     break;
                 }
index 7e947ec2fc85189f19d5793b4eef66e5befe8978..3579273ae0fefe1e8283c9e6a005a9ea9f856401 100644 (file)
@@ -946,10 +946,9 @@ static const struct _frozen * find_frozen(PyObject *);
 static int
 is_builtin(PyObject *name)
 {
-    int i, cmp;
+    int i;
     for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
-        cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
-        if (cmp == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) {
             if (PyImport_Inittab[i].initfunc == NULL)
                 return -1;
             else
@@ -1069,7 +1068,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
 
     for (p = PyImport_Inittab; p->name != NULL; p++) {
         PyModuleDef *def;
-        if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
+        if (_PyUnicode_EqualToASCIIString(name, p->name)) {
             if (p->initfunc == NULL) {
                 /* Cannot re-init internal module ("sys" or "builtins") */
                 mod = PyImport_AddModule(namestr);
@@ -1115,7 +1114,7 @@ find_frozen(PyObject *name)
     for (p = PyImport_FrozenModules; ; p++) {
         if (p->name == NULL)
             return NULL;
-        if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
+        if (_PyUnicode_EqualToASCIIString(name, p->name))
             break;
     }
     return p;
@@ -1316,12 +1315,8 @@ remove_importlib_frames(void)
         int now_in_importlib;
 
         assert(PyTraceBack_Check(tb));
-        now_in_importlib = (PyUnicode_CompareWithASCIIString(
-                                code->co_filename,
-                                importlib_filename) == 0) ||
-                           (PyUnicode_CompareWithASCIIString(
-                                code->co_filename,
-                                external_filename) == 0);
+        now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) ||
+                           _PyUnicode_EqualToASCIIString(code->co_filename, external_filename);
         if (now_in_importlib && !in_importlib) {
             /* This is the link to this chunk of importlib tracebacks */
             outer_link = prev_link;
@@ -1330,8 +1325,7 @@ remove_importlib_frames(void)
 
         if (in_importlib &&
             (always_trim ||
-             PyUnicode_CompareWithASCIIString(code->co_name,
-                                              remove_frames) == 0)) {
+             _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) {
             PyObject *tmp = *outer_link;
             *outer_link = next;
             Py_XINCREF(next);
index 1591a20f502415f61ea4f47ba18bb11b3579b1f1..adca2dab67a36a1caec2fdda41fd93fce5563a7a 100644 (file)
@@ -1472,7 +1472,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e)
         /* Special-case super: it counts as a use of __class__ */
         if (e->v.Name.ctx == Load &&
             st->st_cur->ste_type == FunctionBlock &&
-            !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
+            _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
             if (!GET_IDENTIFIER(__class__) ||
                 !symtable_add_def(st, __class__, USE))
                 VISIT_QUIT(st, 0);
@@ -1621,7 +1621,7 @@ symtable_visit_alias(struct symtable *st, alias_ty a)
         store_name = name;
         Py_INCREF(store_name);
     }
-    if (PyUnicode_CompareWithASCIIString(name, "*")) {
+    if (!_PyUnicode_EqualToASCIIString(name, "*")) {
         int r = symtable_add_def(st, store_name, DEF_IMPORT);
         Py_DECREF(store_name);
         return r;