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