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