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