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