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