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