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