blob: f6688b040679ad28993ea19e2f24335e885122b7 [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__");
687 if (*module == NULL) {
688 *module = PyUnicode_FromString("<string>");
689 if (*module == NULL)
690 goto handle_error;
691 }
692 else
693 Py_INCREF(*module);
694
695 /* Setup filename. */
696 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200697 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200698 Py_ssize_t len;
699 int kind;
700 void *data;
701
702 if (PyUnicode_READY(*filename))
703 goto handle_error;
704
Victor Stinner9e30aa52011-11-21 02:49:52 +0100705 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200706 kind = PyUnicode_KIND(*filename);
707 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000708
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500709#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400710 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000711 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200712 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500713 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
714 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400715 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000716 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200717 *filename = PyUnicode_Substring(*filename, 0,
718 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000719 if (*filename == NULL)
720 goto handle_error;
721 }
722 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000723 Py_INCREF(*filename);
724 }
725 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500726 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200727 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100728 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100729 /* PyList_Check() is needed because sys.argv is set to None during
730 Python finalization */
731 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000732 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000733 *filename = PyList_GetItem(argv, 0);
734 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000735 /* If sys.argv[0] is false, then use '__main__'. */
736 is_true = PyObject_IsTrue(*filename);
737 if (is_true < 0) {
738 Py_DECREF(*filename);
739 goto handle_error;
740 }
741 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300742 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000743 if (*filename == NULL)
744 goto handle_error;
745 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000746 }
747 else {
748 /* embedded interpreters don't have sys.argv, see bug #839151 */
749 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100750 if (*filename == NULL)
751 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000752 }
753 }
754 if (*filename == NULL) {
755 *filename = *module;
756 Py_INCREF(*filename);
757 }
758 }
759
760 return 1;
761
762 handle_error:
763 /* filename not XDECREF'ed here as there is no way to jump here with a
764 dangling reference. */
765 Py_XDECREF(*registry);
766 Py_XDECREF(*module);
767 return 0;
768}
769
770static PyObject *
771get_category(PyObject *message, PyObject *category)
772{
773 int rc;
774
775 /* Get category. */
776 rc = PyObject_IsInstance(message, PyExc_Warning);
777 if (rc == -1)
778 return NULL;
779
780 if (rc == 1)
781 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300782 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000783 category = PyExc_UserWarning;
784
785 /* Validate category. */
786 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300787 /* category is not a subclass of PyExc_Warning or
788 PyObject_IsSubclass raised an error */
789 if (rc == -1 || rc == 0) {
790 PyErr_Format(PyExc_TypeError,
791 "category must be a Warning subclass, not '%s'",
792 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000793 return NULL;
794 }
795
796 return category;
797}
798
799static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100800do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
801 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000802{
803 PyObject *filename, *module, *registry, *res;
804 int lineno;
805
806 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
807 return NULL;
808
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100809 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100810 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000811 Py_DECREF(filename);
812 Py_DECREF(registry);
813 Py_DECREF(module);
814 return res;
815}
816
Victor Stinner22f18752016-12-09 18:08:18 +0100817/*[clinic input]
818warn as warnings_warn
819
820 message: object
821 category: object = None
822 stacklevel: Py_ssize_t = 1
823 source: object = None
824
825Issue a warning, or maybe ignore it or raise an exception.
826[clinic start generated code]*/
827
Christian Heimes33fe8092008-04-13 13:53:33 +0000828static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100829warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
830 Py_ssize_t stacklevel, PyObject *source)
831/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000832{
Christian Heimes33fe8092008-04-13 13:53:33 +0000833 category = get_category(message, category);
834 if (category == NULL)
835 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100836 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000837}
838
839static PyObject *
840warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
841{
842 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100843 "module", "registry", "module_globals",
844 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000845 PyObject *message;
846 PyObject *category;
847 PyObject *filename;
848 int lineno;
849 PyObject *module = NULL;
850 PyObject *registry = NULL;
851 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100852 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000853
Victor Stinner914cde82016-03-19 01:03:51 +0100854 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000855 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100856 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000857 return NULL;
858
859 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200860 _Py_IDENTIFIER(get_source);
861 _Py_IDENTIFIER(splitlines);
862 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;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200872 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
873 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000874
875 /* Check/get the requisite pieces needed for the loader. */
876 loader = PyDict_GetItemString(module_globals, "__loader__");
877 module_name = PyDict_GetItemString(module_globals, "__name__");
878
879 if (loader == NULL || module_name == NULL)
880 goto standard_call;
881
882 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200883 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000884 goto standard_call;
885 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200886 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
887 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000888 if (!source)
889 return NULL;
890 else if (source == Py_None) {
891 Py_DECREF(Py_None);
892 goto standard_call;
893 }
894
895 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100896 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200897 PyId_splitlines.object,
898 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000899 Py_DECREF(source);
900 if (!source_list)
901 return NULL;
902
903 /* Get the source line. */
904 source_line = PyList_GetItem(source_list, lineno-1);
905 if (!source_line) {
906 Py_DECREF(source_list);
907 return NULL;
908 }
909
910 /* Handle the warning. */
911 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100912 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000913 Py_DECREF(source_list);
914 return returned;
915 }
916
917 standard_call:
918 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100919 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000920}
921
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200922static PyObject *
923warnings_filters_mutated(PyObject *self, PyObject *args)
924{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600925 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200926 Py_RETURN_NONE;
927}
928
Christian Heimes33fe8092008-04-13 13:53:33 +0000929
930/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000931
932static int
933warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100934 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000935{
936 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000937
938 if (category == NULL)
939 category = PyExc_RuntimeWarning;
940
Victor Stinner914cde82016-03-19 01:03:51 +0100941 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000942 if (res == NULL)
943 return -1;
944 Py_DECREF(res);
945
946 return 0;
947}
948
Victor Stinner914cde82016-03-19 01:03:51 +0100949static int
950_PyErr_WarnFormatV(PyObject *source,
951 PyObject *category, Py_ssize_t stack_level,
952 const char *format, va_list vargs)
953{
954 PyObject *message;
955 int res;
956
957 message = PyUnicode_FromFormatV(format, vargs);
958 if (message == NULL)
959 return -1;
960
961 res = warn_unicode(category, message, stack_level, source);
962 Py_DECREF(message);
963 return res;
964}
965
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000966int
967PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
968 const char *format, ...)
969{
Victor Stinner914cde82016-03-19 01:03:51 +0100970 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000971 va_list vargs;
972
973#ifdef HAVE_STDARG_PROTOTYPES
974 va_start(vargs, format);
975#else
976 va_start(vargs);
977#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100978 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000979 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100980 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000981}
982
983int
Victor Stinner914cde82016-03-19 01:03:51 +0100984PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
985 const char *format, ...)
986{
987 int res;
988 va_list vargs;
989
990#ifdef HAVE_STDARG_PROTOTYPES
991 va_start(vargs, format);
992#else
993 va_start(vargs);
994#endif
995 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
996 stack_level, format, vargs);
997 va_end(vargs);
998 return res;
999}
1000
1001
1002int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001003PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1004{
1005 int ret;
1006 PyObject *message = PyUnicode_FromString(text);
1007 if (message == NULL)
1008 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001009 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001010 Py_DECREF(message);
1011 return ret;
1012}
1013
Ezio Melotti42da6632011-03-15 05:18:48 +02001014/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001015 Use PyErr_WarnEx instead. */
1016
1017#undef PyErr_Warn
1018
1019PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001020PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001021{
1022 return PyErr_WarnEx(category, text, 1);
1023}
1024
1025/* Warning with explicit origin */
1026int
Victor Stinner14e461d2013-08-26 22:28:21 +02001027PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1028 PyObject *filename, int lineno,
1029 PyObject *module, PyObject *registry)
1030{
1031 PyObject *res;
1032 if (category == NULL)
1033 category = PyExc_RuntimeWarning;
1034 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001035 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001036 if (res == NULL)
1037 return -1;
1038 Py_DECREF(res);
1039 return 0;
1040}
1041
1042int
Christian Heimes33fe8092008-04-13 13:53:33 +00001043PyErr_WarnExplicit(PyObject *category, const char *text,
1044 const char *filename_str, int lineno,
1045 const char *module_str, PyObject *registry)
1046{
Christian Heimes33fe8092008-04-13 13:53:33 +00001047 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001048 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001049 PyObject *module = NULL;
1050 int ret = -1;
1051
1052 if (message == NULL || filename == NULL)
1053 goto exit;
1054 if (module_str != NULL) {
1055 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001056 if (module == NULL)
1057 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001058 }
1059
Victor Stinner14e461d2013-08-26 22:28:21 +02001060 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1061 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001062
1063 exit:
1064 Py_XDECREF(message);
1065 Py_XDECREF(module);
1066 Py_XDECREF(filename);
1067 return ret;
1068}
1069
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001070int
1071PyErr_WarnExplicitFormat(PyObject *category,
1072 const char *filename_str, int lineno,
1073 const char *module_str, PyObject *registry,
1074 const char *format, ...)
1075{
1076 PyObject *message;
1077 PyObject *module = NULL;
1078 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1079 int ret = -1;
1080 va_list vargs;
1081
1082 if (filename == NULL)
1083 goto exit;
1084 if (module_str != NULL) {
1085 module = PyUnicode_FromString(module_str);
1086 if (module == NULL)
1087 goto exit;
1088 }
1089
1090#ifdef HAVE_STDARG_PROTOTYPES
1091 va_start(vargs, format);
1092#else
1093 va_start(vargs);
1094#endif
1095 message = PyUnicode_FromFormatV(format, vargs);
1096 if (message != NULL) {
1097 PyObject *res;
1098 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001099 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001100 Py_DECREF(message);
1101 if (res != NULL) {
1102 Py_DECREF(res);
1103 ret = 0;
1104 }
1105 }
1106 va_end(vargs);
1107exit:
1108 Py_XDECREF(module);
1109 Py_XDECREF(filename);
1110 return ret;
1111}
1112
Christian Heimes33fe8092008-04-13 13:53:33 +00001113
Christian Heimes33fe8092008-04-13 13:53:33 +00001114PyDoc_STRVAR(warn_explicit_doc,
1115"Low-level inferface to warnings functionality.");
1116
1117static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001118 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001119 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1120 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001121 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1122 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001123 /* XXX(brett.cannon): add showwarning? */
1124 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001126};
1127
1128
1129static PyObject *
1130create_filter(PyObject *category, const char *action)
1131{
1132 static PyObject *ignore_str = NULL;
1133 static PyObject *error_str = NULL;
1134 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001135 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001136 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001137
1138 if (!strcmp(action, "ignore")) {
1139 if (ignore_str == NULL) {
1140 ignore_str = PyUnicode_InternFromString("ignore");
1141 if (ignore_str == NULL)
1142 return NULL;
1143 }
1144 action_obj = ignore_str;
1145 }
1146 else if (!strcmp(action, "error")) {
1147 if (error_str == NULL) {
1148 error_str = PyUnicode_InternFromString("error");
1149 if (error_str == NULL)
1150 return NULL;
1151 }
1152 action_obj = error_str;
1153 }
1154 else if (!strcmp(action, "default")) {
1155 if (default_str == NULL) {
1156 default_str = PyUnicode_InternFromString("default");
1157 if (default_str == NULL)
1158 return NULL;
1159 }
1160 action_obj = default_str;
1161 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001162 else if (!strcmp(action, "always")) {
1163 if (always_str == NULL) {
1164 always_str = PyUnicode_InternFromString("always");
1165 if (always_str == NULL)
1166 return NULL;
1167 }
1168 action_obj = always_str;
1169 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001170 else {
1171 Py_FatalError("unknown action");
1172 }
1173
1174 /* This assumes the line number is zero for now. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001175 return PyTuple_Pack(5, action_obj, Py_None,
1176 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001177}
1178
1179static PyObject *
1180init_filters(void)
1181{
Georg Brandl08be72d2010-10-24 15:11:22 +00001182 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001183 unsigned int pos = 0; /* Post-incremented in each use. */
1184 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001185 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001186
Christian Heimes33fe8092008-04-13 13:53:33 +00001187 if (filters == NULL)
1188 return NULL;
1189
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001190 PyList_SET_ITEM(filters, pos++,
1191 create_filter(PyExc_DeprecationWarning, "ignore"));
1192 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001193 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001194 PyList_SET_ITEM(filters, pos++,
1195 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001196 if (Py_BytesWarningFlag > 1)
1197 bytes_action = "error";
1198 else if (Py_BytesWarningFlag)
1199 bytes_action = "default";
1200 else
1201 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001202 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001203 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001204 /* resource usage warnings are enabled by default in pydebug mode */
1205#ifdef Py_DEBUG
1206 resource_action = "always";
1207#else
1208 resource_action = "ignore";
1209#endif
1210 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1211 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001212 for (x = 0; x < pos; x += 1) {
1213 if (PyList_GET_ITEM(filters, x) == NULL) {
1214 Py_DECREF(filters);
1215 return NULL;
1216 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001217 }
1218
1219 return filters;
1220}
1221
Martin v. Löwis1a214512008-06-11 05:26:20 +00001222static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 PyModuleDef_HEAD_INIT,
1224 MODULE_NAME,
1225 warnings__doc__,
1226 0,
1227 warnings_functions,
1228 NULL,
1229 NULL,
1230 NULL,
1231 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001232};
1233
Christian Heimes33fe8092008-04-13 13:53:33 +00001234
1235PyMODINIT_FUNC
1236_PyWarnings_Init(void)
1237{
Brett Cannon0759dd62009-04-01 18:13:07 +00001238 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001239
Martin v. Löwis1a214512008-06-11 05:26:20 +00001240 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001241 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001242 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001243
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001244 if (_PyRuntime.warnings.filters == NULL) {
1245 _PyRuntime.warnings.filters = init_filters();
1246 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001247 return NULL;
1248 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001249 Py_INCREF(_PyRuntime.warnings.filters);
1250 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001251 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001252
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001253 if (_PyRuntime.warnings.once_registry == NULL) {
1254 _PyRuntime.warnings.once_registry = PyDict_New();
1255 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001256 return NULL;
1257 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001258 Py_INCREF(_PyRuntime.warnings.once_registry);
1259 if (PyModule_AddObject(m, "_onceregistry",
1260 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001261 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001262
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001263 if (_PyRuntime.warnings.default_action == NULL) {
1264 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1265 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001266 return NULL;
1267 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001268 Py_INCREF(_PyRuntime.warnings.default_action);
1269 if (PyModule_AddObject(m, "_defaultaction",
1270 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001271 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001272
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001273 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001274 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001275}