blob: 33b461511585e577c5c62a14b56fb60d8d2e4461 [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
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200255 version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200256 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 {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200274 already_warned = PyDict_GetItemWithError(registry, key);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200275 if (already_warned != NULL) {
276 int rc = PyObject_IsTrue(already_warned);
277 if (rc != 0)
278 return rc;
279 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200280 else if (PyErr_Occurred()) {
281 return -1;
282 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000283 }
284
285 /* This warning wasn't found in the registry, set it. */
286 if (should_set)
287 return PyDict_SetItem(registry, key, Py_True);
288 return 0;
289}
290
291/* New reference. */
292static PyObject *
293normalize_module(PyObject *filename)
294{
295 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100296 int kind;
297 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000298 Py_ssize_t len;
299
Victor Stinner9e30aa52011-11-21 02:49:52 +0100300 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000301 if (len < 0)
302 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100303
304 if (len == 0)
305 return PyUnicode_FromString("<unknown>");
306
307 kind = PyUnicode_KIND(filename);
308 data = PyUnicode_DATA(filename);
309
310 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000311 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100312 PyUnicode_READ(kind, data, len-3) == '.' &&
313 PyUnicode_READ(kind, data, len-2) == 'p' &&
314 PyUnicode_READ(kind, data, len-1) == 'y')
315 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100316 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000317 }
318 else {
319 module = filename;
320 Py_INCREF(module);
321 }
322 return module;
323}
324
325static int
326update_registry(PyObject *registry, PyObject *text, PyObject *category,
327 int add_zero)
328{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300329 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000330 int rc;
331
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300332 if (add_zero)
333 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000334 else
335 altkey = PyTuple_Pack(2, text, category);
336
337 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000338 Py_XDECREF(altkey);
339 return rc;
340}
341
342static void
Victor Stinner914cde82016-03-19 01:03:51 +0100343show_warning(PyObject *filename, int lineno, PyObject *text,
344 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyObject *f_stderr;
347 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000348 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200349 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000350
351 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
352
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200353 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000354 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100355 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000356
Victor Stinnerbd303c12013-11-07 23:07:29 +0100357 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000358 if (f_stderr == NULL) {
359 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100360 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000361 }
362
363 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100364 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
365 goto error;
366 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
367 goto error;
368 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
369 goto error;
370 if (PyFile_WriteString(": ", f_stderr) < 0)
371 goto error;
372 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
373 goto error;
374 if (PyFile_WriteString("\n", f_stderr) < 0)
375 goto error;
376 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000377
378 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000379 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100380 int kind;
381 void *data;
382 Py_ssize_t i, len;
383 Py_UCS4 ch;
384 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000385
Victor Stinnera4c704b2013-10-29 23:43:41 +0100386 if (PyUnicode_READY(sourceline) < 1)
387 goto error;
388
389 kind = PyUnicode_KIND(sourceline);
390 data = PyUnicode_DATA(sourceline);
391 len = PyUnicode_GET_LENGTH(sourceline);
392 for (i=0; i<len; i++) {
393 ch = PyUnicode_READ(kind, data, i);
394 if (ch != ' ' && ch != '\t' && ch != '\014')
395 break;
396 }
397
398 truncated = PyUnicode_Substring(sourceline, i, len);
399 if (truncated == NULL)
400 goto error;
401
402 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
403 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000404 PyFile_WriteString("\n", f_stderr);
405 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200406 else {
407 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
408 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100409
410error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100411 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000412 PyErr_Clear();
413}
414
Victor Stinner1231a462016-03-19 00:47:17 +0100415static int
416call_show_warning(PyObject *category, PyObject *text, PyObject *message,
417 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100418 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100419{
420 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100421 _Py_IDENTIFIER(_showwarnmsg);
422 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100423
Victor Stinnere98445a2016-03-23 00:54:48 +0100424 /* If the source parameter is set, try to get the Python implementation.
425 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600426 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100427 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100428 if (show_fn == NULL) {
429 if (PyErr_Occurred())
430 return -1;
431 show_warning(filename, lineno, text, category, sourceline);
432 return 0;
433 }
434
435 if (!PyCallable_Check(show_fn)) {
436 PyErr_SetString(PyExc_TypeError,
437 "warnings._showwarnmsg() must be set to a callable");
438 goto error;
439 }
440
Victor Stinner82656272017-11-22 23:51:42 +0100441 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100442 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200443 if (!PyErr_Occurred()) {
444 PyErr_SetString(PyExc_RuntimeError,
445 "unable to get warnings.WarningMessage");
446 }
Victor Stinner1231a462016-03-19 00:47:17 +0100447 goto error;
448 }
449
450 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100451 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100452 NULL);
453 Py_DECREF(warnmsg_cls);
454 if (msg == NULL)
455 goto error;
456
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100457 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100458 Py_DECREF(show_fn);
459 Py_DECREF(msg);
460
461 if (res == NULL)
462 return -1;
463
464 Py_DECREF(res);
465 return 0;
466
467error:
468 Py_XDECREF(show_fn);
469 return -1;
470}
471
Christian Heimes33fe8092008-04-13 13:53:33 +0000472static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000474 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100475 PyObject *module, PyObject *registry, PyObject *sourceline,
476 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000477{
478 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400479 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100480 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000481 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100483 /* module can be None if a warning is emitted late during Python shutdown.
484 In this case, the Python warnings module was probably unloaded, filters
485 are no more available to choose as action. It is safer to ignore the
486 warning and do nothing. */
487 if (module == Py_None)
488 Py_RETURN_NONE;
489
Brett Cannondb734912008-06-27 00:52:15 +0000490 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300491 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000492 return NULL;
493 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000494
495 /* Normalize module. */
496 if (module == NULL) {
497 module = normalize_module(filename);
498 if (module == NULL)
499 return NULL;
500 }
501 else
502 Py_INCREF(module);
503
504 /* Normalize message. */
505 Py_INCREF(message); /* DECREF'ed in cleanup. */
506 rc = PyObject_IsInstance(message, PyExc_Warning);
507 if (rc == -1) {
508 goto cleanup;
509 }
510 if (rc == 1) {
511 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000512 if (text == NULL)
513 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000514 category = (PyObject*)message->ob_type;
515 }
516 else {
517 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100518 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000519 if (message == NULL)
520 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000521 }
522
523 lineno_obj = PyLong_FromLong(lineno);
524 if (lineno_obj == NULL)
525 goto cleanup;
526
Victor Stinner22f18752016-12-09 18:08:18 +0100527 if (source == Py_None) {
528 source = NULL;
529 }
530
Christian Heimes33fe8092008-04-13 13:53:33 +0000531 /* Create key. */
532 key = PyTuple_Pack(3, text, category, lineno_obj);
533 if (key == NULL)
534 goto cleanup;
535
Brett Cannondb734912008-06-27 00:52:15 +0000536 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000537 rc = already_warned(registry, key, 0);
538 if (rc == -1)
539 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000541 goto return_none;
542 /* Else this warning hasn't been generated before. */
543 }
544
545 action = get_filter(category, text, lineno, module, &item);
546 if (action == NULL)
547 goto cleanup;
548
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200549 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000550 PyErr_SetObject(category, message);
551 goto cleanup;
552 }
553
Victor Stinnerc9758782017-11-27 16:57:07 +0100554 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
555 goto return_none;
556 }
557
Christian Heimes33fe8092008-04-13 13:53:33 +0000558 /* Store in the registry that we've been here, *except* when the action
559 is "always". */
560 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200561 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000562 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100563 PyDict_SetItem(registry, key, Py_True) < 0)
564 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000565 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100566 }
567
568 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000569 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000570 registry = get_once_registry();
571 if (registry == NULL)
572 goto cleanup;
573 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600574 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000576 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200577 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000578 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000579 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000581 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200582 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000583 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100584 "Unrecognized action (%R) in warnings.filters:\n %R",
585 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000586 goto cleanup;
587 }
588 }
589
Christian Heimes1a8501c2008-10-02 19:56:01 +0000590 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000591 goto return_none;
592 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100593 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100594 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100595 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000596 }
597 else /* if (rc == -1) */
598 goto cleanup;
599
600 return_none:
601 result = Py_None;
602 Py_INCREF(result);
603
604 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400605 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000606 Py_XDECREF(key);
607 Py_XDECREF(text);
608 Py_XDECREF(lineno_obj);
609 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000610 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000611 return result; /* Py_None or NULL. */
612}
613
Larry Hastings714e4932015-09-06 00:39:37 -0700614static int
615is_internal_frame(PyFrameObject *frame)
616{
617 static PyObject *importlib_string = NULL;
618 static PyObject *bootstrap_string = NULL;
619 PyObject *filename;
620 int contains;
621
622 if (importlib_string == NULL) {
623 importlib_string = PyUnicode_FromString("importlib");
624 if (importlib_string == NULL) {
625 return 0;
626 }
627
628 bootstrap_string = PyUnicode_FromString("_bootstrap");
629 if (bootstrap_string == NULL) {
630 Py_DECREF(importlib_string);
631 return 0;
632 }
633 Py_INCREF(importlib_string);
634 Py_INCREF(bootstrap_string);
635 }
636
637 if (frame == NULL || frame->f_code == NULL ||
638 frame->f_code->co_filename == NULL) {
639 return 0;
640 }
641 filename = frame->f_code->co_filename;
642 if (!PyUnicode_Check(filename)) {
643 return 0;
644 }
645 contains = PyUnicode_Contains(filename, importlib_string);
646 if (contains < 0) {
647 return 0;
648 }
649 else if (contains > 0) {
650 contains = PyUnicode_Contains(filename, bootstrap_string);
651 if (contains < 0) {
652 return 0;
653 }
654 else if (contains > 0) {
655 return 1;
656 }
657 }
658
659 return 0;
660}
661
662static PyFrameObject *
663next_external_frame(PyFrameObject *frame)
664{
665 do {
666 frame = frame->f_back;
667 } while (frame != NULL && is_internal_frame(frame));
668
669 return frame;
670}
671
Christian Heimes33fe8092008-04-13 13:53:33 +0000672/* filename, module, and registry are new refs, globals is borrowed */
673/* Returns 0 on error (no new refs), 1 on success */
674static int
675setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
676 PyObject **module, PyObject **registry)
677{
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200678 _Py_IDENTIFIER(__warningregistry__);
679 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000680 PyObject *globals;
681
Thomas Kluyver11a89662018-06-08 21:28:37 +0200682 /* Setup globals, filename and lineno. */
Victor Stinner50b48572018-11-01 01:51:40 +0100683 PyFrameObject *f = _PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700684 // Stack level comparisons to Python code is off by one as there is no
685 // warnings-related stack level to avoid.
686 if (stack_level <= 0 || is_internal_frame(f)) {
687 while (--stack_level > 0 && f != NULL) {
688 f = f->f_back;
689 }
690 }
691 else {
692 while (--stack_level > 0 && f != NULL) {
693 f = next_external_frame(f);
694 }
695 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000696
697 if (f == NULL) {
Victor Stinnercaba55b2018-08-03 15:33:52 +0200698 globals = _PyInterpreterState_GET_UNSAFE()->sysdict;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200699 *filename = PyUnicode_FromString("sys");
Christian Heimes33fe8092008-04-13 13:53:33 +0000700 *lineno = 1;
701 }
702 else {
703 globals = f->f_globals;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200704 *filename = f->f_code->co_filename;
705 Py_INCREF(*filename);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000706 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000707 }
708
709 *module = NULL;
710
711 /* Setup registry. */
712 assert(globals != NULL);
713 assert(PyDict_Check(globals));
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200714 *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000715 if (*registry == NULL) {
716 int rc;
717
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200718 if (PyErr_Occurred()) {
719 return 0;
720 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000721 *registry = PyDict_New();
722 if (*registry == NULL)
723 return 0;
724
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200725 rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000726 if (rc < 0)
727 goto handle_error;
728 }
729 else
730 Py_INCREF(*registry);
731
732 /* Setup module. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200733 *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
Oren Milman5d3e8002017-09-24 21:28:42 +0300734 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
735 Py_INCREF(*module);
736 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200737 else if (PyErr_Occurred()) {
738 goto handle_error;
739 }
Oren Milman5d3e8002017-09-24 21:28:42 +0300740 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000741 *module = PyUnicode_FromString("<string>");
742 if (*module == NULL)
743 goto handle_error;
744 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000745
Christian Heimes33fe8092008-04-13 13:53:33 +0000746 return 1;
747
748 handle_error:
749 /* filename not XDECREF'ed here as there is no way to jump here with a
750 dangling reference. */
751 Py_XDECREF(*registry);
752 Py_XDECREF(*module);
753 return 0;
754}
755
756static PyObject *
757get_category(PyObject *message, PyObject *category)
758{
759 int rc;
760
761 /* Get category. */
762 rc = PyObject_IsInstance(message, PyExc_Warning);
763 if (rc == -1)
764 return NULL;
765
766 if (rc == 1)
767 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300768 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000769 category = PyExc_UserWarning;
770
771 /* Validate category. */
772 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300773 /* category is not a subclass of PyExc_Warning or
774 PyObject_IsSubclass raised an error */
775 if (rc == -1 || rc == 0) {
776 PyErr_Format(PyExc_TypeError,
777 "category must be a Warning subclass, not '%s'",
778 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000779 return NULL;
780 }
781
782 return category;
783}
784
785static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100786do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
787 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000788{
789 PyObject *filename, *module, *registry, *res;
790 int lineno;
791
792 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
793 return NULL;
794
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100795 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100796 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000797 Py_DECREF(filename);
798 Py_DECREF(registry);
799 Py_DECREF(module);
800 return res;
801}
802
Victor Stinner22f18752016-12-09 18:08:18 +0100803/*[clinic input]
804warn as warnings_warn
805
806 message: object
807 category: object = None
808 stacklevel: Py_ssize_t = 1
809 source: object = None
810
811Issue a warning, or maybe ignore it or raise an exception.
812[clinic start generated code]*/
813
Christian Heimes33fe8092008-04-13 13:53:33 +0000814static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100815warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
816 Py_ssize_t stacklevel, PyObject *source)
817/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000818{
Christian Heimes33fe8092008-04-13 13:53:33 +0000819 category = get_category(message, category);
820 if (category == NULL)
821 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100822 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000823}
824
825static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200826get_source_line(PyObject *module_globals, int lineno)
827{
828 _Py_IDENTIFIER(get_source);
829 _Py_IDENTIFIER(__loader__);
830 _Py_IDENTIFIER(__name__);
831 PyObject *loader;
832 PyObject *module_name;
833 PyObject *get_source;
834 PyObject *source;
835 PyObject *source_list;
836 PyObject *source_line;
837
838 /* Check/get the requisite pieces needed for the loader. */
839 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
840 if (loader == NULL) {
841 return NULL;
842 }
843 Py_INCREF(loader);
844 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
845 if (!module_name) {
846 Py_DECREF(loader);
847 return NULL;
848 }
849 Py_INCREF(module_name);
850
851 /* Make sure the loader implements the optional get_source() method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200852 (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200853 Py_DECREF(loader);
854 if (!get_source) {
855 Py_DECREF(module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200856 return NULL;
857 }
858 /* Call get_source() to get the source code. */
859 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
860 Py_DECREF(get_source);
861 Py_DECREF(module_name);
862 if (!source) {
863 return NULL;
864 }
865 if (source == Py_None) {
866 Py_DECREF(source);
867 return NULL;
868 }
869
870 /* Split the source into lines. */
871 source_list = PyUnicode_Splitlines(source, 0);
872 Py_DECREF(source);
873 if (!source_list) {
874 return NULL;
875 }
876
877 /* Get the source line. */
878 source_line = PyList_GetItem(source_list, lineno-1);
879 Py_XINCREF(source_line);
880 Py_DECREF(source_list);
881 return source_line;
882}
883
884static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000885warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
886{
887 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100888 "module", "registry", "module_globals",
889 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000890 PyObject *message;
891 PyObject *category;
892 PyObject *filename;
893 int lineno;
894 PyObject *module = NULL;
895 PyObject *registry = NULL;
896 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100897 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200898 PyObject *source_line = NULL;
899 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000900
Victor Stinner914cde82016-03-19 01:03:51 +0100901 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000902 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100903 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000904 return NULL;
905
Victor Stinnerb0565622018-05-15 20:42:12 +0200906 if (module_globals && module_globals != Py_None) {
907 if (!PyDict_Check(module_globals)) {
908 PyErr_Format(PyExc_TypeError,
909 "module_globals must be a dict, not '%.200s'",
910 Py_TYPE(module_globals)->tp_name);
911 return NULL;
912 }
913
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200914 source_line = get_source_line(module_globals, lineno);
915 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000916 return NULL;
917 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000918 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200919 returned = warn_explicit(category, message, filename, lineno, module,
920 registry, source_line, sourceobj);
921 Py_XDECREF(source_line);
922 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000923}
924
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200925static PyObject *
926warnings_filters_mutated(PyObject *self, PyObject *args)
927{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600928 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200929 Py_RETURN_NONE;
930}
931
Christian Heimes33fe8092008-04-13 13:53:33 +0000932
933/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000934
935static int
936warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100937 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000938{
939 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000940
941 if (category == NULL)
942 category = PyExc_RuntimeWarning;
943
Victor Stinner914cde82016-03-19 01:03:51 +0100944 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000945 if (res == NULL)
946 return -1;
947 Py_DECREF(res);
948
949 return 0;
950}
951
Victor Stinner914cde82016-03-19 01:03:51 +0100952static int
953_PyErr_WarnFormatV(PyObject *source,
954 PyObject *category, Py_ssize_t stack_level,
955 const char *format, va_list vargs)
956{
957 PyObject *message;
958 int res;
959
960 message = PyUnicode_FromFormatV(format, vargs);
961 if (message == NULL)
962 return -1;
963
964 res = warn_unicode(category, message, stack_level, source);
965 Py_DECREF(message);
966 return res;
967}
968
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000969int
970PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
971 const char *format, ...)
972{
Victor Stinner914cde82016-03-19 01:03:51 +0100973 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000974 va_list vargs;
975
976#ifdef HAVE_STDARG_PROTOTYPES
977 va_start(vargs, format);
978#else
979 va_start(vargs);
980#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100981 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000982 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100983 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000984}
985
986int
Victor Stinner914cde82016-03-19 01:03:51 +0100987PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
988 const char *format, ...)
989{
990 int res;
991 va_list vargs;
992
993#ifdef HAVE_STDARG_PROTOTYPES
994 va_start(vargs, format);
995#else
996 va_start(vargs);
997#endif
998 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
999 stack_level, format, vargs);
1000 va_end(vargs);
1001 return res;
1002}
1003
1004
1005int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001006PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1007{
1008 int ret;
1009 PyObject *message = PyUnicode_FromString(text);
1010 if (message == NULL)
1011 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001012 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001013 Py_DECREF(message);
1014 return ret;
1015}
1016
Ezio Melotti42da6632011-03-15 05:18:48 +02001017/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001018 Use PyErr_WarnEx instead. */
1019
1020#undef PyErr_Warn
1021
Benjamin Petersone5024512018-09-12 12:06:42 -07001022int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001023PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001024{
1025 return PyErr_WarnEx(category, text, 1);
1026}
1027
1028/* Warning with explicit origin */
1029int
Victor Stinner14e461d2013-08-26 22:28:21 +02001030PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1031 PyObject *filename, int lineno,
1032 PyObject *module, PyObject *registry)
1033{
1034 PyObject *res;
1035 if (category == NULL)
1036 category = PyExc_RuntimeWarning;
1037 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001038 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001039 if (res == NULL)
1040 return -1;
1041 Py_DECREF(res);
1042 return 0;
1043}
1044
1045int
Christian Heimes33fe8092008-04-13 13:53:33 +00001046PyErr_WarnExplicit(PyObject *category, const char *text,
1047 const char *filename_str, int lineno,
1048 const char *module_str, PyObject *registry)
1049{
Christian Heimes33fe8092008-04-13 13:53:33 +00001050 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001051 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001052 PyObject *module = NULL;
1053 int ret = -1;
1054
1055 if (message == NULL || filename == NULL)
1056 goto exit;
1057 if (module_str != NULL) {
1058 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001059 if (module == NULL)
1060 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001061 }
1062
Victor Stinner14e461d2013-08-26 22:28:21 +02001063 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1064 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001065
1066 exit:
1067 Py_XDECREF(message);
1068 Py_XDECREF(module);
1069 Py_XDECREF(filename);
1070 return ret;
1071}
1072
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001073int
1074PyErr_WarnExplicitFormat(PyObject *category,
1075 const char *filename_str, int lineno,
1076 const char *module_str, PyObject *registry,
1077 const char *format, ...)
1078{
1079 PyObject *message;
1080 PyObject *module = NULL;
1081 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1082 int ret = -1;
1083 va_list vargs;
1084
1085 if (filename == NULL)
1086 goto exit;
1087 if (module_str != NULL) {
1088 module = PyUnicode_FromString(module_str);
1089 if (module == NULL)
1090 goto exit;
1091 }
1092
1093#ifdef HAVE_STDARG_PROTOTYPES
1094 va_start(vargs, format);
1095#else
1096 va_start(vargs);
1097#endif
1098 message = PyUnicode_FromFormatV(format, vargs);
1099 if (message != NULL) {
1100 PyObject *res;
1101 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001102 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001103 Py_DECREF(message);
1104 if (res != NULL) {
1105 Py_DECREF(res);
1106 ret = 0;
1107 }
1108 }
1109 va_end(vargs);
1110exit:
1111 Py_XDECREF(module);
1112 Py_XDECREF(filename);
1113 return ret;
1114}
1115
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001116void
1117_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
1118{
1119 /* First, we attempt to funnel the warning through
1120 warnings._warn_unawaited_coroutine.
1121
1122 This could raise an exception, due to:
1123 - a bug
1124 - some kind of shutdown-related brokenness
1125 - succeeding, but with an "error" warning filter installed, so the
1126 warning is converted into a RuntimeWarning exception
1127
1128 In the first two cases, we want to print the error (so we know what it
1129 is!), and then print a warning directly as a fallback. In the last
1130 case, we want to print the error (since it's the warning!), but *not*
1131 do a fallback. And after we print the error we can't check for what
1132 type of error it was (because PyErr_WriteUnraisable clears it), so we
1133 need a flag to keep track.
1134
1135 Since this is called from __del__ context, it's careful to never raise
1136 an exception.
1137 */
1138 _Py_IDENTIFIER(_warn_unawaited_coroutine);
1139 int warned = 0;
1140 PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1);
1141 if (fn) {
1142 PyObject *res = PyObject_CallFunctionObjArgs(fn, coro, NULL);
1143 Py_DECREF(fn);
1144 if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) {
1145 warned = 1;
1146 }
1147 Py_XDECREF(res);
1148 }
1149
1150 if (PyErr_Occurred()) {
1151 PyErr_WriteUnraisable(coro);
1152 }
1153 if (!warned) {
Yury Selivanov35103342018-01-21 20:47:04 -05001154 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
1155 "coroutine '%.50S' was never awaited",
1156 ((PyCoroObject *)coro)->cr_qualname) < 0)
1157 {
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001158 PyErr_WriteUnraisable(coro);
1159 }
1160 }
1161}
Christian Heimes33fe8092008-04-13 13:53:33 +00001162
Christian Heimes33fe8092008-04-13 13:53:33 +00001163PyDoc_STRVAR(warn_explicit_doc,
1164"Low-level inferface to warnings functionality.");
1165
1166static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001167 WARNINGS_WARN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001168 {"warn_explicit", (PyCFunction)(void(*)(void))warnings_warn_explicit,
Christian Heimes33fe8092008-04-13 13:53:33 +00001169 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001170 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1171 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001172 /* XXX(brett.cannon): add showwarning? */
1173 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001175};
1176
1177
Victor Stinner747f48e2017-12-12 22:59:48 +01001178#ifndef Py_DEBUG
Christian Heimes33fe8092008-04-13 13:53:33 +00001179static PyObject *
Nick Coghlan9b997472018-01-08 12:45:02 +10001180create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
Christian Heimes33fe8092008-04-13 13:53:33 +00001181{
Nick Coghlan9b997472018-01-08 12:45:02 +10001182 PyObject *modname_obj = NULL;
Victor Stinner82656272017-11-22 23:51:42 +01001183 PyObject *action_str = _PyUnicode_FromId(id);
1184 if (action_str == NULL) {
1185 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001186 }
1187
Nick Coghlan9b997472018-01-08 12:45:02 +10001188 /* Default to "no module name" for initial filter set */
1189 if (modname != NULL) {
1190 modname_obj = PyUnicode_InternFromString(modname);
1191 if (modname_obj == NULL) {
1192 return NULL;
1193 }
1194 } else {
1195 modname_obj = Py_None;
1196 }
1197
Christian Heimes33fe8092008-04-13 13:53:33 +00001198 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001199 return PyTuple_Pack(5, action_str, Py_None,
Nick Coghlan9b997472018-01-08 12:45:02 +10001200 category, modname_obj, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001201}
Victor Stinner747f48e2017-12-12 22:59:48 +01001202#endif
1203
Christian Heimes33fe8092008-04-13 13:53:33 +00001204
1205static PyObject *
Victor Stinner5d862462017-12-19 11:35:58 +01001206init_filters(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001207{
Victor Stinner747f48e2017-12-12 22:59:48 +01001208#ifdef Py_DEBUG
1209 /* Py_DEBUG builds show all warnings by default */
1210 return PyList_New(0);
1211#else
1212 /* Other builds ignore a number of warning categories by default */
Nick Coghlan9b997472018-01-08 12:45:02 +10001213 PyObject *filters = PyList_New(5);
Victor Stinner747f48e2017-12-12 22:59:48 +01001214 if (filters == NULL) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001215 return NULL;
Victor Stinner747f48e2017-12-12 22:59:48 +01001216 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001217
Victor Stinnerb98f1712017-11-23 17:13:44 +01001218 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner747f48e2017-12-12 22:59:48 +01001219 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001220 create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__"));
Victor Stinner747f48e2017-12-12 22:59:48 +01001221 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001222 create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001223 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001224 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001225 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001226 create_filter(PyExc_ImportWarning, &PyId_ignore, NULL));
1227 PyList_SET_ITEM(filters, pos++,
1228 create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001229
Victor Stinnerb98f1712017-11-23 17:13:44 +01001230 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001231 if (PyList_GET_ITEM(filters, x) == NULL) {
1232 Py_DECREF(filters);
1233 return NULL;
1234 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001235 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001236 return filters;
Victor Stinner747f48e2017-12-12 22:59:48 +01001237#endif
Christian Heimes33fe8092008-04-13 13:53:33 +00001238}
1239
Martin v. Löwis1a214512008-06-11 05:26:20 +00001240static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 PyModuleDef_HEAD_INIT,
1242 MODULE_NAME,
1243 warnings__doc__,
1244 0,
1245 warnings_functions,
1246 NULL,
1247 NULL,
1248 NULL,
1249 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001250};
1251
Christian Heimes33fe8092008-04-13 13:53:33 +00001252
Victor Stinner5d862462017-12-19 11:35:58 +01001253PyMODINIT_FUNC
1254_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001255{
Brett Cannon0759dd62009-04-01 18:13:07 +00001256 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001257
Martin v. Löwis1a214512008-06-11 05:26:20 +00001258 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001259 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001260 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001261
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001262 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner5d862462017-12-19 11:35:58 +01001263 _PyRuntime.warnings.filters = init_filters();
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001264 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001265 return NULL;
1266 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001267 Py_INCREF(_PyRuntime.warnings.filters);
1268 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001269 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001270
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001271 if (_PyRuntime.warnings.once_registry == NULL) {
1272 _PyRuntime.warnings.once_registry = PyDict_New();
1273 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001274 return NULL;
1275 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001276 Py_INCREF(_PyRuntime.warnings.once_registry);
1277 if (PyModule_AddObject(m, "_onceregistry",
1278 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001279 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001280
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001281 if (_PyRuntime.warnings.default_action == NULL) {
1282 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1283 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001284 return NULL;
1285 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001286 Py_INCREF(_PyRuntime.warnings.default_action);
1287 if (PyModule_AddObject(m, "_defaultaction",
1288 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001289 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001290
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001291 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001292 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001293}