mirror of
https://github.com/python/cpython.git
synced 2025-07-19 17:25:54 +00:00
Issue #28701: Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIIString.
The latter function is more readable, faster and doesn't raise exceptions.
This commit is contained in:
parent
5ebff7b300
commit
f4934ea77d
21 changed files with 120 additions and 75 deletions
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue