Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 2 | #include "pycore_pystate.h" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 3 | #include "frameobject.h" |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 4 | #include "clinic/_warnings.c.h" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 5 | |
| 6 | #define MODULE_NAME "_warnings" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 7 | |
| 8 | PyDoc_STRVAR(warnings__doc__, |
| 9 | MODULE_NAME " provides basic warning filtering support.\n" |
| 10 | "It is a helper module to speed up interpreter start-up."); |
| 11 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 12 | _Py_IDENTIFIER(stderr); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 13 | #ifndef Py_DEBUG |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 14 | _Py_IDENTIFIER(default); |
Victor Stinner | b98f171 | 2017-11-23 17:13:44 +0100 | [diff] [blame] | 15 | _Py_IDENTIFIER(ignore); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 16 | #endif |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 17 | |
| 18 | static int |
| 19 | check_matched(PyObject *obj, PyObject *arg) |
| 20 | { |
| 21 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 22 | _Py_IDENTIFIER(match); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 23 | int rc; |
| 24 | |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 25 | /* A 'None' filter always matches */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 26 | if (obj == Py_None) |
| 27 | return 1; |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 28 | |
| 29 | /* An internal plain text default filter must match exactly */ |
| 30 | if (PyUnicode_CheckExact(obj)) { |
| 31 | int cmp_result = PyUnicode_Compare(obj, arg); |
| 32 | if (cmp_result == -1 && PyErr_Occurred()) { |
| 33 | return -1; |
| 34 | } |
| 35 | return !cmp_result; |
| 36 | } |
| 37 | |
| 38 | /* Otherwise assume a regex filter and call its match() method */ |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 39 | result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 40 | if (result == NULL) |
| 41 | return -1; |
| 42 | |
| 43 | rc = PyObject_IsTrue(result); |
| 44 | Py_DECREF(result); |
| 45 | return rc; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | Returns a new reference. |
| 50 | A NULL return value can mean false or an error. |
| 51 | */ |
| 52 | static PyObject * |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 53 | get_warnings_attr(_Py_Identifier *attr_id, int try_import) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 54 | { |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 55 | PyObject *warnings_str; |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 56 | PyObject *warnings_module, *obj; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 57 | _Py_IDENTIFIER(warnings); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 58 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 59 | warnings_str = _PyUnicode_FromId(&PyId_warnings); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 60 | if (warnings_str == NULL) { |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 61 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 64 | /* don't try to import after the start of the Python finallization */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 65 | if (try_import && !_Py_IsFinalizing()) { |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 66 | warnings_module = PyImport_Import(warnings_str); |
| 67 | if (warnings_module == NULL) { |
| 68 | /* Fallback to the C implementation if we cannot get |
| 69 | the Python implementation */ |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 70 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 71 | PyErr_Clear(); |
| 72 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 73 | return NULL; |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | else { |
Nathaniel J. Smith | dba976b | 2018-01-26 11:28:31 -0800 | [diff] [blame] | 77 | /* if we're so late into Python finalization that the module dict is |
| 78 | gone, then we can't even use PyImport_GetModule without triggering |
| 79 | an interpreter abort. |
| 80 | */ |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 81 | if (!_PyInterpreterState_GET_UNSAFE()->modules) { |
Nathaniel J. Smith | dba976b | 2018-01-26 11:28:31 -0800 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 84 | warnings_module = PyImport_GetModule(warnings_str); |
Victor Stinner | 023654f | 2016-03-23 17:48:22 +0100 | [diff] [blame] | 85 | if (warnings_module == NULL) |
| 86 | return NULL; |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 89 | (void)_PyObject_LookupAttrId(warnings_module, attr_id, &obj); |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 90 | Py_DECREF(warnings_module); |
| 91 | return obj; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 95 | static PyObject * |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 96 | get_once_registry(void) |
| 97 | { |
| 98 | PyObject *registry; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 99 | _Py_IDENTIFIER(onceregistry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 100 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 101 | registry = get_warnings_attr(&PyId_onceregistry, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 102 | if (registry == NULL) { |
| 103 | if (PyErr_Occurred()) |
| 104 | return NULL; |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 105 | assert(_PyRuntime.warnings.once_registry); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 106 | return _PyRuntime.warnings.once_registry; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 107 | } |
Oren Milman | 252033d | 2017-09-11 09:28:39 +0300 | [diff] [blame] | 108 | if (!PyDict_Check(registry)) { |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 109 | PyErr_Format(PyExc_TypeError, |
| 110 | MODULE_NAME ".onceregistry must be a dict, " |
| 111 | "not '%.200s'", |
| 112 | Py_TYPE(registry)->tp_name); |
Oren Milman | 252033d | 2017-09-11 09:28:39 +0300 | [diff] [blame] | 113 | Py_DECREF(registry); |
| 114 | return NULL; |
| 115 | } |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 116 | Py_SETREF(_PyRuntime.warnings.once_registry, registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 117 | return registry; |
| 118 | } |
| 119 | |
| 120 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 121 | static PyObject * |
| 122 | get_default_action(void) |
| 123 | { |
| 124 | PyObject *default_action; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 125 | _Py_IDENTIFIER(defaultaction); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 126 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 127 | default_action = get_warnings_attr(&PyId_defaultaction, 0); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 128 | if (default_action == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | if (PyErr_Occurred()) { |
| 130 | return NULL; |
| 131 | } |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 132 | assert(_PyRuntime.warnings.default_action); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 133 | return _PyRuntime.warnings.default_action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 134 | } |
Oren Milman | 9d984fd | 2017-09-12 00:18:09 +0300 | [diff] [blame] | 135 | if (!PyUnicode_Check(default_action)) { |
| 136 | PyErr_Format(PyExc_TypeError, |
| 137 | MODULE_NAME ".defaultaction must be a string, " |
| 138 | "not '%.200s'", |
| 139 | Py_TYPE(default_action)->tp_name); |
| 140 | Py_DECREF(default_action); |
| 141 | return NULL; |
| 142 | } |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 143 | Py_SETREF(_PyRuntime.warnings.default_action, default_action); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 144 | return default_action; |
| 145 | } |
| 146 | |
| 147 | |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 148 | /* The item is a new reference. */ |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 149 | static PyObject* |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 150 | get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, |
| 151 | PyObject *module, PyObject **item) |
| 152 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 153 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 154 | Py_ssize_t i; |
| 155 | PyObject *warnings_filters; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 156 | _Py_IDENTIFIER(filters); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 157 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 158 | warnings_filters = get_warnings_attr(&PyId_filters, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 159 | if (warnings_filters == NULL) { |
| 160 | if (PyErr_Occurred()) |
| 161 | return NULL; |
| 162 | } |
| 163 | else { |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 164 | Py_SETREF(_PyRuntime.warnings.filters, warnings_filters); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 167 | PyObject *filters = _PyRuntime.warnings.filters; |
| 168 | if (filters == NULL || !PyList_Check(filters)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 169 | PyErr_SetString(PyExc_ValueError, |
| 170 | MODULE_NAME ".filters must be a list"); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 174 | /* _PyRuntime.warnings.filters could change while we are iterating over it. */ |
| 175 | for (i = 0; i < PyList_GET_SIZE(filters); i++) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 176 | PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; |
| 177 | Py_ssize_t ln; |
| 178 | int is_subclass, good_msg, good_mod; |
| 179 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 180 | tmp_item = PyList_GET_ITEM(filters, i); |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 181 | if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 182 | PyErr_Format(PyExc_ValueError, |
| 183 | MODULE_NAME ".filters item %zd isn't a 5-tuple", i); |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | /* Python code: action, msg, cat, mod, ln = item */ |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 188 | Py_INCREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 189 | action = PyTuple_GET_ITEM(tmp_item, 0); |
| 190 | msg = PyTuple_GET_ITEM(tmp_item, 1); |
| 191 | cat = PyTuple_GET_ITEM(tmp_item, 2); |
| 192 | mod = PyTuple_GET_ITEM(tmp_item, 3); |
| 193 | ln_obj = PyTuple_GET_ITEM(tmp_item, 4); |
| 194 | |
Oren Milman | 9d984fd | 2017-09-12 00:18:09 +0300 | [diff] [blame] | 195 | if (!PyUnicode_Check(action)) { |
| 196 | PyErr_Format(PyExc_TypeError, |
| 197 | "action must be a string, not '%.200s'", |
| 198 | Py_TYPE(action)->tp_name); |
| 199 | Py_DECREF(tmp_item); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 203 | good_msg = check_matched(msg, text); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 204 | if (good_msg == -1) { |
| 205 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 206 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 207 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 208 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 209 | good_mod = check_matched(mod, module); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 210 | if (good_mod == -1) { |
| 211 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 212 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 213 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 214 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 215 | is_subclass = PyObject_IsSubclass(category, cat); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 216 | if (is_subclass == -1) { |
| 217 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 218 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 219 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 220 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 221 | ln = PyLong_AsSsize_t(ln_obj); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 222 | if (ln == -1 && PyErr_Occurred()) { |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 223 | Py_DECREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 224 | return NULL; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 225 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 226 | |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 227 | if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) { |
| 228 | *item = tmp_item; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 229 | return action; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | Py_DECREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 235 | action = get_default_action(); |
| 236 | if (action != NULL) { |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 237 | Py_INCREF(Py_None); |
| 238 | *item = Py_None; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 239 | return action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 240 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 241 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 245 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 246 | static int |
| 247 | already_warned(PyObject *registry, PyObject *key, int should_set) |
| 248 | { |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 249 | PyObject *version_obj, *already_warned; |
| 250 | _Py_IDENTIFIER(version); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 251 | |
| 252 | if (key == NULL) |
| 253 | return -1; |
| 254 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 255 | version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version); |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 256 | if (version_obj == NULL |
| 257 | || !PyLong_CheckExact(version_obj) |
Serhiy Storchaka | 8905fcc | 2018-12-11 08:38:03 +0200 | [diff] [blame] | 258 | || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) |
| 259 | { |
| 260 | if (PyErr_Occurred()) { |
| 261 | return -1; |
| 262 | } |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 263 | PyDict_Clear(registry); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 264 | version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version); |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 265 | if (version_obj == NULL) |
| 266 | return -1; |
| 267 | if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) { |
| 268 | Py_DECREF(version_obj); |
| 269 | return -1; |
| 270 | } |
| 271 | Py_DECREF(version_obj); |
| 272 | } |
| 273 | else { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 274 | already_warned = PyDict_GetItemWithError(registry, key); |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 275 | if (already_warned != NULL) { |
| 276 | int rc = PyObject_IsTrue(already_warned); |
| 277 | if (rc != 0) |
| 278 | return rc; |
| 279 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 280 | else if (PyErr_Occurred()) { |
| 281 | return -1; |
| 282 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* This warning wasn't found in the registry, set it. */ |
| 286 | if (should_set) |
| 287 | return PyDict_SetItem(registry, key, Py_True); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* New reference. */ |
| 292 | static PyObject * |
| 293 | normalize_module(PyObject *filename) |
| 294 | { |
| 295 | PyObject *module; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 296 | int kind; |
| 297 | void *data; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 298 | Py_ssize_t len; |
| 299 | |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 300 | len = PyUnicode_GetLength(filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 301 | if (len < 0) |
| 302 | return NULL; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 303 | |
| 304 | if (len == 0) |
| 305 | return PyUnicode_FromString("<unknown>"); |
| 306 | |
| 307 | kind = PyUnicode_KIND(filename); |
| 308 | data = PyUnicode_DATA(filename); |
| 309 | |
| 310 | /* if filename.endswith(".py"): */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 311 | if (len >= 3 && |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 312 | PyUnicode_READ(kind, data, len-3) == '.' && |
| 313 | PyUnicode_READ(kind, data, len-2) == 'p' && |
| 314 | PyUnicode_READ(kind, data, len-1) == 'y') |
| 315 | { |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 316 | module = PyUnicode_Substring(filename, 0, len-3); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 317 | } |
| 318 | else { |
| 319 | module = filename; |
| 320 | Py_INCREF(module); |
| 321 | } |
| 322 | return module; |
| 323 | } |
| 324 | |
| 325 | static int |
| 326 | update_registry(PyObject *registry, PyObject *text, PyObject *category, |
| 327 | int add_zero) |
| 328 | { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 329 | PyObject *altkey; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 330 | int rc; |
| 331 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 332 | if (add_zero) |
| 333 | altkey = PyTuple_Pack(3, text, category, _PyLong_Zero); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 334 | else |
| 335 | altkey = PyTuple_Pack(2, text, category); |
| 336 | |
| 337 | rc = already_warned(registry, altkey, 1); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 338 | Py_XDECREF(altkey); |
| 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | static void |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 343 | show_warning(PyObject *filename, int lineno, PyObject *text, |
| 344 | PyObject *category, PyObject *sourceline) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 345 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | PyObject *f_stderr; |
| 347 | PyObject *name; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 348 | char lineno_str[128]; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 349 | _Py_IDENTIFIER(__name__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 350 | |
| 351 | PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); |
| 352 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 353 | name = _PyObject_GetAttrId(category, &PyId___name__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 354 | if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 355 | goto error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 356 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 357 | f_stderr = _PySys_GetObjectId(&PyId_stderr); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 358 | if (f_stderr == NULL) { |
| 359 | fprintf(stderr, "lost sys.stderr\n"); |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 360 | goto error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* Print "filename:lineno: category: text\n" */ |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 364 | if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0) |
| 365 | goto error; |
| 366 | if (PyFile_WriteString(lineno_str, f_stderr) < 0) |
| 367 | goto error; |
| 368 | if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0) |
| 369 | goto error; |
| 370 | if (PyFile_WriteString(": ", f_stderr) < 0) |
| 371 | goto error; |
| 372 | if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0) |
| 373 | goto error; |
| 374 | if (PyFile_WriteString("\n", f_stderr) < 0) |
| 375 | goto error; |
| 376 | Py_CLEAR(name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 377 | |
| 378 | /* Print " source_line\n" */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 379 | if (sourceline) { |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 380 | int kind; |
| 381 | void *data; |
| 382 | Py_ssize_t i, len; |
| 383 | Py_UCS4 ch; |
| 384 | PyObject *truncated; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 385 | |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 386 | if (PyUnicode_READY(sourceline) < 1) |
| 387 | goto error; |
| 388 | |
| 389 | kind = PyUnicode_KIND(sourceline); |
| 390 | data = PyUnicode_DATA(sourceline); |
| 391 | len = PyUnicode_GET_LENGTH(sourceline); |
| 392 | for (i=0; i<len; i++) { |
| 393 | ch = PyUnicode_READ(kind, data, i); |
| 394 | if (ch != ' ' && ch != '\t' && ch != '\014') |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | truncated = PyUnicode_Substring(sourceline, i, len); |
| 399 | if (truncated == NULL) |
| 400 | goto error; |
| 401 | |
| 402 | PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW); |
| 403 | Py_DECREF(truncated); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 404 | PyFile_WriteString("\n", f_stderr); |
| 405 | } |
Victor Stinner | 78e2c98 | 2013-07-16 01:54:37 +0200 | [diff] [blame] | 406 | else { |
| 407 | _Py_DisplaySourceLine(f_stderr, filename, lineno, 2); |
| 408 | } |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 409 | |
| 410 | error: |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 411 | Py_XDECREF(name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 412 | PyErr_Clear(); |
| 413 | } |
| 414 | |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 415 | static int |
| 416 | call_show_warning(PyObject *category, PyObject *text, PyObject *message, |
| 417 | PyObject *filename, int lineno, PyObject *lineno_obj, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 418 | PyObject *sourceline, PyObject *source) |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 419 | { |
| 420 | PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 421 | _Py_IDENTIFIER(_showwarnmsg); |
| 422 | _Py_IDENTIFIER(WarningMessage); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 423 | |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 424 | /* If the source parameter is set, try to get the Python implementation. |
| 425 | The Python implementation is able to log the traceback where the source |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 426 | was allocated, whereas the C implementation doesn't. */ |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 427 | show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 428 | if (show_fn == NULL) { |
| 429 | if (PyErr_Occurred()) |
| 430 | return -1; |
| 431 | show_warning(filename, lineno, text, category, sourceline); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | if (!PyCallable_Check(show_fn)) { |
| 436 | PyErr_SetString(PyExc_TypeError, |
| 437 | "warnings._showwarnmsg() must be set to a callable"); |
| 438 | goto error; |
| 439 | } |
| 440 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 441 | warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 442 | if (warnmsg_cls == NULL) { |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 443 | if (!PyErr_Occurred()) { |
| 444 | PyErr_SetString(PyExc_RuntimeError, |
| 445 | "unable to get warnings.WarningMessage"); |
| 446 | } |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 447 | goto error; |
| 448 | } |
| 449 | |
| 450 | msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 451 | filename, lineno_obj, Py_None, Py_None, source, |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 452 | NULL); |
| 453 | Py_DECREF(warnmsg_cls); |
| 454 | if (msg == NULL) |
| 455 | goto error; |
| 456 | |
Victor Stinner | de4ae3d | 2016-12-04 22:59:09 +0100 | [diff] [blame] | 457 | res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 458 | Py_DECREF(show_fn); |
| 459 | Py_DECREF(msg); |
| 460 | |
| 461 | if (res == NULL) |
| 462 | return -1; |
| 463 | |
| 464 | Py_DECREF(res); |
| 465 | return 0; |
| 466 | |
| 467 | error: |
| 468 | Py_XDECREF(show_fn); |
| 469 | return -1; |
| 470 | } |
| 471 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 472 | static PyObject * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | warn_explicit(PyObject *category, PyObject *message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 474 | PyObject *filename, int lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 475 | PyObject *module, PyObject *registry, PyObject *sourceline, |
| 476 | PyObject *source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 477 | { |
| 478 | PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 479 | PyObject *item = NULL; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 480 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 481 | int rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | |
Victor Stinner | dcdd05b | 2013-11-01 00:55:30 +0100 | [diff] [blame] | 483 | /* module can be None if a warning is emitted late during Python shutdown. |
| 484 | In this case, the Python warnings module was probably unloaded, filters |
| 485 | are no more available to choose as action. It is safer to ignore the |
| 486 | warning and do nothing. */ |
| 487 | if (module == Py_None) |
| 488 | Py_RETURN_NONE; |
| 489 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 490 | if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
Oren Milman | 252033d | 2017-09-11 09:28:39 +0300 | [diff] [blame] | 491 | PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None"); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 494 | |
| 495 | /* Normalize module. */ |
| 496 | if (module == NULL) { |
| 497 | module = normalize_module(filename); |
| 498 | if (module == NULL) |
| 499 | return NULL; |
| 500 | } |
| 501 | else |
| 502 | Py_INCREF(module); |
| 503 | |
| 504 | /* Normalize message. */ |
| 505 | Py_INCREF(message); /* DECREF'ed in cleanup. */ |
| 506 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 507 | if (rc == -1) { |
| 508 | goto cleanup; |
| 509 | } |
| 510 | if (rc == 1) { |
| 511 | text = PyObject_Str(message); |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 512 | if (text == NULL) |
| 513 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 514 | category = (PyObject*)message->ob_type; |
| 515 | } |
| 516 | else { |
| 517 | text = message; |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 518 | message = PyObject_CallFunctionObjArgs(category, message, NULL); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 519 | if (message == NULL) |
| 520 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | lineno_obj = PyLong_FromLong(lineno); |
| 524 | if (lineno_obj == NULL) |
| 525 | goto cleanup; |
| 526 | |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 527 | if (source == Py_None) { |
| 528 | source = NULL; |
| 529 | } |
| 530 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 531 | /* Create key. */ |
| 532 | key = PyTuple_Pack(3, text, category, lineno_obj); |
| 533 | if (key == NULL) |
| 534 | goto cleanup; |
| 535 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 536 | if ((registry != NULL) && (registry != Py_None)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 537 | rc = already_warned(registry, key, 0); |
| 538 | if (rc == -1) |
| 539 | goto cleanup; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | else if (rc == 1) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 541 | goto return_none; |
| 542 | /* Else this warning hasn't been generated before. */ |
| 543 | } |
| 544 | |
| 545 | action = get_filter(category, text, lineno, module, &item); |
| 546 | if (action == NULL) |
| 547 | goto cleanup; |
| 548 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 549 | if (_PyUnicode_EqualToASCIIString(action, "error")) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 550 | PyErr_SetObject(category, message); |
| 551 | goto cleanup; |
| 552 | } |
| 553 | |
Victor Stinner | c975878 | 2017-11-27 16:57:07 +0100 | [diff] [blame] | 554 | if (_PyUnicode_EqualToASCIIString(action, "ignore")) { |
| 555 | goto return_none; |
| 556 | } |
| 557 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 558 | /* Store in the registry that we've been here, *except* when the action |
| 559 | is "always". */ |
| 560 | rc = 0; |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 561 | if (!_PyUnicode_EqualToASCIIString(action, "always")) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 562 | if (registry != NULL && registry != Py_None && |
Victor Stinner | c975878 | 2017-11-27 16:57:07 +0100 | [diff] [blame] | 563 | PyDict_SetItem(registry, key, Py_True) < 0) |
| 564 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 565 | goto cleanup; |
Victor Stinner | c975878 | 2017-11-27 16:57:07 +0100 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | if (_PyUnicode_EqualToASCIIString(action, "once")) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 569 | if (registry == NULL || registry == Py_None) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 570 | registry = get_once_registry(); |
| 571 | if (registry == NULL) |
| 572 | goto cleanup; |
| 573 | } |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 574 | /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 576 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 577 | else if (_PyUnicode_EqualToASCIIString(action, "module")) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 578 | /* registry[(text, category, 0)] = 1 */ |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 579 | if (registry != NULL && registry != Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 581 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 582 | else if (!_PyUnicode_EqualToASCIIString(action, "default")) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 583 | PyErr_Format(PyExc_RuntimeError, |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 584 | "Unrecognized action (%R) in warnings.filters:\n %R", |
| 585 | action, item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 586 | goto cleanup; |
| 587 | } |
| 588 | } |
| 589 | |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 590 | if (rc == 1) /* Already warned for this module. */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 591 | goto return_none; |
| 592 | if (rc == 0) { |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 593 | if (call_show_warning(category, text, message, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 594 | lineno_obj, sourceline, source) < 0) |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 595 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 596 | } |
| 597 | else /* if (rc == -1) */ |
| 598 | goto cleanup; |
| 599 | |
| 600 | return_none: |
| 601 | result = Py_None; |
| 602 | Py_INCREF(result); |
| 603 | |
| 604 | cleanup: |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 605 | Py_XDECREF(item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 606 | Py_XDECREF(key); |
| 607 | Py_XDECREF(text); |
| 608 | Py_XDECREF(lineno_obj); |
| 609 | Py_DECREF(module); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 610 | Py_XDECREF(message); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 611 | return result; /* Py_None or NULL. */ |
| 612 | } |
| 613 | |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 614 | static int |
| 615 | is_internal_frame(PyFrameObject *frame) |
| 616 | { |
| 617 | static PyObject *importlib_string = NULL; |
| 618 | static PyObject *bootstrap_string = NULL; |
| 619 | PyObject *filename; |
| 620 | int contains; |
| 621 | |
| 622 | if (importlib_string == NULL) { |
| 623 | importlib_string = PyUnicode_FromString("importlib"); |
| 624 | if (importlib_string == NULL) { |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | bootstrap_string = PyUnicode_FromString("_bootstrap"); |
| 629 | if (bootstrap_string == NULL) { |
| 630 | Py_DECREF(importlib_string); |
| 631 | return 0; |
| 632 | } |
| 633 | Py_INCREF(importlib_string); |
| 634 | Py_INCREF(bootstrap_string); |
| 635 | } |
| 636 | |
| 637 | if (frame == NULL || frame->f_code == NULL || |
| 638 | frame->f_code->co_filename == NULL) { |
| 639 | return 0; |
| 640 | } |
| 641 | filename = frame->f_code->co_filename; |
| 642 | if (!PyUnicode_Check(filename)) { |
| 643 | return 0; |
| 644 | } |
| 645 | contains = PyUnicode_Contains(filename, importlib_string); |
| 646 | if (contains < 0) { |
| 647 | return 0; |
| 648 | } |
| 649 | else if (contains > 0) { |
| 650 | contains = PyUnicode_Contains(filename, bootstrap_string); |
| 651 | if (contains < 0) { |
| 652 | return 0; |
| 653 | } |
| 654 | else if (contains > 0) { |
| 655 | return 1; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static PyFrameObject * |
| 663 | next_external_frame(PyFrameObject *frame) |
| 664 | { |
| 665 | do { |
| 666 | frame = frame->f_back; |
| 667 | } while (frame != NULL && is_internal_frame(frame)); |
| 668 | |
| 669 | return frame; |
| 670 | } |
| 671 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 672 | /* filename, module, and registry are new refs, globals is borrowed */ |
| 673 | /* Returns 0 on error (no new refs), 1 on success */ |
| 674 | static int |
| 675 | setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, |
| 676 | PyObject **module, PyObject **registry) |
| 677 | { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 678 | _Py_IDENTIFIER(__warningregistry__); |
| 679 | _Py_IDENTIFIER(__name__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 680 | PyObject *globals; |
| 681 | |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 682 | /* Setup globals, filename and lineno. */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 683 | PyFrameObject *f = _PyThreadState_GET()->frame; |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 684 | // Stack level comparisons to Python code is off by one as there is no |
| 685 | // warnings-related stack level to avoid. |
| 686 | if (stack_level <= 0 || is_internal_frame(f)) { |
| 687 | while (--stack_level > 0 && f != NULL) { |
| 688 | f = f->f_back; |
| 689 | } |
| 690 | } |
| 691 | else { |
| 692 | while (--stack_level > 0 && f != NULL) { |
| 693 | f = next_external_frame(f); |
| 694 | } |
| 695 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 696 | |
| 697 | if (f == NULL) { |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 698 | globals = _PyInterpreterState_GET_UNSAFE()->sysdict; |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 699 | *filename = PyUnicode_FromString("sys"); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 700 | *lineno = 1; |
| 701 | } |
| 702 | else { |
| 703 | globals = f->f_globals; |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 704 | *filename = f->f_code->co_filename; |
| 705 | Py_INCREF(*filename); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 706 | *lineno = PyFrame_GetLineNumber(f); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | *module = NULL; |
| 710 | |
| 711 | /* Setup registry. */ |
| 712 | assert(globals != NULL); |
| 713 | assert(PyDict_Check(globals)); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 714 | *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 715 | if (*registry == NULL) { |
| 716 | int rc; |
| 717 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 718 | if (PyErr_Occurred()) { |
| 719 | return 0; |
| 720 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 721 | *registry = PyDict_New(); |
| 722 | if (*registry == NULL) |
| 723 | return 0; |
| 724 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 725 | rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 726 | if (rc < 0) |
| 727 | goto handle_error; |
| 728 | } |
| 729 | else |
| 730 | Py_INCREF(*registry); |
| 731 | |
| 732 | /* Setup module. */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 733 | *module = _PyDict_GetItemIdWithError(globals, &PyId___name__); |
Oren Milman | 5d3e800 | 2017-09-24 21:28:42 +0300 | [diff] [blame] | 734 | if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) { |
| 735 | Py_INCREF(*module); |
| 736 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 737 | else if (PyErr_Occurred()) { |
| 738 | goto handle_error; |
| 739 | } |
Oren Milman | 5d3e800 | 2017-09-24 21:28:42 +0300 | [diff] [blame] | 740 | else { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 741 | *module = PyUnicode_FromString("<string>"); |
| 742 | if (*module == NULL) |
| 743 | goto handle_error; |
| 744 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 745 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 746 | return 1; |
| 747 | |
| 748 | handle_error: |
| 749 | /* filename not XDECREF'ed here as there is no way to jump here with a |
| 750 | dangling reference. */ |
| 751 | Py_XDECREF(*registry); |
| 752 | Py_XDECREF(*module); |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static PyObject * |
| 757 | get_category(PyObject *message, PyObject *category) |
| 758 | { |
| 759 | int rc; |
| 760 | |
| 761 | /* Get category. */ |
| 762 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 763 | if (rc == -1) |
| 764 | return NULL; |
| 765 | |
| 766 | if (rc == 1) |
| 767 | category = (PyObject*)message->ob_type; |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 768 | else if (category == NULL || category == Py_None) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 769 | category = PyExc_UserWarning; |
| 770 | |
| 771 | /* Validate category. */ |
| 772 | rc = PyObject_IsSubclass(category, PyExc_Warning); |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 773 | /* category is not a subclass of PyExc_Warning or |
| 774 | PyObject_IsSubclass raised an error */ |
| 775 | if (rc == -1 || rc == 0) { |
| 776 | PyErr_Format(PyExc_TypeError, |
| 777 | "category must be a Warning subclass, not '%s'", |
| 778 | Py_TYPE(category)->tp_name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 779 | return NULL; |
| 780 | } |
| 781 | |
| 782 | return category; |
| 783 | } |
| 784 | |
| 785 | static PyObject * |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 786 | do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level, |
| 787 | PyObject *source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 788 | { |
| 789 | PyObject *filename, *module, *registry, *res; |
| 790 | int lineno; |
| 791 | |
| 792 | if (!setup_context(stack_level, &filename, &lineno, &module, ®istry)) |
| 793 | return NULL; |
| 794 | |
Victor Stinner | dcdd05b | 2013-11-01 00:55:30 +0100 | [diff] [blame] | 795 | res = warn_explicit(category, message, filename, lineno, module, registry, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 796 | NULL, source); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 797 | Py_DECREF(filename); |
| 798 | Py_DECREF(registry); |
| 799 | Py_DECREF(module); |
| 800 | return res; |
| 801 | } |
| 802 | |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 803 | /*[clinic input] |
| 804 | warn as warnings_warn |
| 805 | |
| 806 | message: object |
| 807 | category: object = None |
| 808 | stacklevel: Py_ssize_t = 1 |
| 809 | source: object = None |
| 810 | |
| 811 | Issue a warning, or maybe ignore it or raise an exception. |
| 812 | [clinic start generated code]*/ |
| 813 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 814 | static PyObject * |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 815 | warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category, |
| 816 | Py_ssize_t stacklevel, PyObject *source) |
| 817 | /*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 818 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 819 | category = get_category(message, category); |
| 820 | if (category == NULL) |
| 821 | return NULL; |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 822 | return do_warn(message, category, stacklevel, source); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static PyObject * |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 826 | get_source_line(PyObject *module_globals, int lineno) |
| 827 | { |
| 828 | _Py_IDENTIFIER(get_source); |
| 829 | _Py_IDENTIFIER(__loader__); |
| 830 | _Py_IDENTIFIER(__name__); |
| 831 | PyObject *loader; |
| 832 | PyObject *module_name; |
| 833 | PyObject *get_source; |
| 834 | PyObject *source; |
| 835 | PyObject *source_list; |
| 836 | PyObject *source_line; |
| 837 | |
| 838 | /* Check/get the requisite pieces needed for the loader. */ |
| 839 | loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__); |
| 840 | if (loader == NULL) { |
| 841 | return NULL; |
| 842 | } |
| 843 | Py_INCREF(loader); |
| 844 | module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__); |
| 845 | if (!module_name) { |
| 846 | Py_DECREF(loader); |
| 847 | return NULL; |
| 848 | } |
| 849 | Py_INCREF(module_name); |
| 850 | |
| 851 | /* Make sure the loader implements the optional get_source() method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 852 | (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source); |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 853 | Py_DECREF(loader); |
| 854 | if (!get_source) { |
| 855 | Py_DECREF(module_name); |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 856 | return NULL; |
| 857 | } |
| 858 | /* Call get_source() to get the source code. */ |
| 859 | source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL); |
| 860 | Py_DECREF(get_source); |
| 861 | Py_DECREF(module_name); |
| 862 | if (!source) { |
| 863 | return NULL; |
| 864 | } |
| 865 | if (source == Py_None) { |
| 866 | Py_DECREF(source); |
| 867 | return NULL; |
| 868 | } |
| 869 | |
| 870 | /* Split the source into lines. */ |
| 871 | source_list = PyUnicode_Splitlines(source, 0); |
| 872 | Py_DECREF(source); |
| 873 | if (!source_list) { |
| 874 | return NULL; |
| 875 | } |
| 876 | |
| 877 | /* Get the source line. */ |
| 878 | source_line = PyList_GetItem(source_list, lineno-1); |
| 879 | Py_XINCREF(source_line); |
| 880 | Py_DECREF(source_list); |
| 881 | return source_line; |
| 882 | } |
| 883 | |
| 884 | static PyObject * |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 885 | warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) |
| 886 | { |
| 887 | static char *kwd_list[] = {"message", "category", "filename", "lineno", |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 888 | "module", "registry", "module_globals", |
| 889 | "source", 0}; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 890 | PyObject *message; |
| 891 | PyObject *category; |
| 892 | PyObject *filename; |
| 893 | int lineno; |
| 894 | PyObject *module = NULL; |
| 895 | PyObject *registry = NULL; |
| 896 | PyObject *module_globals = NULL; |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 897 | PyObject *sourceobj = NULL; |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 898 | PyObject *source_line = NULL; |
| 899 | PyObject *returned; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 900 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 901 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit", |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 902 | kwd_list, &message, &category, &filename, &lineno, &module, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 903 | ®istry, &module_globals, &sourceobj)) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 904 | return NULL; |
| 905 | |
Victor Stinner | b056562 | 2018-05-15 20:42:12 +0200 | [diff] [blame] | 906 | if (module_globals && module_globals != Py_None) { |
| 907 | if (!PyDict_Check(module_globals)) { |
| 908 | PyErr_Format(PyExc_TypeError, |
| 909 | "module_globals must be a dict, not '%.200s'", |
| 910 | Py_TYPE(module_globals)->tp_name); |
| 911 | return NULL; |
| 912 | } |
| 913 | |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 914 | source_line = get_source_line(module_globals, lineno); |
| 915 | if (source_line == NULL && PyErr_Occurred()) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 916 | return NULL; |
| 917 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 918 | } |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 919 | returned = warn_explicit(category, message, filename, lineno, module, |
| 920 | registry, source_line, sourceobj); |
| 921 | Py_XDECREF(source_line); |
| 922 | return returned; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 925 | static PyObject * |
| 926 | warnings_filters_mutated(PyObject *self, PyObject *args) |
| 927 | { |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 928 | _PyRuntime.warnings.filters_version++; |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 929 | Py_RETURN_NONE; |
| 930 | } |
| 931 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 932 | |
| 933 | /* Function to issue a warning message; may raise an exception. */ |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 934 | |
| 935 | static int |
| 936 | warn_unicode(PyObject *category, PyObject *message, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 937 | Py_ssize_t stack_level, PyObject *source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 938 | { |
| 939 | PyObject *res; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 940 | |
| 941 | if (category == NULL) |
| 942 | category = PyExc_RuntimeWarning; |
| 943 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 944 | res = do_warn(message, category, stack_level, source); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 945 | if (res == NULL) |
| 946 | return -1; |
| 947 | Py_DECREF(res); |
| 948 | |
| 949 | return 0; |
| 950 | } |
| 951 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 952 | static int |
| 953 | _PyErr_WarnFormatV(PyObject *source, |
| 954 | PyObject *category, Py_ssize_t stack_level, |
| 955 | const char *format, va_list vargs) |
| 956 | { |
| 957 | PyObject *message; |
| 958 | int res; |
| 959 | |
| 960 | message = PyUnicode_FromFormatV(format, vargs); |
| 961 | if (message == NULL) |
| 962 | return -1; |
| 963 | |
| 964 | res = warn_unicode(category, message, stack_level, source); |
| 965 | Py_DECREF(message); |
| 966 | return res; |
| 967 | } |
| 968 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 969 | int |
| 970 | PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, |
| 971 | const char *format, ...) |
| 972 | { |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 973 | int res; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 974 | va_list vargs; |
| 975 | |
| 976 | #ifdef HAVE_STDARG_PROTOTYPES |
| 977 | va_start(vargs, format); |
| 978 | #else |
| 979 | va_start(vargs); |
| 980 | #endif |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 981 | res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs); |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 982 | va_end(vargs); |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 983 | return res; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | int |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 987 | PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, |
| 988 | const char *format, ...) |
| 989 | { |
| 990 | int res; |
| 991 | va_list vargs; |
| 992 | |
| 993 | #ifdef HAVE_STDARG_PROTOTYPES |
| 994 | va_start(vargs, format); |
| 995 | #else |
| 996 | va_start(vargs); |
| 997 | #endif |
| 998 | res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning, |
| 999 | stack_level, format, vargs); |
| 1000 | va_end(vargs); |
| 1001 | return res; |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | int |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1006 | PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
| 1007 | { |
| 1008 | int ret; |
| 1009 | PyObject *message = PyUnicode_FromString(text); |
| 1010 | if (message == NULL) |
| 1011 | return -1; |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1012 | ret = warn_unicode(category, message, stack_level, NULL); |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1013 | Py_DECREF(message); |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1017 | /* PyErr_Warn is only for backwards compatibility and will be removed. |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1018 | Use PyErr_WarnEx instead. */ |
| 1019 | |
| 1020 | #undef PyErr_Warn |
| 1021 | |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 1022 | int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1023 | PyErr_Warn(PyObject *category, const char *text) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1024 | { |
| 1025 | return PyErr_WarnEx(category, text, 1); |
| 1026 | } |
| 1027 | |
| 1028 | /* Warning with explicit origin */ |
| 1029 | int |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1030 | PyErr_WarnExplicitObject(PyObject *category, PyObject *message, |
| 1031 | PyObject *filename, int lineno, |
| 1032 | PyObject *module, PyObject *registry) |
| 1033 | { |
| 1034 | PyObject *res; |
| 1035 | if (category == NULL) |
| 1036 | category = PyExc_RuntimeWarning; |
| 1037 | res = warn_explicit(category, message, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1038 | module, registry, NULL, NULL); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1039 | if (res == NULL) |
| 1040 | return -1; |
| 1041 | Py_DECREF(res); |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | int |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1046 | PyErr_WarnExplicit(PyObject *category, const char *text, |
| 1047 | const char *filename_str, int lineno, |
| 1048 | const char *module_str, PyObject *registry) |
| 1049 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1050 | PyObject *message = PyUnicode_FromString(text); |
Victor Stinner | cb428f0 | 2010-12-27 20:10:36 +0000 | [diff] [blame] | 1051 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1052 | PyObject *module = NULL; |
| 1053 | int ret = -1; |
| 1054 | |
| 1055 | if (message == NULL || filename == NULL) |
| 1056 | goto exit; |
| 1057 | if (module_str != NULL) { |
| 1058 | module = PyUnicode_FromString(module_str); |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1059 | if (module == NULL) |
| 1060 | goto exit; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1063 | ret = PyErr_WarnExplicitObject(category, message, filename, lineno, |
| 1064 | module, registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1065 | |
| 1066 | exit: |
| 1067 | Py_XDECREF(message); |
| 1068 | Py_XDECREF(module); |
| 1069 | Py_XDECREF(filename); |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1073 | int |
| 1074 | PyErr_WarnExplicitFormat(PyObject *category, |
| 1075 | const char *filename_str, int lineno, |
| 1076 | const char *module_str, PyObject *registry, |
| 1077 | const char *format, ...) |
| 1078 | { |
| 1079 | PyObject *message; |
| 1080 | PyObject *module = NULL; |
| 1081 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
| 1082 | int ret = -1; |
| 1083 | va_list vargs; |
| 1084 | |
| 1085 | if (filename == NULL) |
| 1086 | goto exit; |
| 1087 | if (module_str != NULL) { |
| 1088 | module = PyUnicode_FromString(module_str); |
| 1089 | if (module == NULL) |
| 1090 | goto exit; |
| 1091 | } |
| 1092 | |
| 1093 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1094 | va_start(vargs, format); |
| 1095 | #else |
| 1096 | va_start(vargs); |
| 1097 | #endif |
| 1098 | message = PyUnicode_FromFormatV(format, vargs); |
| 1099 | if (message != NULL) { |
| 1100 | PyObject *res; |
| 1101 | res = warn_explicit(category, message, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1102 | module, registry, NULL, NULL); |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1103 | Py_DECREF(message); |
| 1104 | if (res != NULL) { |
| 1105 | Py_DECREF(res); |
| 1106 | ret = 0; |
| 1107 | } |
| 1108 | } |
| 1109 | va_end(vargs); |
| 1110 | exit: |
| 1111 | Py_XDECREF(module); |
| 1112 | Py_XDECREF(filename); |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1116 | void |
| 1117 | _PyErr_WarnUnawaitedCoroutine(PyObject *coro) |
| 1118 | { |
| 1119 | /* First, we attempt to funnel the warning through |
| 1120 | warnings._warn_unawaited_coroutine. |
| 1121 | |
| 1122 | This could raise an exception, due to: |
| 1123 | - a bug |
| 1124 | - some kind of shutdown-related brokenness |
| 1125 | - succeeding, but with an "error" warning filter installed, so the |
| 1126 | warning is converted into a RuntimeWarning exception |
| 1127 | |
| 1128 | In the first two cases, we want to print the error (so we know what it |
| 1129 | is!), and then print a warning directly as a fallback. In the last |
| 1130 | case, we want to print the error (since it's the warning!), but *not* |
| 1131 | do a fallback. And after we print the error we can't check for what |
| 1132 | type of error it was (because PyErr_WriteUnraisable clears it), so we |
| 1133 | need a flag to keep track. |
| 1134 | |
| 1135 | Since this is called from __del__ context, it's careful to never raise |
| 1136 | an exception. |
| 1137 | */ |
| 1138 | _Py_IDENTIFIER(_warn_unawaited_coroutine); |
| 1139 | int warned = 0; |
| 1140 | PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1); |
| 1141 | if (fn) { |
| 1142 | PyObject *res = PyObject_CallFunctionObjArgs(fn, coro, NULL); |
| 1143 | Py_DECREF(fn); |
| 1144 | if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) { |
| 1145 | warned = 1; |
| 1146 | } |
| 1147 | Py_XDECREF(res); |
| 1148 | } |
| 1149 | |
| 1150 | if (PyErr_Occurred()) { |
| 1151 | PyErr_WriteUnraisable(coro); |
| 1152 | } |
| 1153 | if (!warned) { |
Yury Selivanov | 3510334 | 2018-01-21 20:47:04 -0500 | [diff] [blame] | 1154 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 1155 | "coroutine '%.50S' was never awaited", |
| 1156 | ((PyCoroObject *)coro)->cr_qualname) < 0) |
| 1157 | { |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1158 | PyErr_WriteUnraisable(coro); |
| 1159 | } |
| 1160 | } |
| 1161 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1162 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1163 | PyDoc_STRVAR(warn_explicit_doc, |
| 1164 | "Low-level inferface to warnings functionality."); |
| 1165 | |
| 1166 | static PyMethodDef warnings_functions[] = { |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 1167 | WARNINGS_WARN_METHODDEF |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1168 | {"warn_explicit", (PyCFunction)(void(*)(void))warnings_warn_explicit, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1169 | METH_VARARGS | METH_KEYWORDS, warn_explicit_doc}, |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1170 | {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS, |
| 1171 | NULL}, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 1172 | /* XXX(brett.cannon): add showwarning? */ |
| 1173 | /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | {NULL, NULL} /* sentinel */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1175 | }; |
| 1176 | |
| 1177 | |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1178 | #ifndef Py_DEBUG |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1179 | static PyObject * |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1180 | create_filter(PyObject *category, _Py_Identifier *id, const char *modname) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1181 | { |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1182 | PyObject *modname_obj = NULL; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 1183 | PyObject *action_str = _PyUnicode_FromId(id); |
| 1184 | if (action_str == NULL) { |
| 1185 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1188 | /* Default to "no module name" for initial filter set */ |
| 1189 | if (modname != NULL) { |
| 1190 | modname_obj = PyUnicode_InternFromString(modname); |
| 1191 | if (modname_obj == NULL) { |
| 1192 | return NULL; |
| 1193 | } |
| 1194 | } else { |
| 1195 | modname_obj = Py_None; |
| 1196 | } |
| 1197 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1198 | /* This assumes the line number is zero for now. */ |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 1199 | return PyTuple_Pack(5, action_str, Py_None, |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1200 | category, modname_obj, _PyLong_Zero); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1201 | } |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1202 | #endif |
| 1203 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1204 | |
| 1205 | static PyObject * |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1206 | init_filters(void) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1207 | { |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1208 | #ifdef Py_DEBUG |
| 1209 | /* Py_DEBUG builds show all warnings by default */ |
| 1210 | return PyList_New(0); |
| 1211 | #else |
| 1212 | /* Other builds ignore a number of warning categories by default */ |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1213 | PyObject *filters = PyList_New(5); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1214 | if (filters == NULL) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1215 | return NULL; |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1216 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1217 | |
Victor Stinner | b98f171 | 2017-11-23 17:13:44 +0100 | [diff] [blame] | 1218 | size_t pos = 0; /* Post-incremented in each use. */ |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1219 | PyList_SET_ITEM(filters, pos++, |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1220 | create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__")); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1221 | PyList_SET_ITEM(filters, pos++, |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1222 | create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL)); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1223 | PyList_SET_ITEM(filters, pos++, |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1224 | create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL)); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1225 | PyList_SET_ITEM(filters, pos++, |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 1226 | create_filter(PyExc_ImportWarning, &PyId_ignore, NULL)); |
| 1227 | PyList_SET_ITEM(filters, pos++, |
| 1228 | create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL)); |
Victor Stinner | 09f3a8a | 2017-11-20 17:32:40 -0800 | [diff] [blame] | 1229 | |
Victor Stinner | b98f171 | 2017-11-23 17:13:44 +0100 | [diff] [blame] | 1230 | for (size_t x = 0; x < pos; x++) { |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1231 | if (PyList_GET_ITEM(filters, x) == NULL) { |
| 1232 | Py_DECREF(filters); |
| 1233 | return NULL; |
| 1234 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1235 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1236 | return filters; |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 1237 | #endif |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1240 | static struct PyModuleDef warningsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | PyModuleDef_HEAD_INIT, |
| 1242 | MODULE_NAME, |
| 1243 | warnings__doc__, |
| 1244 | 0, |
| 1245 | warnings_functions, |
| 1246 | NULL, |
| 1247 | NULL, |
| 1248 | NULL, |
| 1249 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1250 | }; |
| 1251 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1252 | |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1253 | PyMODINIT_FUNC |
| 1254 | _PyWarnings_Init(void) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1255 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 1256 | PyObject *m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1257 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1258 | m = PyModule_Create(&warningsmodule); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1259 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1260 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1261 | |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1262 | struct _warnings_runtime_state *state = &_PyRuntime.warnings; |
| 1263 | if (state->filters == NULL) { |
| 1264 | state->filters = init_filters(); |
| 1265 | if (state->filters == NULL) |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1266 | return NULL; |
| 1267 | } |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1268 | Py_INCREF(state->filters); |
| 1269 | if (PyModule_AddObject(m, "filters", state->filters) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1270 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1271 | |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1272 | if (state->once_registry == NULL) { |
| 1273 | state->once_registry = PyDict_New(); |
| 1274 | if (state->once_registry == NULL) |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1275 | return NULL; |
| 1276 | } |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1277 | Py_INCREF(state->once_registry); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1278 | if (PyModule_AddObject(m, "_onceregistry", |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1279 | state->once_registry) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1280 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1281 | |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1282 | if (state->default_action == NULL) { |
| 1283 | state->default_action = PyUnicode_FromString("default"); |
| 1284 | if (state->default_action == NULL) |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1285 | return NULL; |
| 1286 | } |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1287 | Py_INCREF(state->default_action); |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1288 | if (PyModule_AddObject(m, "_defaultaction", |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1289 | state->default_action) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1290 | return NULL; |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1291 | |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1292 | state->filters_version = 0; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1293 | return m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1294 | } |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1295 | |
| 1296 | |
| 1297 | void |
| 1298 | _PyWarnings_Fini(_PyRuntimeState *runtime) |
| 1299 | { |
| 1300 | struct _warnings_runtime_state *state = &runtime->warnings; |
| 1301 | Py_CLEAR(state->filters); |
| 1302 | Py_CLEAR(state->once_registry); |
| 1303 | Py_CLEAR(state->default_action); |
| 1304 | } |