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:
Serhiy Storchaka 2016-11-16 10:19:57 +02:00
commit 1a73bf365e
22 changed files with 125 additions and 80 deletions

View file

@ -503,7 +503,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;
}
@ -511,13 +511,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)
@ -526,12 +526,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);
@ -715,7 +715,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 */

View file

@ -934,12 +934,12 @@ 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 (PyUnicode_CompareWithASCIIString(name, "async") == 0 ||
PyUnicode_CompareWithASCIIString(name, "await") == 0)
if (_PyUnicode_EqualToASCIIString(name, "async") ||
_PyUnicode_EqualToASCIIString(name, "await"))
{
PyObject *message = PyUnicode_FromString(
"'async' and 'await' will become reserved keywords"
@ -963,7 +963,7 @@ forbidden_name(struct compiling *c, identifier name, const node *n,
if (full_checks) {
const char * const *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;
}

View file

@ -1522,7 +1522,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) {
@ -2688,7 +2688,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 "
@ -3027,9 +3027,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;

View file

@ -102,7 +102,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);

View file

@ -1808,7 +1808,7 @@ vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
return cleanreturn(0, &freelist);
}
for (i = 0; i < len; i++) {
if (*kwlist[i] && !PyUnicode_CompareWithASCIIString(key, kwlist[i])) {
if (*kwlist[i] && _PyUnicode_EqualToASCIIString(key, kwlist[i])) {
match = 1;
break;
}

View file

@ -936,10 +936,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
@ -1059,7 +1058,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);
@ -1109,7 +1108,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;
@ -1310,12 +1309,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;
@ -1324,8 +1319,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))) {
Py_XINCREF(next);
Py_XSETREF(*outer_link, next);
prev_link = outer_link;

View file

@ -1503,7 +1503,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);
@ -1652,7 +1652,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;