Skip to content

Commit b98f171

Browse files
authored
bpo-27535: Cleanup create_filter() (#4516)
create_filter() now expects the action as a _Py_Identifier which avoids string comparison, and more important, to avoid handling the "unknown action" annoying case.
1 parent 0327bde commit b98f171

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

Python/_warnings.c

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ MODULE_NAME " provides basic warning filtering support.\n"
1111

1212
_Py_IDENTIFIER(argv);
1313
_Py_IDENTIFIER(stderr);
14+
_Py_IDENTIFIER(ignore);
15+
_Py_IDENTIFIER(error);
16+
_Py_IDENTIFIER(always);
17+
_Py_static_string(PyId_default, "default");
1418

1519
static int
1620
check_matched(PyObject *obj, PyObject *arg)
@@ -1149,31 +1153,8 @@ static PyMethodDef warnings_functions[] = {
11491153

11501154

11511155
static PyObject *
1152-
create_filter(PyObject *category, const char *action)
1156+
create_filter(PyObject *category, _Py_Identifier *id)
11531157
{
1154-
_Py_IDENTIFIER(ignore);
1155-
_Py_IDENTIFIER(error);
1156-
_Py_IDENTIFIER(always);
1157-
_Py_static_string(PyId_default, "default");
1158-
_Py_Identifier *id;
1159-
1160-
if (!strcmp(action, "ignore")) {
1161-
id = &PyId_ignore;
1162-
}
1163-
else if (!strcmp(action, "error")) {
1164-
id = &PyId_error;
1165-
}
1166-
else if (!strcmp(action, "default")) {
1167-
id = &PyId_default;
1168-
}
1169-
else if (!strcmp(action, "always")) {
1170-
id = &PyId_always;
1171-
}
1172-
else {
1173-
PyErr_SetString(PyExc_ValueError, "unknown action");
1174-
return NULL;
1175-
}
1176-
11771158
PyObject *action_str = _PyUnicode_FromId(id);
11781159
if (action_str == NULL) {
11791160
return NULL;
@@ -1199,48 +1180,47 @@ init_filters(const _PyCoreConfig *config)
11991180
}
12001181
#endif
12011182
PyObject *filters = PyList_New(count);
1202-
unsigned int pos = 0; /* Post-incremented in each use. */
1203-
unsigned int x;
1204-
const char *bytes_action, *resource_action;
1205-
12061183
if (filters == NULL)
12071184
return NULL;
12081185

1186+
size_t pos = 0; /* Post-incremented in each use. */
12091187
#ifndef Py_DEBUG
12101188
if (!dev_mode) {
12111189
PyList_SET_ITEM(filters, pos++,
1212-
create_filter(PyExc_DeprecationWarning, "ignore"));
1190+
create_filter(PyExc_DeprecationWarning, &PyId_ignore));
12131191
PyList_SET_ITEM(filters, pos++,
1214-
create_filter(PyExc_PendingDeprecationWarning, "ignore"));
1192+
create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore));
12151193
PyList_SET_ITEM(filters, pos++,
1216-
create_filter(PyExc_ImportWarning, "ignore"));
1194+
create_filter(PyExc_ImportWarning, &PyId_ignore));
12171195
}
12181196
#endif
12191197

1198+
_Py_Identifier *bytes_action;
12201199
if (Py_BytesWarningFlag > 1)
1221-
bytes_action = "error";
1200+
bytes_action = &PyId_error;
12221201
else if (Py_BytesWarningFlag)
1223-
bytes_action = "default";
1202+
bytes_action = &PyId_default;
12241203
else
1225-
bytes_action = "ignore";
1204+
bytes_action = &PyId_ignore;
12261205
PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
12271206
bytes_action));
12281207

1208+
_Py_Identifier *resource_action;
12291209
/* resource usage warnings are enabled by default in pydebug mode */
12301210
#ifdef Py_DEBUG
1231-
resource_action = "always";
1211+
resource_action = &PyId_always;
12321212
#else
1233-
resource_action = (dev_mode ? "always" : "ignore");
1213+
resource_action = (dev_mode ? &PyId_always : &PyId_ignore);
12341214
#endif
12351215
PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
12361216
resource_action));
12371217

12381218
if (dev_mode) {
12391219
PyList_SET_ITEM(filters, pos++,
1240-
create_filter(PyExc_Warning, "default"));
1220+
create_filter(PyExc_Warning, &PyId_default));
12411221
}
12421222

1243-
for (x = 0; x < pos; x += 1) {
1223+
for (size_t x = 0; x < pos; x++) {
12441224
if (PyList_GET_ITEM(filters, x) == NULL) {
12451225
Py_DECREF(filters);
12461226
return NULL;

0 commit comments

Comments
 (0)