blob: d6614b2cf6d46b84e5acd7aeed7dc6e4255bb22b [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002#include "internal/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 */
81 if (!PyThreadState_GET()->interp->modules) {
82 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)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600258 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200259 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600260 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200261 if (version_obj == NULL)
262 return -1;
263 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
264 Py_DECREF(version_obj);
265 return -1;
266 }
267 Py_DECREF(version_obj);
268 }
269 else {
270 already_warned = PyDict_GetItem(registry, key);
271 if (already_warned != NULL) {
272 int rc = PyObject_IsTrue(already_warned);
273 if (rc != 0)
274 return rc;
275 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000276 }
277
278 /* This warning wasn't found in the registry, set it. */
279 if (should_set)
280 return PyDict_SetItem(registry, key, Py_True);
281 return 0;
282}
283
284/* New reference. */
285static PyObject *
286normalize_module(PyObject *filename)
287{
288 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100289 int kind;
290 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000291 Py_ssize_t len;
292
Victor Stinner9e30aa52011-11-21 02:49:52 +0100293 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000294 if (len < 0)
295 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100296
297 if (len == 0)
298 return PyUnicode_FromString("<unknown>");
299
300 kind = PyUnicode_KIND(filename);
301 data = PyUnicode_DATA(filename);
302
303 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000304 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100305 PyUnicode_READ(kind, data, len-3) == '.' &&
306 PyUnicode_READ(kind, data, len-2) == 'p' &&
307 PyUnicode_READ(kind, data, len-1) == 'y')
308 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100309 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000310 }
311 else {
312 module = filename;
313 Py_INCREF(module);
314 }
315 return module;
316}
317
318static int
319update_registry(PyObject *registry, PyObject *text, PyObject *category,
320 int add_zero)
321{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300322 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000323 int rc;
324
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300325 if (add_zero)
326 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000327 else
328 altkey = PyTuple_Pack(2, text, category);
329
330 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000331 Py_XDECREF(altkey);
332 return rc;
333}
334
335static void
Victor Stinner914cde82016-03-19 01:03:51 +0100336show_warning(PyObject *filename, int lineno, PyObject *text,
337 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 PyObject *f_stderr;
340 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000341 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200342 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000343
344 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
345
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200346 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000347 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100348 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000349
Victor Stinnerbd303c12013-11-07 23:07:29 +0100350 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000351 if (f_stderr == NULL) {
352 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100353 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000354 }
355
356 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100357 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
358 goto error;
359 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
360 goto error;
361 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
362 goto error;
363 if (PyFile_WriteString(": ", f_stderr) < 0)
364 goto error;
365 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
366 goto error;
367 if (PyFile_WriteString("\n", f_stderr) < 0)
368 goto error;
369 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000370
371 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000372 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100373 int kind;
374 void *data;
375 Py_ssize_t i, len;
376 Py_UCS4 ch;
377 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000378
Victor Stinnera4c704b2013-10-29 23:43:41 +0100379 if (PyUnicode_READY(sourceline) < 1)
380 goto error;
381
382 kind = PyUnicode_KIND(sourceline);
383 data = PyUnicode_DATA(sourceline);
384 len = PyUnicode_GET_LENGTH(sourceline);
385 for (i=0; i<len; i++) {
386 ch = PyUnicode_READ(kind, data, i);
387 if (ch != ' ' && ch != '\t' && ch != '\014')
388 break;
389 }
390
391 truncated = PyUnicode_Substring(sourceline, i, len);
392 if (truncated == NULL)
393 goto error;
394
395 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
396 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000397 PyFile_WriteString("\n", f_stderr);
398 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200399 else {
400 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
401 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100402
403error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100404 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000405 PyErr_Clear();
406}
407
Victor Stinner1231a462016-03-19 00:47:17 +0100408static int
409call_show_warning(PyObject *category, PyObject *text, PyObject *message,
410 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100411 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100412{
413 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100414 _Py_IDENTIFIER(_showwarnmsg);
415 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100416
Victor Stinnere98445a2016-03-23 00:54:48 +0100417 /* If the source parameter is set, try to get the Python implementation.
418 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600419 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100420 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100421 if (show_fn == NULL) {
422 if (PyErr_Occurred())
423 return -1;
424 show_warning(filename, lineno, text, category, sourceline);
425 return 0;
426 }
427
428 if (!PyCallable_Check(show_fn)) {
429 PyErr_SetString(PyExc_TypeError,
430 "warnings._showwarnmsg() must be set to a callable");
431 goto error;
432 }
433
Victor Stinner82656272017-11-22 23:51:42 +0100434 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100435 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200436 if (!PyErr_Occurred()) {
437 PyErr_SetString(PyExc_RuntimeError,
438 "unable to get warnings.WarningMessage");
439 }
Victor Stinner1231a462016-03-19 00:47:17 +0100440 goto error;
441 }
442
443 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100444 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100445 NULL);
446 Py_DECREF(warnmsg_cls);
447 if (msg == NULL)
448 goto error;
449
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100450 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100451 Py_DECREF(show_fn);
452 Py_DECREF(msg);
453
454 if (res == NULL)
455 return -1;
456
457 Py_DECREF(res);
458 return 0;
459
460error:
461 Py_XDECREF(show_fn);
462 return -1;
463}
464
Christian Heimes33fe8092008-04-13 13:53:33 +0000465static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000467 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100468 PyObject *module, PyObject *registry, PyObject *sourceline,
469 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000470{
471 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400472 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100473 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000474 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100476 /* module can be None if a warning is emitted late during Python shutdown.
477 In this case, the Python warnings module was probably unloaded, filters
478 are no more available to choose as action. It is safer to ignore the
479 warning and do nothing. */
480 if (module == Py_None)
481 Py_RETURN_NONE;
482
Brett Cannondb734912008-06-27 00:52:15 +0000483 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300484 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000485 return NULL;
486 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000487
488 /* Normalize module. */
489 if (module == NULL) {
490 module = normalize_module(filename);
491 if (module == NULL)
492 return NULL;
493 }
494 else
495 Py_INCREF(module);
496
497 /* Normalize message. */
498 Py_INCREF(message); /* DECREF'ed in cleanup. */
499 rc = PyObject_IsInstance(message, PyExc_Warning);
500 if (rc == -1) {
501 goto cleanup;
502 }
503 if (rc == 1) {
504 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000505 if (text == NULL)
506 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000507 category = (PyObject*)message->ob_type;
508 }
509 else {
510 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100511 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000512 if (message == NULL)
513 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000514 }
515
516 lineno_obj = PyLong_FromLong(lineno);
517 if (lineno_obj == NULL)
518 goto cleanup;
519
Victor Stinner22f18752016-12-09 18:08:18 +0100520 if (source == Py_None) {
521 source = NULL;
522 }
523
Christian Heimes33fe8092008-04-13 13:53:33 +0000524 /* Create key. */
525 key = PyTuple_Pack(3, text, category, lineno_obj);
526 if (key == NULL)
527 goto cleanup;
528
Brett Cannondb734912008-06-27 00:52:15 +0000529 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000530 rc = already_warned(registry, key, 0);
531 if (rc == -1)
532 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000534 goto return_none;
535 /* Else this warning hasn't been generated before. */
536 }
537
538 action = get_filter(category, text, lineno, module, &item);
539 if (action == NULL)
540 goto cleanup;
541
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200542 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000543 PyErr_SetObject(category, message);
544 goto cleanup;
545 }
546
Victor Stinnerc9758782017-11-27 16:57:07 +0100547 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
548 goto return_none;
549 }
550
Christian Heimes33fe8092008-04-13 13:53:33 +0000551 /* Store in the registry that we've been here, *except* when the action
552 is "always". */
553 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200554 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000555 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100556 PyDict_SetItem(registry, key, Py_True) < 0)
557 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000558 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100559 }
560
561 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000562 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000563 registry = get_once_registry();
564 if (registry == NULL)
565 goto cleanup;
566 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600567 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000569 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200570 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000571 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000572 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000574 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200575 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000576 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100577 "Unrecognized action (%R) in warnings.filters:\n %R",
578 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000579 goto cleanup;
580 }
581 }
582
Christian Heimes1a8501c2008-10-02 19:56:01 +0000583 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000584 goto return_none;
585 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100586 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100587 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100588 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000589 }
590 else /* if (rc == -1) */
591 goto cleanup;
592
593 return_none:
594 result = Py_None;
595 Py_INCREF(result);
596
597 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400598 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000599 Py_XDECREF(key);
600 Py_XDECREF(text);
601 Py_XDECREF(lineno_obj);
602 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000603 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000604 return result; /* Py_None or NULL. */
605}
606
Larry Hastings714e4932015-09-06 00:39:37 -0700607static int
608is_internal_frame(PyFrameObject *frame)
609{
610 static PyObject *importlib_string = NULL;
611 static PyObject *bootstrap_string = NULL;
612 PyObject *filename;
613 int contains;
614
615 if (importlib_string == NULL) {
616 importlib_string = PyUnicode_FromString("importlib");
617 if (importlib_string == NULL) {
618 return 0;
619 }
620
621 bootstrap_string = PyUnicode_FromString("_bootstrap");
622 if (bootstrap_string == NULL) {
623 Py_DECREF(importlib_string);
624 return 0;
625 }
626 Py_INCREF(importlib_string);
627 Py_INCREF(bootstrap_string);
628 }
629
630 if (frame == NULL || frame->f_code == NULL ||
631 frame->f_code->co_filename == NULL) {
632 return 0;
633 }
634 filename = frame->f_code->co_filename;
635 if (!PyUnicode_Check(filename)) {
636 return 0;
637 }
638 contains = PyUnicode_Contains(filename, importlib_string);
639 if (contains < 0) {
640 return 0;
641 }
642 else if (contains > 0) {
643 contains = PyUnicode_Contains(filename, bootstrap_string);
644 if (contains < 0) {
645 return 0;
646 }
647 else if (contains > 0) {
648 return 1;
649 }
650 }
651
652 return 0;
653}
654
655static PyFrameObject *
656next_external_frame(PyFrameObject *frame)
657{
658 do {
659 frame = frame->f_back;
660 } while (frame != NULL && is_internal_frame(frame));
661
662 return frame;
663}
664
Christian Heimes33fe8092008-04-13 13:53:33 +0000665/* filename, module, and registry are new refs, globals is borrowed */
666/* Returns 0 on error (no new refs), 1 on success */
667static int
668setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
669 PyObject **module, PyObject **registry)
670{
671 PyObject *globals;
672
Thomas Kluyver11a89662018-06-08 21:28:37 +0200673 /* Setup globals, filename and lineno. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000674 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700675 // Stack level comparisons to Python code is off by one as there is no
676 // warnings-related stack level to avoid.
677 if (stack_level <= 0 || is_internal_frame(f)) {
678 while (--stack_level > 0 && f != NULL) {
679 f = f->f_back;
680 }
681 }
682 else {
683 while (--stack_level > 0 && f != NULL) {
684 f = next_external_frame(f);
685 }
686 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000687
688 if (f == NULL) {
689 globals = PyThreadState_Get()->interp->sysdict;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200690 *filename = PyUnicode_FromString("sys");
Christian Heimes33fe8092008-04-13 13:53:33 +0000691 *lineno = 1;
692 }
693 else {
694 globals = f->f_globals;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200695 *filename = f->f_code->co_filename;
696 Py_INCREF(*filename);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000697 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000698 }
699
700 *module = NULL;
701
702 /* Setup registry. */
703 assert(globals != NULL);
704 assert(PyDict_Check(globals));
705 *registry = PyDict_GetItemString(globals, "__warningregistry__");
706 if (*registry == NULL) {
707 int rc;
708
709 *registry = PyDict_New();
710 if (*registry == NULL)
711 return 0;
712
713 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
714 if (rc < 0)
715 goto handle_error;
716 }
717 else
718 Py_INCREF(*registry);
719
720 /* Setup module. */
721 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300722 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
723 Py_INCREF(*module);
724 }
725 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000726 *module = PyUnicode_FromString("<string>");
727 if (*module == NULL)
728 goto handle_error;
729 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000730
Christian Heimes33fe8092008-04-13 13:53:33 +0000731 return 1;
732
733 handle_error:
734 /* filename not XDECREF'ed here as there is no way to jump here with a
735 dangling reference. */
736 Py_XDECREF(*registry);
737 Py_XDECREF(*module);
738 return 0;
739}
740
741static PyObject *
742get_category(PyObject *message, PyObject *category)
743{
744 int rc;
745
746 /* Get category. */
747 rc = PyObject_IsInstance(message, PyExc_Warning);
748 if (rc == -1)
749 return NULL;
750
751 if (rc == 1)
752 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300753 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000754 category = PyExc_UserWarning;
755
756 /* Validate category. */
757 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300758 /* category is not a subclass of PyExc_Warning or
759 PyObject_IsSubclass raised an error */
760 if (rc == -1 || rc == 0) {
761 PyErr_Format(PyExc_TypeError,
762 "category must be a Warning subclass, not '%s'",
763 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000764 return NULL;
765 }
766
767 return category;
768}
769
770static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100771do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
772 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000773{
774 PyObject *filename, *module, *registry, *res;
775 int lineno;
776
777 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
778 return NULL;
779
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100780 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100781 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000782 Py_DECREF(filename);
783 Py_DECREF(registry);
784 Py_DECREF(module);
785 return res;
786}
787
Victor Stinner22f18752016-12-09 18:08:18 +0100788/*[clinic input]
789warn as warnings_warn
790
791 message: object
792 category: object = None
793 stacklevel: Py_ssize_t = 1
794 source: object = None
795
796Issue a warning, or maybe ignore it or raise an exception.
797[clinic start generated code]*/
798
Christian Heimes33fe8092008-04-13 13:53:33 +0000799static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100800warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
801 Py_ssize_t stacklevel, PyObject *source)
802/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000803{
Christian Heimes33fe8092008-04-13 13:53:33 +0000804 category = get_category(message, category);
805 if (category == NULL)
806 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100807 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000808}
809
810static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200811get_source_line(PyObject *module_globals, int lineno)
812{
813 _Py_IDENTIFIER(get_source);
814 _Py_IDENTIFIER(__loader__);
815 _Py_IDENTIFIER(__name__);
816 PyObject *loader;
817 PyObject *module_name;
818 PyObject *get_source;
819 PyObject *source;
820 PyObject *source_list;
821 PyObject *source_line;
822
823 /* Check/get the requisite pieces needed for the loader. */
824 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
825 if (loader == NULL) {
826 return NULL;
827 }
828 Py_INCREF(loader);
829 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
830 if (!module_name) {
831 Py_DECREF(loader);
832 return NULL;
833 }
834 Py_INCREF(module_name);
835
836 /* Make sure the loader implements the optional get_source() method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200837 (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200838 Py_DECREF(loader);
839 if (!get_source) {
840 Py_DECREF(module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200841 return NULL;
842 }
843 /* Call get_source() to get the source code. */
844 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
845 Py_DECREF(get_source);
846 Py_DECREF(module_name);
847 if (!source) {
848 return NULL;
849 }
850 if (source == Py_None) {
851 Py_DECREF(source);
852 return NULL;
853 }
854
855 /* Split the source into lines. */
856 source_list = PyUnicode_Splitlines(source, 0);
857 Py_DECREF(source);
858 if (!source_list) {
859 return NULL;
860 }
861
862 /* Get the source line. */
863 source_line = PyList_GetItem(source_list, lineno-1);
864 Py_XINCREF(source_line);
865 Py_DECREF(source_list);
866 return source_line;
867}
868
869static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000870warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
871{
872 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100873 "module", "registry", "module_globals",
874 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000875 PyObject *message;
876 PyObject *category;
877 PyObject *filename;
878 int lineno;
879 PyObject *module = NULL;
880 PyObject *registry = NULL;
881 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100882 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200883 PyObject *source_line = NULL;
884 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000885
Victor Stinner914cde82016-03-19 01:03:51 +0100886 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000887 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100888 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000889 return NULL;
890
Victor Stinnerb0565622018-05-15 20:42:12 +0200891 if (module_globals && module_globals != Py_None) {
892 if (!PyDict_Check(module_globals)) {
893 PyErr_Format(PyExc_TypeError,
894 "module_globals must be a dict, not '%.200s'",
895 Py_TYPE(module_globals)->tp_name);
896 return NULL;
897 }
898
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200899 source_line = get_source_line(module_globals, lineno);
900 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000901 return NULL;
902 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000903 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200904 returned = warn_explicit(category, message, filename, lineno, module,
905 registry, source_line, sourceobj);
906 Py_XDECREF(source_line);
907 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000908}
909
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200910static PyObject *
911warnings_filters_mutated(PyObject *self, PyObject *args)
912{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600913 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200914 Py_RETURN_NONE;
915}
916
Christian Heimes33fe8092008-04-13 13:53:33 +0000917
918/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000919
920static int
921warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100922 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000923{
924 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000925
926 if (category == NULL)
927 category = PyExc_RuntimeWarning;
928
Victor Stinner914cde82016-03-19 01:03:51 +0100929 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000930 if (res == NULL)
931 return -1;
932 Py_DECREF(res);
933
934 return 0;
935}
936
Victor Stinner914cde82016-03-19 01:03:51 +0100937static int
938_PyErr_WarnFormatV(PyObject *source,
939 PyObject *category, Py_ssize_t stack_level,
940 const char *format, va_list vargs)
941{
942 PyObject *message;
943 int res;
944
945 message = PyUnicode_FromFormatV(format, vargs);
946 if (message == NULL)
947 return -1;
948
949 res = warn_unicode(category, message, stack_level, source);
950 Py_DECREF(message);
951 return res;
952}
953
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000954int
955PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
956 const char *format, ...)
957{
Victor Stinner914cde82016-03-19 01:03:51 +0100958 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000959 va_list vargs;
960
961#ifdef HAVE_STDARG_PROTOTYPES
962 va_start(vargs, format);
963#else
964 va_start(vargs);
965#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100966 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000967 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100968 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000969}
970
971int
Victor Stinner914cde82016-03-19 01:03:51 +0100972PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
973 const char *format, ...)
974{
975 int res;
976 va_list vargs;
977
978#ifdef HAVE_STDARG_PROTOTYPES
979 va_start(vargs, format);
980#else
981 va_start(vargs);
982#endif
983 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
984 stack_level, format, vargs);
985 va_end(vargs);
986 return res;
987}
988
989
990int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000991PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
992{
993 int ret;
994 PyObject *message = PyUnicode_FromString(text);
995 if (message == NULL)
996 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +0100997 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000998 Py_DECREF(message);
999 return ret;
1000}
1001
Ezio Melotti42da6632011-03-15 05:18:48 +02001002/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001003 Use PyErr_WarnEx instead. */
1004
1005#undef PyErr_Warn
1006
1007PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001008PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001009{
1010 return PyErr_WarnEx(category, text, 1);
1011}
1012
1013/* Warning with explicit origin */
1014int
Victor Stinner14e461d2013-08-26 22:28:21 +02001015PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1016 PyObject *filename, int lineno,
1017 PyObject *module, PyObject *registry)
1018{
1019 PyObject *res;
1020 if (category == NULL)
1021 category = PyExc_RuntimeWarning;
1022 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001023 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001024 if (res == NULL)
1025 return -1;
1026 Py_DECREF(res);
1027 return 0;
1028}
1029
1030int
Christian Heimes33fe8092008-04-13 13:53:33 +00001031PyErr_WarnExplicit(PyObject *category, const char *text,
1032 const char *filename_str, int lineno,
1033 const char *module_str, PyObject *registry)
1034{
Christian Heimes33fe8092008-04-13 13:53:33 +00001035 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001036 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001037 PyObject *module = NULL;
1038 int ret = -1;
1039
1040 if (message == NULL || filename == NULL)
1041 goto exit;
1042 if (module_str != NULL) {
1043 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001044 if (module == NULL)
1045 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001046 }
1047
Victor Stinner14e461d2013-08-26 22:28:21 +02001048 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1049 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001050
1051 exit:
1052 Py_XDECREF(message);
1053 Py_XDECREF(module);
1054 Py_XDECREF(filename);
1055 return ret;
1056}
1057
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001058int
1059PyErr_WarnExplicitFormat(PyObject *category,
1060 const char *filename_str, int lineno,
1061 const char *module_str, PyObject *registry,
1062 const char *format, ...)
1063{
1064 PyObject *message;
1065 PyObject *module = NULL;
1066 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1067 int ret = -1;
1068 va_list vargs;
1069
1070 if (filename == NULL)
1071 goto exit;
1072 if (module_str != NULL) {
1073 module = PyUnicode_FromString(module_str);
1074 if (module == NULL)
1075 goto exit;
1076 }
1077
1078#ifdef HAVE_STDARG_PROTOTYPES
1079 va_start(vargs, format);
1080#else
1081 va_start(vargs);
1082#endif
1083 message = PyUnicode_FromFormatV(format, vargs);
1084 if (message != NULL) {
1085 PyObject *res;
1086 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001087 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001088 Py_DECREF(message);
1089 if (res != NULL) {
1090 Py_DECREF(res);
1091 ret = 0;
1092 }
1093 }
1094 va_end(vargs);
1095exit:
1096 Py_XDECREF(module);
1097 Py_XDECREF(filename);
1098 return ret;
1099}
1100
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001101void
1102_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
1103{
1104 /* First, we attempt to funnel the warning through
1105 warnings._warn_unawaited_coroutine.
1106
1107 This could raise an exception, due to:
1108 - a bug
1109 - some kind of shutdown-related brokenness
1110 - succeeding, but with an "error" warning filter installed, so the
1111 warning is converted into a RuntimeWarning exception
1112
1113 In the first two cases, we want to print the error (so we know what it
1114 is!), and then print a warning directly as a fallback. In the last
1115 case, we want to print the error (since it's the warning!), but *not*
1116 do a fallback. And after we print the error we can't check for what
1117 type of error it was (because PyErr_WriteUnraisable clears it), so we
1118 need a flag to keep track.
1119
1120 Since this is called from __del__ context, it's careful to never raise
1121 an exception.
1122 */
1123 _Py_IDENTIFIER(_warn_unawaited_coroutine);
1124 int warned = 0;
1125 PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1);
1126 if (fn) {
1127 PyObject *res = PyObject_CallFunctionObjArgs(fn, coro, NULL);
1128 Py_DECREF(fn);
1129 if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) {
1130 warned = 1;
1131 }
1132 Py_XDECREF(res);
1133 }
1134
1135 if (PyErr_Occurred()) {
1136 PyErr_WriteUnraisable(coro);
1137 }
1138 if (!warned) {
Yury Selivanov35103342018-01-21 20:47:04 -05001139 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
1140 "coroutine '%.50S' was never awaited",
1141 ((PyCoroObject *)coro)->cr_qualname) < 0)
1142 {
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001143 PyErr_WriteUnraisable(coro);
1144 }
1145 }
1146}
Christian Heimes33fe8092008-04-13 13:53:33 +00001147
Christian Heimes33fe8092008-04-13 13:53:33 +00001148PyDoc_STRVAR(warn_explicit_doc,
1149"Low-level inferface to warnings functionality.");
1150
1151static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001152 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001153 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1154 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001155 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1156 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001157 /* XXX(brett.cannon): add showwarning? */
1158 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001160};
1161
1162
Victor Stinner747f48e2017-12-12 22:59:48 +01001163#ifndef Py_DEBUG
Christian Heimes33fe8092008-04-13 13:53:33 +00001164static PyObject *
Nick Coghlan9b997472018-01-08 12:45:02 +10001165create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
Christian Heimes33fe8092008-04-13 13:53:33 +00001166{
Nick Coghlan9b997472018-01-08 12:45:02 +10001167 PyObject *modname_obj = NULL;
Victor Stinner82656272017-11-22 23:51:42 +01001168 PyObject *action_str = _PyUnicode_FromId(id);
1169 if (action_str == NULL) {
1170 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001171 }
1172
Nick Coghlan9b997472018-01-08 12:45:02 +10001173 /* Default to "no module name" for initial filter set */
1174 if (modname != NULL) {
1175 modname_obj = PyUnicode_InternFromString(modname);
1176 if (modname_obj == NULL) {
1177 return NULL;
1178 }
1179 } else {
1180 modname_obj = Py_None;
1181 }
1182
Christian Heimes33fe8092008-04-13 13:53:33 +00001183 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001184 return PyTuple_Pack(5, action_str, Py_None,
Nick Coghlan9b997472018-01-08 12:45:02 +10001185 category, modname_obj, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001186}
Victor Stinner747f48e2017-12-12 22:59:48 +01001187#endif
1188
Christian Heimes33fe8092008-04-13 13:53:33 +00001189
1190static PyObject *
Victor Stinner5d862462017-12-19 11:35:58 +01001191init_filters(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001192{
Victor Stinner747f48e2017-12-12 22:59:48 +01001193#ifdef Py_DEBUG
1194 /* Py_DEBUG builds show all warnings by default */
1195 return PyList_New(0);
1196#else
1197 /* Other builds ignore a number of warning categories by default */
Nick Coghlan9b997472018-01-08 12:45:02 +10001198 PyObject *filters = PyList_New(5);
Victor Stinner747f48e2017-12-12 22:59:48 +01001199 if (filters == NULL) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001200 return NULL;
Victor Stinner747f48e2017-12-12 22:59:48 +01001201 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001202
Victor Stinnerb98f1712017-11-23 17:13:44 +01001203 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner747f48e2017-12-12 22:59:48 +01001204 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001205 create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__"));
Victor Stinner747f48e2017-12-12 22:59:48 +01001206 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001207 create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001208 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001209 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001210 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001211 create_filter(PyExc_ImportWarning, &PyId_ignore, NULL));
1212 PyList_SET_ITEM(filters, pos++,
1213 create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001214
Victor Stinnerb98f1712017-11-23 17:13:44 +01001215 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001216 if (PyList_GET_ITEM(filters, x) == NULL) {
1217 Py_DECREF(filters);
1218 return NULL;
1219 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001220 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001221 return filters;
Victor Stinner747f48e2017-12-12 22:59:48 +01001222#endif
Christian Heimes33fe8092008-04-13 13:53:33 +00001223}
1224
Martin v. Löwis1a214512008-06-11 05:26:20 +00001225static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 PyModuleDef_HEAD_INIT,
1227 MODULE_NAME,
1228 warnings__doc__,
1229 0,
1230 warnings_functions,
1231 NULL,
1232 NULL,
1233 NULL,
1234 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001235};
1236
Christian Heimes33fe8092008-04-13 13:53:33 +00001237
Victor Stinner5d862462017-12-19 11:35:58 +01001238PyMODINIT_FUNC
1239_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001240{
Brett Cannon0759dd62009-04-01 18:13:07 +00001241 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001242
Martin v. Löwis1a214512008-06-11 05:26:20 +00001243 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001244 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001245 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001246
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001247 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner5d862462017-12-19 11:35:58 +01001248 _PyRuntime.warnings.filters = init_filters();
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001249 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001250 return NULL;
1251 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001252 Py_INCREF(_PyRuntime.warnings.filters);
1253 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001254 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001255
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001256 if (_PyRuntime.warnings.once_registry == NULL) {
1257 _PyRuntime.warnings.once_registry = PyDict_New();
1258 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001259 return NULL;
1260 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001261 Py_INCREF(_PyRuntime.warnings.once_registry);
1262 if (PyModule_AddObject(m, "_onceregistry",
1263 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001264 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001265
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001266 if (_PyRuntime.warnings.default_action == NULL) {
1267 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1268 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001269 return NULL;
1270 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001271 Py_INCREF(_PyRuntime.warnings.default_action);
1272 if (PyModule_AddObject(m, "_defaultaction",
1273 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001274 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001275
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001276 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001277 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001278}