blob: 7eedd1374cbacf5ddd621f4fb46f0b7909e2c6d7 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01002#include "pycore_pystate.h"
Christian Heimes33fe8092008-04-13 13:53:33 +00003#include "frameobject.h"
Victor Stinner22f18752016-12-09 18:08:18 +01004#include "clinic/_warnings.c.h"
Christian Heimes33fe8092008-04-13 13:53:33 +00005
6#define MODULE_NAME "_warnings"
Christian Heimes33fe8092008-04-13 13:53:33 +00007
8PyDoc_STRVAR(warnings__doc__,
9MODULE_NAME " provides basic warning filtering support.\n"
10"It is a helper module to speed up interpreter start-up.");
11
Victor Stinnerbd303c12013-11-07 23:07:29 +010012_Py_IDENTIFIER(stderr);
Victor Stinner747f48e2017-12-12 22:59:48 +010013#ifndef Py_DEBUG
Nick Coghlan9b997472018-01-08 12:45:02 +100014_Py_IDENTIFIER(default);
Victor Stinnerb98f1712017-11-23 17:13:44 +010015_Py_IDENTIFIER(ignore);
Victor Stinner747f48e2017-12-12 22:59:48 +010016#endif
Christian Heimes33fe8092008-04-13 13:53:33 +000017
18static int
19check_matched(PyObject *obj, PyObject *arg)
20{
21 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020022 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000023 int rc;
24
Nick Coghlan9b997472018-01-08 12:45:02 +100025 /* A 'None' filter always matches */
Christian Heimes33fe8092008-04-13 13:53:33 +000026 if (obj == Py_None)
27 return 1;
Nick Coghlan9b997472018-01-08 12:45:02 +100028
29 /* An internal plain text default filter must match exactly */
30 if (PyUnicode_CheckExact(obj)) {
31 int cmp_result = PyUnicode_Compare(obj, arg);
32 if (cmp_result == -1 && PyErr_Occurred()) {
33 return -1;
34 }
35 return !cmp_result;
36 }
37
38 /* Otherwise assume a regex filter and call its match() method */
Victor Stinner55ba38a2016-12-09 16:09:30 +010039 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000040 if (result == NULL)
41 return -1;
42
43 rc = PyObject_IsTrue(result);
44 Py_DECREF(result);
45 return rc;
46}
47
48/*
49 Returns a new reference.
50 A NULL return value can mean false or an error.
51*/
52static PyObject *
Victor Stinner82656272017-11-22 23:51:42 +010053get_warnings_attr(_Py_Identifier *attr_id, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000054{
Victor Stinner82656272017-11-22 23:51:42 +010055 PyObject *warnings_str;
Victor Stinnere98445a2016-03-23 00:54:48 +010056 PyObject *warnings_module, *obj;
Victor Stinner82656272017-11-22 23:51:42 +010057 _Py_IDENTIFIER(warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000058
Victor Stinner82656272017-11-22 23:51:42 +010059 warnings_str = _PyUnicode_FromId(&PyId_warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000060 if (warnings_str == NULL) {
Victor Stinner82656272017-11-22 23:51:42 +010061 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +000062 }
63
Victor Stinnere98445a2016-03-23 00:54:48 +010064 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060065 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010066 warnings_module = PyImport_Import(warnings_str);
67 if (warnings_module == NULL) {
68 /* Fallback to the C implementation if we cannot get
69 the Python implementation */
Serhiy Storchakad4f84802017-11-11 15:19:47 +020070 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
71 PyErr_Clear();
72 }
Christian Heimes33fe8092008-04-13 13:53:33 +000073 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010074 }
75 }
76 else {
Nathaniel J. Smithdba976b2018-01-26 11:28:31 -080077 /* if we're so late into Python finalization that the module dict is
78 gone, then we can't even use PyImport_GetModule without triggering
79 an interpreter abort.
80 */
Victor Stinnercaba55b2018-08-03 15:33:52 +020081 if (!_PyInterpreterState_GET_UNSAFE()->modules) {
Nathaniel J. Smithdba976b2018-01-26 11:28:31 -080082 return NULL;
83 }
Eric Snow3f9eee62017-09-15 16:35:20 -060084 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010085 if (warnings_module == NULL)
86 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010087 }
88
Serhiy Storchakaf320be72018-01-25 10:49:40 +020089 (void)_PyObject_LookupAttrId(warnings_module, attr_id, &obj);
Victor Stinnere98445a2016-03-23 00:54:48 +010090 Py_DECREF(warnings_module);
91 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000092}
93
94
Neal Norwitz32dde222008-04-15 06:43:13 +000095static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000096get_once_registry(void)
97{
98 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +010099 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000100
Victor Stinner82656272017-11-22 23:51:42 +0100101 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000102 if (registry == NULL) {
103 if (PyErr_Occurred())
104 return NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200105 assert(_PyRuntime.warnings.once_registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600106 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +0000107 }
Oren Milman252033d2017-09-11 09:28:39 +0300108 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200109 PyErr_Format(PyExc_TypeError,
110 MODULE_NAME ".onceregistry must be a dict, "
111 "not '%.200s'",
112 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +0300113 Py_DECREF(registry);
114 return NULL;
115 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200116 Py_SETREF(_PyRuntime.warnings.once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000117 return registry;
118}
119
120
Brett Cannon0759dd62009-04-01 18:13:07 +0000121static PyObject *
122get_default_action(void)
123{
124 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100125 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000126
Victor Stinner82656272017-11-22 23:51:42 +0100127 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000128 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 if (PyErr_Occurred()) {
130 return NULL;
131 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200132 assert(_PyRuntime.warnings.default_action);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600133 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000134 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300135 if (!PyUnicode_Check(default_action)) {
136 PyErr_Format(PyExc_TypeError,
137 MODULE_NAME ".defaultaction must be a string, "
138 "not '%.200s'",
139 Py_TYPE(default_action)->tp_name);
140 Py_DECREF(default_action);
141 return NULL;
142 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200143 Py_SETREF(_PyRuntime.warnings.default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000144 return default_action;
145}
146
147
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400148/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100149static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000150get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
151 PyObject *module, PyObject **item)
152{
Brett Cannon0759dd62009-04-01 18:13:07 +0000153 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000154 Py_ssize_t i;
155 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100156 _Py_IDENTIFIER(filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000157
Victor Stinner82656272017-11-22 23:51:42 +0100158 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000159 if (warnings_filters == NULL) {
160 if (PyErr_Occurred())
161 return NULL;
162 }
163 else {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200164 Py_SETREF(_PyRuntime.warnings.filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000165 }
166
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600167 PyObject *filters = _PyRuntime.warnings.filters;
168 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000169 PyErr_SetString(PyExc_ValueError,
170 MODULE_NAME ".filters must be a list");
171 return NULL;
172 }
173
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600174 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
175 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000176 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
177 Py_ssize_t ln;
178 int is_subclass, good_msg, good_mod;
179
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600180 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400181 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000182 PyErr_Format(PyExc_ValueError,
183 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
184 return NULL;
185 }
186
187 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400188 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000189 action = PyTuple_GET_ITEM(tmp_item, 0);
190 msg = PyTuple_GET_ITEM(tmp_item, 1);
191 cat = PyTuple_GET_ITEM(tmp_item, 2);
192 mod = PyTuple_GET_ITEM(tmp_item, 3);
193 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
194
Oren Milman9d984fd2017-09-12 00:18:09 +0300195 if (!PyUnicode_Check(action)) {
196 PyErr_Format(PyExc_TypeError,
197 "action must be a string, not '%.200s'",
198 Py_TYPE(action)->tp_name);
199 Py_DECREF(tmp_item);
200 return NULL;
201 }
202
Christian Heimes33fe8092008-04-13 13:53:33 +0000203 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400204 if (good_msg == -1) {
205 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100206 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400207 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100208
Christian Heimes33fe8092008-04-13 13:53:33 +0000209 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400210 if (good_mod == -1) {
211 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100212 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400213 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100214
Christian Heimes33fe8092008-04-13 13:53:33 +0000215 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400216 if (is_subclass == -1) {
217 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100218 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400219 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100220
Christian Heimes33fe8092008-04-13 13:53:33 +0000221 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400222 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400223 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000224 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400225 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000226
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400227 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
228 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100229 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400230 }
231
232 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000233 }
234
Brett Cannon0759dd62009-04-01 18:13:07 +0000235 action = get_default_action();
236 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400237 Py_INCREF(Py_None);
238 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100239 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000240 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000241
Christian Heimes33fe8092008-04-13 13:53:33 +0000242 return NULL;
243}
244
Brett Cannon0759dd62009-04-01 18:13:07 +0000245
Christian Heimes33fe8092008-04-13 13:53:33 +0000246static int
247already_warned(PyObject *registry, PyObject *key, int should_set)
248{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200249 PyObject *version_obj, *already_warned;
250 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000251
252 if (key == NULL)
253 return -1;
254
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200255 version_obj = _PyDict_GetItemId(registry, &PyId_version);
256 if (version_obj == NULL
257 || !PyLong_CheckExact(version_obj)
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +0200258 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version)
259 {
260 if (PyErr_Occurred()) {
261 return -1;
262 }
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200263 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600264 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200265 if (version_obj == NULL)
266 return -1;
267 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
268 Py_DECREF(version_obj);
269 return -1;
270 }
271 Py_DECREF(version_obj);
272 }
273 else {
274 already_warned = PyDict_GetItem(registry, key);
275 if (already_warned != NULL) {
276 int rc = PyObject_IsTrue(already_warned);
277 if (rc != 0)
278 return rc;
279 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000280 }
281
282 /* This warning wasn't found in the registry, set it. */
283 if (should_set)
284 return PyDict_SetItem(registry, key, Py_True);
285 return 0;
286}
287
288/* New reference. */
289static PyObject *
290normalize_module(PyObject *filename)
291{
292 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100293 int kind;
294 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000295 Py_ssize_t len;
296
Victor Stinner9e30aa52011-11-21 02:49:52 +0100297 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000298 if (len < 0)
299 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100300
301 if (len == 0)
302 return PyUnicode_FromString("<unknown>");
303
304 kind = PyUnicode_KIND(filename);
305 data = PyUnicode_DATA(filename);
306
307 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000308 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100309 PyUnicode_READ(kind, data, len-3) == '.' &&
310 PyUnicode_READ(kind, data, len-2) == 'p' &&
311 PyUnicode_READ(kind, data, len-1) == 'y')
312 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100313 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000314 }
315 else {
316 module = filename;
317 Py_INCREF(module);
318 }
319 return module;
320}
321
322static int
323update_registry(PyObject *registry, PyObject *text, PyObject *category,
324 int add_zero)
325{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300326 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000327 int rc;
328
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300329 if (add_zero)
330 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000331 else
332 altkey = PyTuple_Pack(2, text, category);
333
334 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000335 Py_XDECREF(altkey);
336 return rc;
337}
338
339static void
Victor Stinner914cde82016-03-19 01:03:51 +0100340show_warning(PyObject *filename, int lineno, PyObject *text,
341 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 PyObject *f_stderr;
344 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000345 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200346 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000347
348 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
349
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200350 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000351 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100352 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000353
Victor Stinnerbd303c12013-11-07 23:07:29 +0100354 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000355 if (f_stderr == NULL) {
356 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100357 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000358 }
359
360 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100361 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
362 goto error;
363 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
364 goto error;
365 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
366 goto error;
367 if (PyFile_WriteString(": ", f_stderr) < 0)
368 goto error;
369 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
370 goto error;
371 if (PyFile_WriteString("\n", f_stderr) < 0)
372 goto error;
373 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000374
375 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000376 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100377 int kind;
378 void *data;
379 Py_ssize_t i, len;
380 Py_UCS4 ch;
381 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000382
Victor Stinnera4c704b2013-10-29 23:43:41 +0100383 if (PyUnicode_READY(sourceline) < 1)
384 goto error;
385
386 kind = PyUnicode_KIND(sourceline);
387 data = PyUnicode_DATA(sourceline);
388 len = PyUnicode_GET_LENGTH(sourceline);
389 for (i=0; i<len; i++) {
390 ch = PyUnicode_READ(kind, data, i);
391 if (ch != ' ' && ch != '\t' && ch != '\014')
392 break;
393 }
394
395 truncated = PyUnicode_Substring(sourceline, i, len);
396 if (truncated == NULL)
397 goto error;
398
399 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
400 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000401 PyFile_WriteString("\n", f_stderr);
402 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200403 else {
404 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
405 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100406
407error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100408 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000409 PyErr_Clear();
410}
411
Victor Stinner1231a462016-03-19 00:47:17 +0100412static int
413call_show_warning(PyObject *category, PyObject *text, PyObject *message,
414 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100415 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100416{
417 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100418 _Py_IDENTIFIER(_showwarnmsg);
419 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100420
Victor Stinnere98445a2016-03-23 00:54:48 +0100421 /* If the source parameter is set, try to get the Python implementation.
422 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600423 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100424 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100425 if (show_fn == NULL) {
426 if (PyErr_Occurred())
427 return -1;
428 show_warning(filename, lineno, text, category, sourceline);
429 return 0;
430 }
431
432 if (!PyCallable_Check(show_fn)) {
433 PyErr_SetString(PyExc_TypeError,
434 "warnings._showwarnmsg() must be set to a callable");
435 goto error;
436 }
437
Victor Stinner82656272017-11-22 23:51:42 +0100438 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100439 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200440 if (!PyErr_Occurred()) {
441 PyErr_SetString(PyExc_RuntimeError,
442 "unable to get warnings.WarningMessage");
443 }
Victor Stinner1231a462016-03-19 00:47:17 +0100444 goto error;
445 }
446
447 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100448 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100449 NULL);
450 Py_DECREF(warnmsg_cls);
451 if (msg == NULL)
452 goto error;
453
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100454 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100455 Py_DECREF(show_fn);
456 Py_DECREF(msg);
457
458 if (res == NULL)
459 return -1;
460
461 Py_DECREF(res);
462 return 0;
463
464error:
465 Py_XDECREF(show_fn);
466 return -1;
467}
468
Christian Heimes33fe8092008-04-13 13:53:33 +0000469static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000471 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100472 PyObject *module, PyObject *registry, PyObject *sourceline,
473 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000474{
475 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400476 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100477 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000478 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100480 /* module can be None if a warning is emitted late during Python shutdown.
481 In this case, the Python warnings module was probably unloaded, filters
482 are no more available to choose as action. It is safer to ignore the
483 warning and do nothing. */
484 if (module == Py_None)
485 Py_RETURN_NONE;
486
Brett Cannondb734912008-06-27 00:52:15 +0000487 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300488 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000489 return NULL;
490 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000491
492 /* Normalize module. */
493 if (module == NULL) {
494 module = normalize_module(filename);
495 if (module == NULL)
496 return NULL;
497 }
498 else
499 Py_INCREF(module);
500
501 /* Normalize message. */
502 Py_INCREF(message); /* DECREF'ed in cleanup. */
503 rc = PyObject_IsInstance(message, PyExc_Warning);
504 if (rc == -1) {
505 goto cleanup;
506 }
507 if (rc == 1) {
508 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000509 if (text == NULL)
510 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000511 category = (PyObject*)message->ob_type;
512 }
513 else {
514 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100515 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000516 if (message == NULL)
517 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000518 }
519
520 lineno_obj = PyLong_FromLong(lineno);
521 if (lineno_obj == NULL)
522 goto cleanup;
523
Victor Stinner22f18752016-12-09 18:08:18 +0100524 if (source == Py_None) {
525 source = NULL;
526 }
527
Christian Heimes33fe8092008-04-13 13:53:33 +0000528 /* Create key. */
529 key = PyTuple_Pack(3, text, category, lineno_obj);
530 if (key == NULL)
531 goto cleanup;
532
Brett Cannondb734912008-06-27 00:52:15 +0000533 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000534 rc = already_warned(registry, key, 0);
535 if (rc == -1)
536 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000538 goto return_none;
539 /* Else this warning hasn't been generated before. */
540 }
541
542 action = get_filter(category, text, lineno, module, &item);
543 if (action == NULL)
544 goto cleanup;
545
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200546 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000547 PyErr_SetObject(category, message);
548 goto cleanup;
549 }
550
Victor Stinnerc9758782017-11-27 16:57:07 +0100551 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
552 goto return_none;
553 }
554
Christian Heimes33fe8092008-04-13 13:53:33 +0000555 /* Store in the registry that we've been here, *except* when the action
556 is "always". */
557 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200558 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000559 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100560 PyDict_SetItem(registry, key, Py_True) < 0)
561 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000562 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100563 }
564
565 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000566 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000567 registry = get_once_registry();
568 if (registry == NULL)
569 goto cleanup;
570 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600571 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000573 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200574 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000575 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000576 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000578 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200579 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000580 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100581 "Unrecognized action (%R) in warnings.filters:\n %R",
582 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000583 goto cleanup;
584 }
585 }
586
Christian Heimes1a8501c2008-10-02 19:56:01 +0000587 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000588 goto return_none;
589 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100590 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100591 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100592 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000593 }
594 else /* if (rc == -1) */
595 goto cleanup;
596
597 return_none:
598 result = Py_None;
599 Py_INCREF(result);
600
601 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400602 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000603 Py_XDECREF(key);
604 Py_XDECREF(text);
605 Py_XDECREF(lineno_obj);
606 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000607 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000608 return result; /* Py_None or NULL. */
609}
610
Larry Hastings714e4932015-09-06 00:39:37 -0700611static int
612is_internal_frame(PyFrameObject *frame)
613{
614 static PyObject *importlib_string = NULL;
615 static PyObject *bootstrap_string = NULL;
616 PyObject *filename;
617 int contains;
618
619 if (importlib_string == NULL) {
620 importlib_string = PyUnicode_FromString("importlib");
621 if (importlib_string == NULL) {
622 return 0;
623 }
624
625 bootstrap_string = PyUnicode_FromString("_bootstrap");
626 if (bootstrap_string == NULL) {
627 Py_DECREF(importlib_string);
628 return 0;
629 }
630 Py_INCREF(importlib_string);
631 Py_INCREF(bootstrap_string);
632 }
633
634 if (frame == NULL || frame->f_code == NULL ||
635 frame->f_code->co_filename == NULL) {
636 return 0;
637 }
638 filename = frame->f_code->co_filename;
639 if (!PyUnicode_Check(filename)) {
640 return 0;
641 }
642 contains = PyUnicode_Contains(filename, importlib_string);
643 if (contains < 0) {
644 return 0;
645 }
646 else if (contains > 0) {
647 contains = PyUnicode_Contains(filename, bootstrap_string);
648 if (contains < 0) {
649 return 0;
650 }
651 else if (contains > 0) {
652 return 1;
653 }
654 }
655
656 return 0;
657}
658
659static PyFrameObject *
660next_external_frame(PyFrameObject *frame)
661{
662 do {
663 frame = frame->f_back;
664 } while (frame != NULL && is_internal_frame(frame));
665
666 return frame;
667}
668
Christian Heimes33fe8092008-04-13 13:53:33 +0000669/* filename, module, and registry are new refs, globals is borrowed */
670/* Returns 0 on error (no new refs), 1 on success */
671static int
672setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
673 PyObject **module, PyObject **registry)
674{
675 PyObject *globals;
676
Thomas Kluyver11a89662018-06-08 21:28:37 +0200677 /* Setup globals, filename and lineno. */
Victor Stinner50b48572018-11-01 01:51:40 +0100678 PyFrameObject *f = _PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700679 // Stack level comparisons to Python code is off by one as there is no
680 // warnings-related stack level to avoid.
681 if (stack_level <= 0 || is_internal_frame(f)) {
682 while (--stack_level > 0 && f != NULL) {
683 f = f->f_back;
684 }
685 }
686 else {
687 while (--stack_level > 0 && f != NULL) {
688 f = next_external_frame(f);
689 }
690 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000691
692 if (f == NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200693 globals = _PyInterpreterState_GET_UNSAFE()->sysdict;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200694 *filename = PyUnicode_FromString("sys");
Christian Heimes33fe8092008-04-13 13:53:33 +0000695 *lineno = 1;
696 }
697 else {
698 globals = f->f_globals;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200699 *filename = f->f_code->co_filename;
700 Py_INCREF(*filename);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000701 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000702 }
703
704 *module = NULL;
705
706 /* Setup registry. */
707 assert(globals != NULL);
708 assert(PyDict_Check(globals));
709 *registry = PyDict_GetItemString(globals, "__warningregistry__");
710 if (*registry == NULL) {
711 int rc;
712
713 *registry = PyDict_New();
714 if (*registry == NULL)
715 return 0;
716
717 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
718 if (rc < 0)
719 goto handle_error;
720 }
721 else
722 Py_INCREF(*registry);
723
724 /* Setup module. */
725 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300726 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
727 Py_INCREF(*module);
728 }
729 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000730 *module = PyUnicode_FromString("<string>");
731 if (*module == NULL)
732 goto handle_error;
733 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000734
Christian Heimes33fe8092008-04-13 13:53:33 +0000735 return 1;
736
737 handle_error:
738 /* filename not XDECREF'ed here as there is no way to jump here with a
739 dangling reference. */
740 Py_XDECREF(*registry);
741 Py_XDECREF(*module);
742 return 0;
743}
744
745static PyObject *
746get_category(PyObject *message, PyObject *category)
747{
748 int rc;
749
750 /* Get category. */
751 rc = PyObject_IsInstance(message, PyExc_Warning);
752 if (rc == -1)
753 return NULL;
754
755 if (rc == 1)
756 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300757 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000758 category = PyExc_UserWarning;
759
760 /* Validate category. */
761 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300762 /* category is not a subclass of PyExc_Warning or
763 PyObject_IsSubclass raised an error */
764 if (rc == -1 || rc == 0) {
765 PyErr_Format(PyExc_TypeError,
766 "category must be a Warning subclass, not '%s'",
767 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000768 return NULL;
769 }
770
771 return category;
772}
773
774static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100775do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
776 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000777{
778 PyObject *filename, *module, *registry, *res;
779 int lineno;
780
781 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
782 return NULL;
783
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100784 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100785 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000786 Py_DECREF(filename);
787 Py_DECREF(registry);
788 Py_DECREF(module);
789 return res;
790}
791
Victor Stinner22f18752016-12-09 18:08:18 +0100792/*[clinic input]
793warn as warnings_warn
794
795 message: object
796 category: object = None
797 stacklevel: Py_ssize_t = 1
798 source: object = None
799
800Issue a warning, or maybe ignore it or raise an exception.
801[clinic start generated code]*/
802
Christian Heimes33fe8092008-04-13 13:53:33 +0000803static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100804warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
805 Py_ssize_t stacklevel, PyObject *source)
806/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000807{
Christian Heimes33fe8092008-04-13 13:53:33 +0000808 category = get_category(message, category);
809 if (category == NULL)
810 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100811 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000812}
813
814static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200815get_source_line(PyObject *module_globals, int lineno)
816{
817 _Py_IDENTIFIER(get_source);
818 _Py_IDENTIFIER(__loader__);
819 _Py_IDENTIFIER(__name__);
820 PyObject *loader;
821 PyObject *module_name;
822 PyObject *get_source;
823 PyObject *source;
824 PyObject *source_list;
825 PyObject *source_line;
826
827 /* Check/get the requisite pieces needed for the loader. */
828 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
829 if (loader == NULL) {
830 return NULL;
831 }
832 Py_INCREF(loader);
833 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
834 if (!module_name) {
835 Py_DECREF(loader);
836 return NULL;
837 }
838 Py_INCREF(module_name);
839
840 /* Make sure the loader implements the optional get_source() method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200841 (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200842 Py_DECREF(loader);
843 if (!get_source) {
844 Py_DECREF(module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200845 return NULL;
846 }
847 /* Call get_source() to get the source code. */
848 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
849 Py_DECREF(get_source);
850 Py_DECREF(module_name);
851 if (!source) {
852 return NULL;
853 }
854 if (source == Py_None) {
855 Py_DECREF(source);
856 return NULL;
857 }
858
859 /* Split the source into lines. */
860 source_list = PyUnicode_Splitlines(source, 0);
861 Py_DECREF(source);
862 if (!source_list) {
863 return NULL;
864 }
865
866 /* Get the source line. */
867 source_line = PyList_GetItem(source_list, lineno-1);
868 Py_XINCREF(source_line);
869 Py_DECREF(source_list);
870 return source_line;
871}
872
873static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000874warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
875{
876 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100877 "module", "registry", "module_globals",
878 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000879 PyObject *message;
880 PyObject *category;
881 PyObject *filename;
882 int lineno;
883 PyObject *module = NULL;
884 PyObject *registry = NULL;
885 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100886 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200887 PyObject *source_line = NULL;
888 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000889
Victor Stinner914cde82016-03-19 01:03:51 +0100890 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000891 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100892 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000893 return NULL;
894
Victor Stinnerb0565622018-05-15 20:42:12 +0200895 if (module_globals && module_globals != Py_None) {
896 if (!PyDict_Check(module_globals)) {
897 PyErr_Format(PyExc_TypeError,
898 "module_globals must be a dict, not '%.200s'",
899 Py_TYPE(module_globals)->tp_name);
900 return NULL;
901 }
902
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200903 source_line = get_source_line(module_globals, lineno);
904 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000905 return NULL;
906 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000907 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200908 returned = warn_explicit(category, message, filename, lineno, module,
909 registry, source_line, sourceobj);
910 Py_XDECREF(source_line);
911 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000912}
913
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200914static PyObject *
915warnings_filters_mutated(PyObject *self, PyObject *args)
916{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600917 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200918 Py_RETURN_NONE;
919}
920
Christian Heimes33fe8092008-04-13 13:53:33 +0000921
922/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000923
924static int
925warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100926 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000927{
928 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000929
930 if (category == NULL)
931 category = PyExc_RuntimeWarning;
932
Victor Stinner914cde82016-03-19 01:03:51 +0100933 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000934 if (res == NULL)
935 return -1;
936 Py_DECREF(res);
937
938 return 0;
939}
940
Victor Stinner914cde82016-03-19 01:03:51 +0100941static int
942_PyErr_WarnFormatV(PyObject *source,
943 PyObject *category, Py_ssize_t stack_level,
944 const char *format, va_list vargs)
945{
946 PyObject *message;
947 int res;
948
949 message = PyUnicode_FromFormatV(format, vargs);
950 if (message == NULL)
951 return -1;
952
953 res = warn_unicode(category, message, stack_level, source);
954 Py_DECREF(message);
955 return res;
956}
957
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000958int
959PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
960 const char *format, ...)
961{
Victor Stinner914cde82016-03-19 01:03:51 +0100962 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000963 va_list vargs;
964
965#ifdef HAVE_STDARG_PROTOTYPES
966 va_start(vargs, format);
967#else
968 va_start(vargs);
969#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100970 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000971 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100972 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000973}
974
975int
Victor Stinner914cde82016-03-19 01:03:51 +0100976PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
977 const char *format, ...)
978{
979 int res;
980 va_list vargs;
981
982#ifdef HAVE_STDARG_PROTOTYPES
983 va_start(vargs, format);
984#else
985 va_start(vargs);
986#endif
987 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
988 stack_level, format, vargs);
989 va_end(vargs);
990 return res;
991}
992
993
994int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000995PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
996{
997 int ret;
998 PyObject *message = PyUnicode_FromString(text);
999 if (message == NULL)
1000 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001001 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001002 Py_DECREF(message);
1003 return ret;
1004}
1005
Ezio Melotti42da6632011-03-15 05:18:48 +02001006/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001007 Use PyErr_WarnEx instead. */
1008
1009#undef PyErr_Warn
1010
Benjamin Petersone5024512018-09-12 12:06:42 -07001011int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001012PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001013{
1014 return PyErr_WarnEx(category, text, 1);
1015}
1016
1017/* Warning with explicit origin */
1018int
Victor Stinner14e461d2013-08-26 22:28:21 +02001019PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1020 PyObject *filename, int lineno,
1021 PyObject *module, PyObject *registry)
1022{
1023 PyObject *res;
1024 if (category == NULL)
1025 category = PyExc_RuntimeWarning;
1026 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001027 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001028 if (res == NULL)
1029 return -1;
1030 Py_DECREF(res);
1031 return 0;
1032}
1033
1034int
Christian Heimes33fe8092008-04-13 13:53:33 +00001035PyErr_WarnExplicit(PyObject *category, const char *text,
1036 const char *filename_str, int lineno,
1037 const char *module_str, PyObject *registry)
1038{
Christian Heimes33fe8092008-04-13 13:53:33 +00001039 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001040 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001041 PyObject *module = NULL;
1042 int ret = -1;
1043
1044 if (message == NULL || filename == NULL)
1045 goto exit;
1046 if (module_str != NULL) {
1047 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001048 if (module == NULL)
1049 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001050 }
1051
Victor Stinner14e461d2013-08-26 22:28:21 +02001052 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1053 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001054
1055 exit:
1056 Py_XDECREF(message);
1057 Py_XDECREF(module);
1058 Py_XDECREF(filename);
1059 return ret;
1060}
1061
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001062int
1063PyErr_WarnExplicitFormat(PyObject *category,
1064 const char *filename_str, int lineno,
1065 const char *module_str, PyObject *registry,
1066 const char *format, ...)
1067{
1068 PyObject *message;
1069 PyObject *module = NULL;
1070 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1071 int ret = -1;
1072 va_list vargs;
1073
1074 if (filename == NULL)
1075 goto exit;
1076 if (module_str != NULL) {
1077 module = PyUnicode_FromString(module_str);
1078 if (module == NULL)
1079 goto exit;
1080 }
1081
1082#ifdef HAVE_STDARG_PROTOTYPES
1083 va_start(vargs, format);
1084#else
1085 va_start(vargs);
1086#endif
1087 message = PyUnicode_FromFormatV(format, vargs);
1088 if (message != NULL) {
1089 PyObject *res;
1090 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001091 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001092 Py_DECREF(message);
1093 if (res != NULL) {
1094 Py_DECREF(res);
1095 ret = 0;
1096 }
1097 }
1098 va_end(vargs);
1099exit:
1100 Py_XDECREF(module);
1101 Py_XDECREF(filename);
1102 return ret;
1103}
1104
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001105void
1106_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
1107{
1108 /* First, we attempt to funnel the warning through
1109 warnings._warn_unawaited_coroutine.
1110
1111 This could raise an exception, due to:
1112 - a bug
1113 - some kind of shutdown-related brokenness
1114 - succeeding, but with an "error" warning filter installed, so the
1115 warning is converted into a RuntimeWarning exception
1116
1117 In the first two cases, we want to print the error (so we know what it
1118 is!), and then print a warning directly as a fallback. In the last
1119 case, we want to print the error (since it's the warning!), but *not*
1120 do a fallback. And after we print the error we can't check for what
1121 type of error it was (because PyErr_WriteUnraisable clears it), so we
1122 need a flag to keep track.
1123
1124 Since this is called from __del__ context, it's careful to never raise
1125 an exception.
1126 */
1127 _Py_IDENTIFIER(_warn_unawaited_coroutine);
1128 int warned = 0;
1129 PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1);
1130 if (fn) {
1131 PyObject *res = PyObject_CallFunctionObjArgs(fn, coro, NULL);
1132 Py_DECREF(fn);
1133 if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) {
1134 warned = 1;
1135 }
1136 Py_XDECREF(res);
1137 }
1138
1139 if (PyErr_Occurred()) {
1140 PyErr_WriteUnraisable(coro);
1141 }
1142 if (!warned) {
Yury Selivanov35103342018-01-21 20:47:04 -05001143 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
1144 "coroutine '%.50S' was never awaited",
1145 ((PyCoroObject *)coro)->cr_qualname) < 0)
1146 {
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001147 PyErr_WriteUnraisable(coro);
1148 }
1149 }
1150}
Christian Heimes33fe8092008-04-13 13:53:33 +00001151
Christian Heimes33fe8092008-04-13 13:53:33 +00001152PyDoc_STRVAR(warn_explicit_doc,
1153"Low-level inferface to warnings functionality.");
1154
1155static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001156 WARNINGS_WARN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001157 {"warn_explicit", (PyCFunction)(void(*)(void))warnings_warn_explicit,
Christian Heimes33fe8092008-04-13 13:53:33 +00001158 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001159 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1160 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001161 /* XXX(brett.cannon): add showwarning? */
1162 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001164};
1165
1166
Victor Stinner747f48e2017-12-12 22:59:48 +01001167#ifndef Py_DEBUG
Christian Heimes33fe8092008-04-13 13:53:33 +00001168static PyObject *
Nick Coghlan9b997472018-01-08 12:45:02 +10001169create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
Christian Heimes33fe8092008-04-13 13:53:33 +00001170{
Nick Coghlan9b997472018-01-08 12:45:02 +10001171 PyObject *modname_obj = NULL;
Victor Stinner82656272017-11-22 23:51:42 +01001172 PyObject *action_str = _PyUnicode_FromId(id);
1173 if (action_str == NULL) {
1174 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001175 }
1176
Nick Coghlan9b997472018-01-08 12:45:02 +10001177 /* Default to "no module name" for initial filter set */
1178 if (modname != NULL) {
1179 modname_obj = PyUnicode_InternFromString(modname);
1180 if (modname_obj == NULL) {
1181 return NULL;
1182 }
1183 } else {
1184 modname_obj = Py_None;
1185 }
1186
Christian Heimes33fe8092008-04-13 13:53:33 +00001187 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001188 return PyTuple_Pack(5, action_str, Py_None,
Nick Coghlan9b997472018-01-08 12:45:02 +10001189 category, modname_obj, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001190}
Victor Stinner747f48e2017-12-12 22:59:48 +01001191#endif
1192
Christian Heimes33fe8092008-04-13 13:53:33 +00001193
1194static PyObject *
Victor Stinner5d862462017-12-19 11:35:58 +01001195init_filters(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001196{
Victor Stinner747f48e2017-12-12 22:59:48 +01001197#ifdef Py_DEBUG
1198 /* Py_DEBUG builds show all warnings by default */
1199 return PyList_New(0);
1200#else
1201 /* Other builds ignore a number of warning categories by default */
Nick Coghlan9b997472018-01-08 12:45:02 +10001202 PyObject *filters = PyList_New(5);
Victor Stinner747f48e2017-12-12 22:59:48 +01001203 if (filters == NULL) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001204 return NULL;
Victor Stinner747f48e2017-12-12 22:59:48 +01001205 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001206
Victor Stinnerb98f1712017-11-23 17:13:44 +01001207 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner747f48e2017-12-12 22:59:48 +01001208 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001209 create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__"));
Victor Stinner747f48e2017-12-12 22:59:48 +01001210 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001211 create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001212 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001213 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001214 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001215 create_filter(PyExc_ImportWarning, &PyId_ignore, NULL));
1216 PyList_SET_ITEM(filters, pos++,
1217 create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001218
Victor Stinnerb98f1712017-11-23 17:13:44 +01001219 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001220 if (PyList_GET_ITEM(filters, x) == NULL) {
1221 Py_DECREF(filters);
1222 return NULL;
1223 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001224 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001225 return filters;
Victor Stinner747f48e2017-12-12 22:59:48 +01001226#endif
Christian Heimes33fe8092008-04-13 13:53:33 +00001227}
1228
Martin v. Löwis1a214512008-06-11 05:26:20 +00001229static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 PyModuleDef_HEAD_INIT,
1231 MODULE_NAME,
1232 warnings__doc__,
1233 0,
1234 warnings_functions,
1235 NULL,
1236 NULL,
1237 NULL,
1238 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001239};
1240
Christian Heimes33fe8092008-04-13 13:53:33 +00001241
Victor Stinner5d862462017-12-19 11:35:58 +01001242PyMODINIT_FUNC
1243_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001244{
Brett Cannon0759dd62009-04-01 18:13:07 +00001245 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001246
Martin v. Löwis1a214512008-06-11 05:26:20 +00001247 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001248 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001249 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001250
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001251 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner5d862462017-12-19 11:35:58 +01001252 _PyRuntime.warnings.filters = init_filters();
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001253 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001254 return NULL;
1255 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001256 Py_INCREF(_PyRuntime.warnings.filters);
1257 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001258 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001259
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001260 if (_PyRuntime.warnings.once_registry == NULL) {
1261 _PyRuntime.warnings.once_registry = PyDict_New();
1262 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001263 return NULL;
1264 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001265 Py_INCREF(_PyRuntime.warnings.once_registry);
1266 if (PyModule_AddObject(m, "_onceregistry",
1267 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001268 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001269
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001270 if (_PyRuntime.warnings.default_action == NULL) {
1271 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1272 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001273 return NULL;
1274 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001275 Py_INCREF(_PyRuntime.warnings.default_action);
1276 if (PyModule_AddObject(m, "_defaultaction",
1277 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001278 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001279
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001280 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001281 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001282}