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

Skip to content

Commit 1a73bf3

Browse files
Issue #28701: Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIIString.
The latter function is more readable, faster and doesn't raise exceptions.
2 parents 1e2784e + 3b73ea1 commit 1a73bf3

22 files changed

Lines changed: 125 additions & 80 deletions

Doc/c-api/unicode.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,9 @@ They all return *NULL* or ``-1`` if an exception occurs.
16431643
Compare two strings and return ``-1``, ``0``, ``1`` for less than, equal, and greater than,
16441644
respectively.
16451645
1646+
This function returns ``-1`` upon failure, so one should call
1647+
:c:func:`PyErr_Occurred` to check for errors.
1648+
16461649
16471650
.. c:function:: int PyUnicode_CompareWithASCIIString(PyObject *uni, const char *string)
16481651
@@ -1651,6 +1654,9 @@ They all return *NULL* or ``-1`` if an exception occurs.
16511654
ASCII-encoded strings, but the function interprets the input string as
16521655
ISO-8859-1 if it contains non-ASCII characters.
16531656
1657+
This function returns ``-1`` upon failure, so one should call
1658+
:c:func:`PyErr_Occurred` to check for errors.
1659+
16541660
16551661
.. c:function:: PyObject* PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
16561662

Include/unicodeobject.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,17 @@ PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
20482048
const char *right /* ASCII-encoded string */
20492049
);
20502050

2051+
#ifndef Py_LIMITED_API
2052+
/* Test whether a unicode is equal to ASCII string. Return 1 if true,
2053+
0 otherwise. Return 0 if any argument contains non-ASCII characters.
2054+
Any error occurs inside will be cleared before return. */
2055+
2056+
PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
2057+
PyObject *left,
2058+
const char *right /* ASCII-encoded string */
2059+
);
2060+
#endif
2061+
20512062
/* Rich compare two strings and return one of the following:
20522063
20532064
- NULL in case an exception was raised

Modules/_decimal/_decimal.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,12 +1118,12 @@ context_getattr(PyObject *self, PyObject *name)
11181118
PyObject *retval;
11191119

11201120
if (PyUnicode_Check(name)) {
1121-
if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
1121+
if (_PyUnicode_EqualToASCIIString(name, "traps")) {
11221122
retval = ((PyDecContextObject *)self)->traps;
11231123
Py_INCREF(retval);
11241124
return retval;
11251125
}
1126-
if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
1126+
if (_PyUnicode_EqualToASCIIString(name, "flags")) {
11271127
retval = ((PyDecContextObject *)self)->flags;
11281128
Py_INCREF(retval);
11291129
return retval;
@@ -1143,10 +1143,10 @@ context_setattr(PyObject *self, PyObject *name, PyObject *value)
11431143
}
11441144

11451145
if (PyUnicode_Check(name)) {
1146-
if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
1146+
if (_PyUnicode_EqualToASCIIString(name, "traps")) {
11471147
return context_settraps_dict(self, value);
11481148
}
1149-
if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
1149+
if (_PyUnicode_EqualToASCIIString(name, "flags")) {
11501150
return context_setstatus_dict(self, value);
11511151
}
11521152
}
@@ -2449,14 +2449,14 @@ dectuple_as_str(PyObject *dectuple)
24492449
tmp = PyTuple_GET_ITEM(dectuple, 2);
24502450
if (PyUnicode_Check(tmp)) {
24512451
/* special */
2452-
if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) {
2452+
if (_PyUnicode_EqualToASCIIString(tmp, "F")) {
24532453
strcat(sign_special, "Inf");
24542454
is_infinite = 1;
24552455
}
2456-
else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) {
2456+
else if (_PyUnicode_EqualToASCIIString(tmp, "n")) {
24572457
strcat(sign_special, "NaN");
24582458
}
2459-
else if (PyUnicode_CompareWithASCIIString(tmp, "N") == 0) {
2459+
else if (_PyUnicode_EqualToASCIIString(tmp, "N")) {
24602460
strcat(sign_special, "sNaN");
24612461
}
24622462
else {

Modules/_elementtree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3661,11 +3661,11 @@ xmlparser_getattro(XMLParserObject* self, PyObject* nameobj)
36613661
{
36623662
if (PyUnicode_Check(nameobj)) {
36633663
PyObject* res;
3664-
if (PyUnicode_CompareWithASCIIString(nameobj, "entity") == 0)
3664+
if (_PyUnicode_EqualToASCIIString(nameobj, "entity"))
36653665
res = self->entity;
3666-
else if (PyUnicode_CompareWithASCIIString(nameobj, "target") == 0)
3666+
else if (_PyUnicode_EqualToASCIIString(nameobj, "target"))
36673667
res = self->target;
3668-
else if (PyUnicode_CompareWithASCIIString(nameobj, "version") == 0) {
3668+
else if (_PyUnicode_EqualToASCIIString(nameobj, "version")) {
36693669
return PyUnicode_FromFormat(
36703670
"Expat %d.%d.%d", XML_MAJOR_VERSION,
36713671
XML_MINOR_VERSION, XML_MICRO_VERSION);

Modules/_io/textio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
10231023
else if (PyUnicode_Check(res)) {
10241024
const encodefuncentry *e = encodefuncs;
10251025
while (e->name != NULL) {
1026-
if (!PyUnicode_CompareWithASCIIString(res, e->name)) {
1026+
if (_PyUnicode_EqualToASCIIString(res, e->name)) {
10271027
self->encodefunc = e->encodefunc;
10281028
break;
10291029
}

Modules/_io/winconsoleio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ char _PyIO_get_console_type(PyObject *path_or_fd) {
9393
}
9494

9595
char m = '\0';
96-
if (PyUnicode_CompareWithASCIIString(decoded_upper, "CONIN$") == 0) {
96+
if (_PyUnicode_EqualToASCIIString(decoded_upper, "CONIN$")) {
9797
m = 'r';
98-
} else if (PyUnicode_CompareWithASCIIString(decoded_upper, "CONOUT$") == 0) {
98+
} else if (_PyUnicode_EqualToASCIIString(decoded_upper, "CONOUT$")) {
9999
m = 'w';
100-
} else if (PyUnicode_CompareWithASCIIString(decoded_upper, "CON") == 0) {
100+
} else if (_PyUnicode_EqualToASCIIString(decoded_upper, "CON")) {
101101
m = 'x';
102102
}
103103

Modules/_lsprof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ normalizeUserObj(PyObject *obj)
181181
}
182182
}
183183
if (modname != NULL) {
184-
if (PyUnicode_CompareWithASCIIString(modname, "builtins") != 0) {
184+
if (!_PyUnicode_EqualToASCIIString(modname, "builtins")) {
185185
PyObject *result;
186186
result = PyUnicode_FromFormat("<%U.%s>", modname,
187187
fn->m_ml->ml_name);

Modules/_pickle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
16871687
while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
16881688
PyObject *candidate;
16891689
if (PyUnicode_Check(module_name) &&
1690-
!PyUnicode_CompareWithASCIIString(module_name, "__main__"))
1690+
_PyUnicode_EqualToASCIIString(module_name, "__main__"))
16911691
continue;
16921692
if (module == Py_None)
16931693
continue;

Modules/_sqlite/connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
11911191
return -1;
11921192
}
11931193
for (candidate = begin_statements; *candidate; candidate++) {
1194-
if (!PyUnicode_CompareWithASCIIString(uppercase_level, *candidate + 6))
1194+
if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
11951195
break;
11961196
}
11971197
Py_DECREF(uppercase_level);

Modules/_testcapimodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,7 @@ test_string_from_format(PyObject *self, PyObject *args)
23252325
result = PyUnicode_FromFormat(FORMAT, (TYPE)1); \
23262326
if (result == NULL) \
23272327
return NULL; \
2328-
if (PyUnicode_CompareWithASCIIString(result, "1")) { \
2328+
if (!_PyUnicode_EqualToASCIIString(result, "1")) { \
23292329
msg = FORMAT " failed at 1"; \
23302330
goto Fail; \
23312331
} \

0 commit comments

Comments
 (0)