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