blob: a9f96410c8798633cb9d890dc413f75f1c7d18c8 [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(argv);
13_Py_IDENTIFIER(stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +000014
15static int
16check_matched(PyObject *obj, PyObject *arg)
17{
18 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020019 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000020 int rc;
21
22 if (obj == Py_None)
23 return 1;
Victor Stinner55ba38a2016-12-09 16:09:30 +010024 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000025 if (result == NULL)
26 return -1;
27
28 rc = PyObject_IsTrue(result);
29 Py_DECREF(result);
30 return rc;
31}
32
33/*
34 Returns a new reference.
35 A NULL return value can mean false or an error.
36*/
37static PyObject *
Victor Stinnere98445a2016-03-23 00:54:48 +010038get_warnings_attr(const char *attr, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000039{
40 static PyObject *warnings_str = NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010041 PyObject *warnings_module, *obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000042
43 if (warnings_str == NULL) {
44 warnings_str = PyUnicode_InternFromString("warnings");
45 if (warnings_str == NULL)
46 return NULL;
47 }
48
Victor Stinnere98445a2016-03-23 00:54:48 +010049 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060050 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010051 warnings_module = PyImport_Import(warnings_str);
52 if (warnings_module == NULL) {
53 /* Fallback to the C implementation if we cannot get
54 the Python implementation */
55 PyErr_Clear();
Christian Heimes33fe8092008-04-13 13:53:33 +000056 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010057 }
58 }
59 else {
Eric Snow3f9eee62017-09-15 16:35:20 -060060 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010061 if (warnings_module == NULL)
62 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010063 }
64
65 if (!PyObject_HasAttrString(warnings_module, attr)) {
66 Py_DECREF(warnings_module);
67 return NULL;
68 }
69
70 obj = PyObject_GetAttrString(warnings_module, attr);
71 Py_DECREF(warnings_module);
72 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000073}
74
75
Neal Norwitz32dde222008-04-15 06:43:13 +000076static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000077get_once_registry(void)
78{
79 PyObject *registry;
80
Victor Stinnere98445a2016-03-23 00:54:48 +010081 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000082 if (registry == NULL) {
83 if (PyErr_Occurred())
84 return NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060085 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000086 }
Oren Milman252033d2017-09-11 09:28:39 +030087 if (!PyDict_Check(registry)) {
88 PyErr_SetString(PyExc_TypeError,
89 "warnings.onceregistry must be a dict");
90 Py_DECREF(registry);
91 return NULL;
92 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060093 Py_DECREF(_PyRuntime.warnings.once_registry);
94 _PyRuntime.warnings.once_registry = registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000095 return registry;
96}
97
98
Brett Cannon0759dd62009-04-01 18:13:07 +000099static PyObject *
100get_default_action(void)
101{
102 PyObject *default_action;
103
Victor Stinnere98445a2016-03-23 00:54:48 +0100104 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000105 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (PyErr_Occurred()) {
107 return NULL;
108 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600109 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000110 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300111 if (!PyUnicode_Check(default_action)) {
112 PyErr_Format(PyExc_TypeError,
113 MODULE_NAME ".defaultaction must be a string, "
114 "not '%.200s'",
115 Py_TYPE(default_action)->tp_name);
116 Py_DECREF(default_action);
117 return NULL;
118 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600119 Py_DECREF(_PyRuntime.warnings.default_action);
120 _PyRuntime.warnings.default_action = default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000121 return default_action;
122}
123
124
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400125/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100126static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000127get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
128 PyObject *module, PyObject **item)
129{
Brett Cannon0759dd62009-04-01 18:13:07 +0000130 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000131 Py_ssize_t i;
132 PyObject *warnings_filters;
133
Victor Stinnere98445a2016-03-23 00:54:48 +0100134 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000135 if (warnings_filters == NULL) {
136 if (PyErr_Occurred())
137 return NULL;
138 }
139 else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600140 Py_DECREF(_PyRuntime.warnings.filters);
141 _PyRuntime.warnings.filters = warnings_filters;
Christian Heimes33fe8092008-04-13 13:53:33 +0000142 }
143
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600144 PyObject *filters = _PyRuntime.warnings.filters;
145 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000146 PyErr_SetString(PyExc_ValueError,
147 MODULE_NAME ".filters must be a list");
148 return NULL;
149 }
150
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
152 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000153 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
154 Py_ssize_t ln;
155 int is_subclass, good_msg, good_mod;
156
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600157 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400158 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000159 PyErr_Format(PyExc_ValueError,
160 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
161 return NULL;
162 }
163
164 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400165 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000166 action = PyTuple_GET_ITEM(tmp_item, 0);
167 msg = PyTuple_GET_ITEM(tmp_item, 1);
168 cat = PyTuple_GET_ITEM(tmp_item, 2);
169 mod = PyTuple_GET_ITEM(tmp_item, 3);
170 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
171
Oren Milman9d984fd2017-09-12 00:18:09 +0300172 if (!PyUnicode_Check(action)) {
173 PyErr_Format(PyExc_TypeError,
174 "action must be a string, not '%.200s'",
175 Py_TYPE(action)->tp_name);
176 Py_DECREF(tmp_item);
177 return NULL;
178 }
179
Christian Heimes33fe8092008-04-13 13:53:33 +0000180 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400181 if (good_msg == -1) {
182 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100183 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400184 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100185
Christian Heimes33fe8092008-04-13 13:53:33 +0000186 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400187 if (good_mod == -1) {
188 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100189 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400190 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100191
Christian Heimes33fe8092008-04-13 13:53:33 +0000192 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400193 if (is_subclass == -1) {
194 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100195 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400196 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100197
Christian Heimes33fe8092008-04-13 13:53:33 +0000198 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400199 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400200 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000201 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400202 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000203
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400204 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
205 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100206 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400207 }
208
209 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000210 }
211
Brett Cannon0759dd62009-04-01 18:13:07 +0000212 action = get_default_action();
213 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400214 Py_INCREF(Py_None);
215 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100216 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000217 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000218
Christian Heimes33fe8092008-04-13 13:53:33 +0000219 return NULL;
220}
221
Brett Cannon0759dd62009-04-01 18:13:07 +0000222
Christian Heimes33fe8092008-04-13 13:53:33 +0000223static int
224already_warned(PyObject *registry, PyObject *key, int should_set)
225{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200226 PyObject *version_obj, *already_warned;
227 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000228
229 if (key == NULL)
230 return -1;
231
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200232 version_obj = _PyDict_GetItemId(registry, &PyId_version);
233 if (version_obj == NULL
234 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600235 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200236 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600237 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200238 if (version_obj == NULL)
239 return -1;
240 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
241 Py_DECREF(version_obj);
242 return -1;
243 }
244 Py_DECREF(version_obj);
245 }
246 else {
247 already_warned = PyDict_GetItem(registry, key);
248 if (already_warned != NULL) {
249 int rc = PyObject_IsTrue(already_warned);
250 if (rc != 0)
251 return rc;
252 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000253 }
254
255 /* This warning wasn't found in the registry, set it. */
256 if (should_set)
257 return PyDict_SetItem(registry, key, Py_True);
258 return 0;
259}
260
261/* New reference. */
262static PyObject *
263normalize_module(PyObject *filename)
264{
265 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100266 int kind;
267 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000268 Py_ssize_t len;
269
Victor Stinner9e30aa52011-11-21 02:49:52 +0100270 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000271 if (len < 0)
272 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100273
274 if (len == 0)
275 return PyUnicode_FromString("<unknown>");
276
277 kind = PyUnicode_KIND(filename);
278 data = PyUnicode_DATA(filename);
279
280 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000281 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100282 PyUnicode_READ(kind, data, len-3) == '.' &&
283 PyUnicode_READ(kind, data, len-2) == 'p' &&
284 PyUnicode_READ(kind, data, len-1) == 'y')
285 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100286 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000287 }
288 else {
289 module = filename;
290 Py_INCREF(module);
291 }
292 return module;
293}
294
295static int
296update_registry(PyObject *registry, PyObject *text, PyObject *category,
297 int add_zero)
298{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300299 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000300 int rc;
301
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300302 if (add_zero)
303 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000304 else
305 altkey = PyTuple_Pack(2, text, category);
306
307 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000308 Py_XDECREF(altkey);
309 return rc;
310}
311
312static void
Victor Stinner914cde82016-03-19 01:03:51 +0100313show_warning(PyObject *filename, int lineno, PyObject *text,
314 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 PyObject *f_stderr;
317 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000318 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200319 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000320
321 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
322
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200323 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000324 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100325 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000326
Victor Stinnerbd303c12013-11-07 23:07:29 +0100327 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000328 if (f_stderr == NULL) {
329 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100330 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000331 }
332
333 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100334 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
335 goto error;
336 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
337 goto error;
338 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
339 goto error;
340 if (PyFile_WriteString(": ", f_stderr) < 0)
341 goto error;
342 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
343 goto error;
344 if (PyFile_WriteString("\n", f_stderr) < 0)
345 goto error;
346 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000347
348 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000349 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100350 int kind;
351 void *data;
352 Py_ssize_t i, len;
353 Py_UCS4 ch;
354 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000355
Victor Stinnera4c704b2013-10-29 23:43:41 +0100356 if (PyUnicode_READY(sourceline) < 1)
357 goto error;
358
359 kind = PyUnicode_KIND(sourceline);
360 data = PyUnicode_DATA(sourceline);
361 len = PyUnicode_GET_LENGTH(sourceline);
362 for (i=0; i<len; i++) {
363 ch = PyUnicode_READ(kind, data, i);
364 if (ch != ' ' && ch != '\t' && ch != '\014')
365 break;
366 }
367
368 truncated = PyUnicode_Substring(sourceline, i, len);
369 if (truncated == NULL)
370 goto error;
371
372 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
373 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000374 PyFile_WriteString("\n", f_stderr);
375 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200376 else {
377 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
378 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100379
380error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100381 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000382 PyErr_Clear();
383}
384
Victor Stinner1231a462016-03-19 00:47:17 +0100385static int
386call_show_warning(PyObject *category, PyObject *text, PyObject *message,
387 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100388 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100389{
390 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
391
Victor Stinnere98445a2016-03-23 00:54:48 +0100392 /* If the source parameter is set, try to get the Python implementation.
393 The Python implementation is able to log the traceback where the source
394 was allocated, whereas the C implementation doesnt. */
395 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100396 if (show_fn == NULL) {
397 if (PyErr_Occurred())
398 return -1;
399 show_warning(filename, lineno, text, category, sourceline);
400 return 0;
401 }
402
403 if (!PyCallable_Check(show_fn)) {
404 PyErr_SetString(PyExc_TypeError,
405 "warnings._showwarnmsg() must be set to a callable");
406 goto error;
407 }
408
Victor Stinnere98445a2016-03-23 00:54:48 +0100409 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100410 if (warnmsg_cls == NULL) {
411 PyErr_SetString(PyExc_RuntimeError,
412 "unable to get warnings.WarningMessage");
413 goto error;
414 }
415
416 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100417 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100418 NULL);
419 Py_DECREF(warnmsg_cls);
420 if (msg == NULL)
421 goto error;
422
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100423 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100424 Py_DECREF(show_fn);
425 Py_DECREF(msg);
426
427 if (res == NULL)
428 return -1;
429
430 Py_DECREF(res);
431 return 0;
432
433error:
434 Py_XDECREF(show_fn);
435 return -1;
436}
437
Christian Heimes33fe8092008-04-13 13:53:33 +0000438static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000440 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100441 PyObject *module, PyObject *registry, PyObject *sourceline,
442 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000443{
444 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400445 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100446 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000447 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100449 /* module can be None if a warning is emitted late during Python shutdown.
450 In this case, the Python warnings module was probably unloaded, filters
451 are no more available to choose as action. It is safer to ignore the
452 warning and do nothing. */
453 if (module == Py_None)
454 Py_RETURN_NONE;
455
Brett Cannondb734912008-06-27 00:52:15 +0000456 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300457 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000458 return NULL;
459 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000460
461 /* Normalize module. */
462 if (module == NULL) {
463 module = normalize_module(filename);
464 if (module == NULL)
465 return NULL;
466 }
467 else
468 Py_INCREF(module);
469
470 /* Normalize message. */
471 Py_INCREF(message); /* DECREF'ed in cleanup. */
472 rc = PyObject_IsInstance(message, PyExc_Warning);
473 if (rc == -1) {
474 goto cleanup;
475 }
476 if (rc == 1) {
477 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000478 if (text == NULL)
479 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000480 category = (PyObject*)message->ob_type;
481 }
482 else {
483 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100484 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000485 if (message == NULL)
486 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000487 }
488
489 lineno_obj = PyLong_FromLong(lineno);
490 if (lineno_obj == NULL)
491 goto cleanup;
492
Victor Stinner22f18752016-12-09 18:08:18 +0100493 if (source == Py_None) {
494 source = NULL;
495 }
496
Christian Heimes33fe8092008-04-13 13:53:33 +0000497 /* Create key. */
498 key = PyTuple_Pack(3, text, category, lineno_obj);
499 if (key == NULL)
500 goto cleanup;
501
Brett Cannondb734912008-06-27 00:52:15 +0000502 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000503 rc = already_warned(registry, key, 0);
504 if (rc == -1)
505 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000507 goto return_none;
508 /* Else this warning hasn't been generated before. */
509 }
510
511 action = get_filter(category, text, lineno, module, &item);
512 if (action == NULL)
513 goto cleanup;
514
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200515 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000516 PyErr_SetObject(category, message);
517 goto cleanup;
518 }
519
520 /* Store in the registry that we've been here, *except* when the action
521 is "always". */
522 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200523 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000524 if (registry != NULL && registry != Py_None &&
525 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000526 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200527 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000528 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200529 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000530 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000531 registry = get_once_registry();
532 if (registry == NULL)
533 goto cleanup;
534 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600535 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000537 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200538 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000539 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000540 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000542 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200543 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000544 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100545 "Unrecognized action (%R) in warnings.filters:\n %R",
546 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000547 goto cleanup;
548 }
549 }
550
Christian Heimes1a8501c2008-10-02 19:56:01 +0000551 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000552 goto return_none;
553 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100554 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100555 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100556 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000557 }
558 else /* if (rc == -1) */
559 goto cleanup;
560
561 return_none:
562 result = Py_None;
563 Py_INCREF(result);
564
565 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400566 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000567 Py_XDECREF(key);
568 Py_XDECREF(text);
569 Py_XDECREF(lineno_obj);
570 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000571 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000572 return result; /* Py_None or NULL. */
573}
574
Larry Hastings714e4932015-09-06 00:39:37 -0700575static int
576is_internal_frame(PyFrameObject *frame)
577{
578 static PyObject *importlib_string = NULL;
579 static PyObject *bootstrap_string = NULL;
580 PyObject *filename;
581 int contains;
582
583 if (importlib_string == NULL) {
584 importlib_string = PyUnicode_FromString("importlib");
585 if (importlib_string == NULL) {
586 return 0;
587 }
588
589 bootstrap_string = PyUnicode_FromString("_bootstrap");
590 if (bootstrap_string == NULL) {
591 Py_DECREF(importlib_string);
592 return 0;
593 }
594 Py_INCREF(importlib_string);
595 Py_INCREF(bootstrap_string);
596 }
597
598 if (frame == NULL || frame->f_code == NULL ||
599 frame->f_code->co_filename == NULL) {
600 return 0;
601 }
602 filename = frame->f_code->co_filename;
603 if (!PyUnicode_Check(filename)) {
604 return 0;
605 }
606 contains = PyUnicode_Contains(filename, importlib_string);
607 if (contains < 0) {
608 return 0;
609 }
610 else if (contains > 0) {
611 contains = PyUnicode_Contains(filename, bootstrap_string);
612 if (contains < 0) {
613 return 0;
614 }
615 else if (contains > 0) {
616 return 1;
617 }
618 }
619
620 return 0;
621}
622
623static PyFrameObject *
624next_external_frame(PyFrameObject *frame)
625{
626 do {
627 frame = frame->f_back;
628 } while (frame != NULL && is_internal_frame(frame));
629
630 return frame;
631}
632
Christian Heimes33fe8092008-04-13 13:53:33 +0000633/* filename, module, and registry are new refs, globals is borrowed */
634/* Returns 0 on error (no new refs), 1 on success */
635static int
636setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
637 PyObject **module, PyObject **registry)
638{
639 PyObject *globals;
640
641 /* Setup globals and lineno. */
642 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700643 // Stack level comparisons to Python code is off by one as there is no
644 // warnings-related stack level to avoid.
645 if (stack_level <= 0 || is_internal_frame(f)) {
646 while (--stack_level > 0 && f != NULL) {
647 f = f->f_back;
648 }
649 }
650 else {
651 while (--stack_level > 0 && f != NULL) {
652 f = next_external_frame(f);
653 }
654 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000655
656 if (f == NULL) {
657 globals = PyThreadState_Get()->interp->sysdict;
658 *lineno = 1;
659 }
660 else {
661 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000662 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000663 }
664
665 *module = NULL;
666
667 /* Setup registry. */
668 assert(globals != NULL);
669 assert(PyDict_Check(globals));
670 *registry = PyDict_GetItemString(globals, "__warningregistry__");
671 if (*registry == NULL) {
672 int rc;
673
674 *registry = PyDict_New();
675 if (*registry == NULL)
676 return 0;
677
678 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
679 if (rc < 0)
680 goto handle_error;
681 }
682 else
683 Py_INCREF(*registry);
684
685 /* Setup module. */
686 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300687 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
688 Py_INCREF(*module);
689 }
690 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000691 *module = PyUnicode_FromString("<string>");
692 if (*module == NULL)
693 goto handle_error;
694 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000695
696 /* Setup filename. */
697 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200698 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200699 Py_ssize_t len;
700 int kind;
701 void *data;
702
703 if (PyUnicode_READY(*filename))
704 goto handle_error;
705
Victor Stinner9e30aa52011-11-21 02:49:52 +0100706 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200707 kind = PyUnicode_KIND(*filename);
708 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000709
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500710#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400711 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000712 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500714 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
715 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400716 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000717 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200718 *filename = PyUnicode_Substring(*filename, 0,
719 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000720 if (*filename == NULL)
721 goto handle_error;
722 }
723 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000724 Py_INCREF(*filename);
725 }
726 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500727 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200728 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100729 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100730 /* PyList_Check() is needed because sys.argv is set to None during
731 Python finalization */
732 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000733 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000734 *filename = PyList_GetItem(argv, 0);
735 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000736 /* If sys.argv[0] is false, then use '__main__'. */
737 is_true = PyObject_IsTrue(*filename);
738 if (is_true < 0) {
739 Py_DECREF(*filename);
740 goto handle_error;
741 }
742 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300743 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000744 if (*filename == NULL)
745 goto handle_error;
746 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000747 }
748 else {
749 /* embedded interpreters don't have sys.argv, see bug #839151 */
750 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100751 if (*filename == NULL)
752 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000753 }
754 }
755 if (*filename == NULL) {
756 *filename = *module;
757 Py_INCREF(*filename);
758 }
759 }
760
761 return 1;
762
763 handle_error:
764 /* filename not XDECREF'ed here as there is no way to jump here with a
765 dangling reference. */
766 Py_XDECREF(*registry);
767 Py_XDECREF(*module);
768 return 0;
769}
770
771static PyObject *
772get_category(PyObject *message, PyObject *category)
773{
774 int rc;
775
776 /* Get category. */
777 rc = PyObject_IsInstance(message, PyExc_Warning);
778 if (rc == -1)
779 return NULL;
780
781 if (rc == 1)
782 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300783 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000784 category = PyExc_UserWarning;
785
786 /* Validate category. */
787 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300788 /* category is not a subclass of PyExc_Warning or
789 PyObject_IsSubclass raised an error */
790 if (rc == -1 || rc == 0) {
791 PyErr_Format(PyExc_TypeError,
792 "category must be a Warning subclass, not '%s'",
793 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000794 return NULL;
795 }
796
797 return category;
798}
799
800static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100801do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
802 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000803{
804 PyObject *filename, *module, *registry, *res;
805 int lineno;
806
807 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
808 return NULL;
809
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100810 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100811 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000812 Py_DECREF(filename);
813 Py_DECREF(registry);
814 Py_DECREF(module);
815 return res;
816}
817
Victor Stinner22f18752016-12-09 18:08:18 +0100818/*[clinic input]
819warn as warnings_warn
820
821 message: object
822 category: object = None
823 stacklevel: Py_ssize_t = 1
824 source: object = None
825
826Issue a warning, or maybe ignore it or raise an exception.
827[clinic start generated code]*/
828
Christian Heimes33fe8092008-04-13 13:53:33 +0000829static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100830warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
831 Py_ssize_t stacklevel, PyObject *source)
832/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000833{
Christian Heimes33fe8092008-04-13 13:53:33 +0000834 category = get_category(message, category);
835 if (category == NULL)
836 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100837 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000838}
839
840static PyObject *
841warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
842{
843 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100844 "module", "registry", "module_globals",
845 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000846 PyObject *message;
847 PyObject *category;
848 PyObject *filename;
849 int lineno;
850 PyObject *module = NULL;
851 PyObject *registry = NULL;
852 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100853 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000854
Victor Stinner914cde82016-03-19 01:03:51 +0100855 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000856 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100857 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000858 return NULL;
859
860 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200861 _Py_IDENTIFIER(get_source);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200862 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000863 PyObject *loader;
864 PyObject *module_name;
865 PyObject *source;
866 PyObject *source_list;
867 PyObject *source_line;
868 PyObject *returned;
869
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200870 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
871 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000872
873 /* Check/get the requisite pieces needed for the loader. */
874 loader = PyDict_GetItemString(module_globals, "__loader__");
875 module_name = PyDict_GetItemString(module_globals, "__name__");
876
877 if (loader == NULL || module_name == NULL)
878 goto standard_call;
879
880 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200881 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000882 goto standard_call;
883 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200884 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
885 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000886 if (!source)
887 return NULL;
888 else if (source == Py_None) {
889 Py_DECREF(Py_None);
890 goto standard_call;
891 }
892
893 /* Split the source into lines. */
Oren Milman91fb0af2017-09-24 21:27:12 +0300894 source_list = PyUnicode_Splitlines(source, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000895 Py_DECREF(source);
896 if (!source_list)
897 return NULL;
898
899 /* Get the source line. */
900 source_line = PyList_GetItem(source_list, lineno-1);
901 if (!source_line) {
902 Py_DECREF(source_list);
903 return NULL;
904 }
905
906 /* Handle the warning. */
907 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100908 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000909 Py_DECREF(source_list);
910 return returned;
911 }
912
913 standard_call:
914 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100915 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000916}
917
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200918static PyObject *
919warnings_filters_mutated(PyObject *self, PyObject *args)
920{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600921 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200922 Py_RETURN_NONE;
923}
924
Christian Heimes33fe8092008-04-13 13:53:33 +0000925
926/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000927
928static int
929warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100930 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000931{
932 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000933
934 if (category == NULL)
935 category = PyExc_RuntimeWarning;
936
Victor Stinner914cde82016-03-19 01:03:51 +0100937 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000938 if (res == NULL)
939 return -1;
940 Py_DECREF(res);
941
942 return 0;
943}
944
Victor Stinner914cde82016-03-19 01:03:51 +0100945static int
946_PyErr_WarnFormatV(PyObject *source,
947 PyObject *category, Py_ssize_t stack_level,
948 const char *format, va_list vargs)
949{
950 PyObject *message;
951 int res;
952
953 message = PyUnicode_FromFormatV(format, vargs);
954 if (message == NULL)
955 return -1;
956
957 res = warn_unicode(category, message, stack_level, source);
958 Py_DECREF(message);
959 return res;
960}
961
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000962int
963PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
964 const char *format, ...)
965{
Victor Stinner914cde82016-03-19 01:03:51 +0100966 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000967 va_list vargs;
968
969#ifdef HAVE_STDARG_PROTOTYPES
970 va_start(vargs, format);
971#else
972 va_start(vargs);
973#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100974 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000975 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100976 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000977}
978
979int
Victor Stinner914cde82016-03-19 01:03:51 +0100980PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
981 const char *format, ...)
982{
983 int res;
984 va_list vargs;
985
986#ifdef HAVE_STDARG_PROTOTYPES
987 va_start(vargs, format);
988#else
989 va_start(vargs);
990#endif
991 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
992 stack_level, format, vargs);
993 va_end(vargs);
994 return res;
995}
996
997
998int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000999PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1000{
1001 int ret;
1002 PyObject *message = PyUnicode_FromString(text);
1003 if (message == NULL)
1004 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001005 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001006 Py_DECREF(message);
1007 return ret;
1008}
1009
Ezio Melotti42da6632011-03-15 05:18:48 +02001010/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001011 Use PyErr_WarnEx instead. */
1012
1013#undef PyErr_Warn
1014
1015PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001016PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001017{
1018 return PyErr_WarnEx(category, text, 1);
1019}
1020
1021/* Warning with explicit origin */
1022int
Victor Stinner14e461d2013-08-26 22:28:21 +02001023PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1024 PyObject *filename, int lineno,
1025 PyObject *module, PyObject *registry)
1026{
1027 PyObject *res;
1028 if (category == NULL)
1029 category = PyExc_RuntimeWarning;
1030 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001031 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001032 if (res == NULL)
1033 return -1;
1034 Py_DECREF(res);
1035 return 0;
1036}
1037
1038int
Christian Heimes33fe8092008-04-13 13:53:33 +00001039PyErr_WarnExplicit(PyObject *category, const char *text,
1040 const char *filename_str, int lineno,
1041 const char *module_str, PyObject *registry)
1042{
Christian Heimes33fe8092008-04-13 13:53:33 +00001043 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001044 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001045 PyObject *module = NULL;
1046 int ret = -1;
1047
1048 if (message == NULL || filename == NULL)
1049 goto exit;
1050 if (module_str != NULL) {
1051 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001052 if (module == NULL)
1053 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001054 }
1055
Victor Stinner14e461d2013-08-26 22:28:21 +02001056 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1057 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001058
1059 exit:
1060 Py_XDECREF(message);
1061 Py_XDECREF(module);
1062 Py_XDECREF(filename);
1063 return ret;
1064}
1065
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001066int
1067PyErr_WarnExplicitFormat(PyObject *category,
1068 const char *filename_str, int lineno,
1069 const char *module_str, PyObject *registry,
1070 const char *format, ...)
1071{
1072 PyObject *message;
1073 PyObject *module = NULL;
1074 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1075 int ret = -1;
1076 va_list vargs;
1077
1078 if (filename == NULL)
1079 goto exit;
1080 if (module_str != NULL) {
1081 module = PyUnicode_FromString(module_str);
1082 if (module == NULL)
1083 goto exit;
1084 }
1085
1086#ifdef HAVE_STDARG_PROTOTYPES
1087 va_start(vargs, format);
1088#else
1089 va_start(vargs);
1090#endif
1091 message = PyUnicode_FromFormatV(format, vargs);
1092 if (message != NULL) {
1093 PyObject *res;
1094 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001095 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001096 Py_DECREF(message);
1097 if (res != NULL) {
1098 Py_DECREF(res);
1099 ret = 0;
1100 }
1101 }
1102 va_end(vargs);
1103exit:
1104 Py_XDECREF(module);
1105 Py_XDECREF(filename);
1106 return ret;
1107}
1108
Christian Heimes33fe8092008-04-13 13:53:33 +00001109
Christian Heimes33fe8092008-04-13 13:53:33 +00001110PyDoc_STRVAR(warn_explicit_doc,
1111"Low-level inferface to warnings functionality.");
1112
1113static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001114 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001115 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1116 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001117 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1118 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001119 /* XXX(brett.cannon): add showwarning? */
1120 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001122};
1123
1124
1125static PyObject *
1126create_filter(PyObject *category, const char *action)
1127{
1128 static PyObject *ignore_str = NULL;
1129 static PyObject *error_str = NULL;
1130 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001131 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001132 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001133
1134 if (!strcmp(action, "ignore")) {
1135 if (ignore_str == NULL) {
1136 ignore_str = PyUnicode_InternFromString("ignore");
1137 if (ignore_str == NULL)
1138 return NULL;
1139 }
1140 action_obj = ignore_str;
1141 }
1142 else if (!strcmp(action, "error")) {
1143 if (error_str == NULL) {
1144 error_str = PyUnicode_InternFromString("error");
1145 if (error_str == NULL)
1146 return NULL;
1147 }
1148 action_obj = error_str;
1149 }
1150 else if (!strcmp(action, "default")) {
1151 if (default_str == NULL) {
1152 default_str = PyUnicode_InternFromString("default");
1153 if (default_str == NULL)
1154 return NULL;
1155 }
1156 action_obj = default_str;
1157 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001158 else if (!strcmp(action, "always")) {
1159 if (always_str == NULL) {
1160 always_str = PyUnicode_InternFromString("always");
1161 if (always_str == NULL)
1162 return NULL;
1163 }
1164 action_obj = always_str;
1165 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001166 else {
1167 Py_FatalError("unknown action");
1168 }
1169
1170 /* This assumes the line number is zero for now. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001171 return PyTuple_Pack(5, action_obj, Py_None,
1172 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001173}
1174
1175static PyObject *
1176init_filters(void)
1177{
Georg Brandl08be72d2010-10-24 15:11:22 +00001178 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001179 unsigned int pos = 0; /* Post-incremented in each use. */
1180 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001181 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001182
Christian Heimes33fe8092008-04-13 13:53:33 +00001183 if (filters == NULL)
1184 return NULL;
1185
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001186 PyList_SET_ITEM(filters, pos++,
1187 create_filter(PyExc_DeprecationWarning, "ignore"));
1188 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001189 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001190 PyList_SET_ITEM(filters, pos++,
1191 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001192 if (Py_BytesWarningFlag > 1)
1193 bytes_action = "error";
1194 else if (Py_BytesWarningFlag)
1195 bytes_action = "default";
1196 else
1197 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001198 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001199 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001200 /* resource usage warnings are enabled by default in pydebug mode */
1201#ifdef Py_DEBUG
1202 resource_action = "always";
1203#else
1204 resource_action = "ignore";
1205#endif
1206 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1207 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001208 for (x = 0; x < pos; x += 1) {
1209 if (PyList_GET_ITEM(filters, x) == NULL) {
1210 Py_DECREF(filters);
1211 return NULL;
1212 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001213 }
1214
1215 return filters;
1216}
1217
Martin v. Löwis1a214512008-06-11 05:26:20 +00001218static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PyModuleDef_HEAD_INIT,
1220 MODULE_NAME,
1221 warnings__doc__,
1222 0,
1223 warnings_functions,
1224 NULL,
1225 NULL,
1226 NULL,
1227 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001228};
1229
Christian Heimes33fe8092008-04-13 13:53:33 +00001230
1231PyMODINIT_FUNC
1232_PyWarnings_Init(void)
1233{
Brett Cannon0759dd62009-04-01 18:13:07 +00001234 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001235
Martin v. Löwis1a214512008-06-11 05:26:20 +00001236 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001237 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001238 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001239
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001240 if (_PyRuntime.warnings.filters == NULL) {
1241 _PyRuntime.warnings.filters = init_filters();
1242 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001243 return NULL;
1244 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001245 Py_INCREF(_PyRuntime.warnings.filters);
1246 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001247 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001248
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001249 if (_PyRuntime.warnings.once_registry == NULL) {
1250 _PyRuntime.warnings.once_registry = PyDict_New();
1251 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001252 return NULL;
1253 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001254 Py_INCREF(_PyRuntime.warnings.once_registry);
1255 if (PyModule_AddObject(m, "_onceregistry",
1256 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001257 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001258
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001259 if (_PyRuntime.warnings.default_action == NULL) {
1260 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1261 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001262 return NULL;
1263 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001264 Py_INCREF(_PyRuntime.warnings.default_action);
1265 if (PyModule_AddObject(m, "_defaultaction",
1266 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001267 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001268
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001269 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001270 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001271}