blob: 4ea9fce47cf020490632e2edd6eeed354370f737 [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);
862 _Py_IDENTIFIER(splitlines);
863 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000864 PyObject *loader;
865 PyObject *module_name;
866 PyObject *source;
867 PyObject *source_list;
868 PyObject *source_line;
869 PyObject *returned;
870
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200871 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
872 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200873 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
874 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000875
876 /* Check/get the requisite pieces needed for the loader. */
877 loader = PyDict_GetItemString(module_globals, "__loader__");
878 module_name = PyDict_GetItemString(module_globals, "__name__");
879
880 if (loader == NULL || module_name == NULL)
881 goto standard_call;
882
883 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200884 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000885 goto standard_call;
886 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200887 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
888 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000889 if (!source)
890 return NULL;
891 else if (source == Py_None) {
892 Py_DECREF(Py_None);
893 goto standard_call;
894 }
895
896 /* Split the source into lines. */
Oren Milman91fb0af2017-09-24 21:27:12 +0300897 source_list = PyUnicode_Splitlines(source, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000898 Py_DECREF(source);
899 if (!source_list)
900 return NULL;
901
902 /* Get the source line. */
903 source_line = PyList_GetItem(source_list, lineno-1);
904 if (!source_line) {
905 Py_DECREF(source_list);
906 return NULL;
907 }
908
909 /* Handle the warning. */
910 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100911 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000912 Py_DECREF(source_list);
913 return returned;
914 }
915
916 standard_call:
917 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100918 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000919}
920
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200921static PyObject *
922warnings_filters_mutated(PyObject *self, PyObject *args)
923{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600924 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200925 Py_RETURN_NONE;
926}
927
Christian Heimes33fe8092008-04-13 13:53:33 +0000928
929/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000930
931static int
932warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100933 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000934{
935 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000936
937 if (category == NULL)
938 category = PyExc_RuntimeWarning;
939
Victor Stinner914cde82016-03-19 01:03:51 +0100940 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000941 if (res == NULL)
942 return -1;
943 Py_DECREF(res);
944
945 return 0;
946}
947
Victor Stinner914cde82016-03-19 01:03:51 +0100948static int
949_PyErr_WarnFormatV(PyObject *source,
950 PyObject *category, Py_ssize_t stack_level,
951 const char *format, va_list vargs)
952{
953 PyObject *message;
954 int res;
955
956 message = PyUnicode_FromFormatV(format, vargs);
957 if (message == NULL)
958 return -1;
959
960 res = warn_unicode(category, message, stack_level, source);
961 Py_DECREF(message);
962 return res;
963}
964
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000965int
966PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
967 const char *format, ...)
968{
Victor Stinner914cde82016-03-19 01:03:51 +0100969 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000970 va_list vargs;
971
972#ifdef HAVE_STDARG_PROTOTYPES
973 va_start(vargs, format);
974#else
975 va_start(vargs);
976#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100977 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000978 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100979 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000980}
981
982int
Victor Stinner914cde82016-03-19 01:03:51 +0100983PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
984 const char *format, ...)
985{
986 int res;
987 va_list vargs;
988
989#ifdef HAVE_STDARG_PROTOTYPES
990 va_start(vargs, format);
991#else
992 va_start(vargs);
993#endif
994 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
995 stack_level, format, vargs);
996 va_end(vargs);
997 return res;
998}
999
1000
1001int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001002PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1003{
1004 int ret;
1005 PyObject *message = PyUnicode_FromString(text);
1006 if (message == NULL)
1007 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001008 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001009 Py_DECREF(message);
1010 return ret;
1011}
1012
Ezio Melotti42da6632011-03-15 05:18:48 +02001013/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001014 Use PyErr_WarnEx instead. */
1015
1016#undef PyErr_Warn
1017
1018PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001019PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001020{
1021 return PyErr_WarnEx(category, text, 1);
1022}
1023
1024/* Warning with explicit origin */
1025int
Victor Stinner14e461d2013-08-26 22:28:21 +02001026PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1027 PyObject *filename, int lineno,
1028 PyObject *module, PyObject *registry)
1029{
1030 PyObject *res;
1031 if (category == NULL)
1032 category = PyExc_RuntimeWarning;
1033 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001034 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001035 if (res == NULL)
1036 return -1;
1037 Py_DECREF(res);
1038 return 0;
1039}
1040
1041int
Christian Heimes33fe8092008-04-13 13:53:33 +00001042PyErr_WarnExplicit(PyObject *category, const char *text,
1043 const char *filename_str, int lineno,
1044 const char *module_str, PyObject *registry)
1045{
Christian Heimes33fe8092008-04-13 13:53:33 +00001046 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001047 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001048 PyObject *module = NULL;
1049 int ret = -1;
1050
1051 if (message == NULL || filename == NULL)
1052 goto exit;
1053 if (module_str != NULL) {
1054 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001055 if (module == NULL)
1056 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001057 }
1058
Victor Stinner14e461d2013-08-26 22:28:21 +02001059 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1060 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001061
1062 exit:
1063 Py_XDECREF(message);
1064 Py_XDECREF(module);
1065 Py_XDECREF(filename);
1066 return ret;
1067}
1068
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001069int
1070PyErr_WarnExplicitFormat(PyObject *category,
1071 const char *filename_str, int lineno,
1072 const char *module_str, PyObject *registry,
1073 const char *format, ...)
1074{
1075 PyObject *message;
1076 PyObject *module = NULL;
1077 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1078 int ret = -1;
1079 va_list vargs;
1080
1081 if (filename == NULL)
1082 goto exit;
1083 if (module_str != NULL) {
1084 module = PyUnicode_FromString(module_str);
1085 if (module == NULL)
1086 goto exit;
1087 }
1088
1089#ifdef HAVE_STDARG_PROTOTYPES
1090 va_start(vargs, format);
1091#else
1092 va_start(vargs);
1093#endif
1094 message = PyUnicode_FromFormatV(format, vargs);
1095 if (message != NULL) {
1096 PyObject *res;
1097 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001098 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001099 Py_DECREF(message);
1100 if (res != NULL) {
1101 Py_DECREF(res);
1102 ret = 0;
1103 }
1104 }
1105 va_end(vargs);
1106exit:
1107 Py_XDECREF(module);
1108 Py_XDECREF(filename);
1109 return ret;
1110}
1111
Christian Heimes33fe8092008-04-13 13:53:33 +00001112
Christian Heimes33fe8092008-04-13 13:53:33 +00001113PyDoc_STRVAR(warn_explicit_doc,
1114"Low-level inferface to warnings functionality.");
1115
1116static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001117 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001118 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1119 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001120 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1121 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001122 /* XXX(brett.cannon): add showwarning? */
1123 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001125};
1126
1127
1128static PyObject *
1129create_filter(PyObject *category, const char *action)
1130{
1131 static PyObject *ignore_str = NULL;
1132 static PyObject *error_str = NULL;
1133 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001134 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001135 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001136
1137 if (!strcmp(action, "ignore")) {
1138 if (ignore_str == NULL) {
1139 ignore_str = PyUnicode_InternFromString("ignore");
1140 if (ignore_str == NULL)
1141 return NULL;
1142 }
1143 action_obj = ignore_str;
1144 }
1145 else if (!strcmp(action, "error")) {
1146 if (error_str == NULL) {
1147 error_str = PyUnicode_InternFromString("error");
1148 if (error_str == NULL)
1149 return NULL;
1150 }
1151 action_obj = error_str;
1152 }
1153 else if (!strcmp(action, "default")) {
1154 if (default_str == NULL) {
1155 default_str = PyUnicode_InternFromString("default");
1156 if (default_str == NULL)
1157 return NULL;
1158 }
1159 action_obj = default_str;
1160 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001161 else if (!strcmp(action, "always")) {
1162 if (always_str == NULL) {
1163 always_str = PyUnicode_InternFromString("always");
1164 if (always_str == NULL)
1165 return NULL;
1166 }
1167 action_obj = always_str;
1168 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001169 else {
1170 Py_FatalError("unknown action");
1171 }
1172
1173 /* This assumes the line number is zero for now. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001174 return PyTuple_Pack(5, action_obj, Py_None,
1175 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001176}
1177
1178static PyObject *
1179init_filters(void)
1180{
Georg Brandl08be72d2010-10-24 15:11:22 +00001181 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001182 unsigned int pos = 0; /* Post-incremented in each use. */
1183 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001184 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001185
Christian Heimes33fe8092008-04-13 13:53:33 +00001186 if (filters == NULL)
1187 return NULL;
1188
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001189 PyList_SET_ITEM(filters, pos++,
1190 create_filter(PyExc_DeprecationWarning, "ignore"));
1191 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001192 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001193 PyList_SET_ITEM(filters, pos++,
1194 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001195 if (Py_BytesWarningFlag > 1)
1196 bytes_action = "error";
1197 else if (Py_BytesWarningFlag)
1198 bytes_action = "default";
1199 else
1200 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001201 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001202 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001203 /* resource usage warnings are enabled by default in pydebug mode */
1204#ifdef Py_DEBUG
1205 resource_action = "always";
1206#else
1207 resource_action = "ignore";
1208#endif
1209 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1210 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001211 for (x = 0; x < pos; x += 1) {
1212 if (PyList_GET_ITEM(filters, x) == NULL) {
1213 Py_DECREF(filters);
1214 return NULL;
1215 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001216 }
1217
1218 return filters;
1219}
1220
Martin v. Löwis1a214512008-06-11 05:26:20 +00001221static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 PyModuleDef_HEAD_INIT,
1223 MODULE_NAME,
1224 warnings__doc__,
1225 0,
1226 warnings_functions,
1227 NULL,
1228 NULL,
1229 NULL,
1230 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001231};
1232
Christian Heimes33fe8092008-04-13 13:53:33 +00001233
1234PyMODINIT_FUNC
1235_PyWarnings_Init(void)
1236{
Brett Cannon0759dd62009-04-01 18:13:07 +00001237 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001238
Martin v. Löwis1a214512008-06-11 05:26:20 +00001239 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001240 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001241 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001242
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001243 if (_PyRuntime.warnings.filters == NULL) {
1244 _PyRuntime.warnings.filters = init_filters();
1245 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001246 return NULL;
1247 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001248 Py_INCREF(_PyRuntime.warnings.filters);
1249 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001250 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001251
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001252 if (_PyRuntime.warnings.once_registry == NULL) {
1253 _PyRuntime.warnings.once_registry = PyDict_New();
1254 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001255 return NULL;
1256 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001257 Py_INCREF(_PyRuntime.warnings.once_registry);
1258 if (PyModule_AddObject(m, "_onceregistry",
1259 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001260 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001261
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001262 if (_PyRuntime.warnings.default_action == NULL) {
1263 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1264 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001265 return NULL;
1266 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001267 Py_INCREF(_PyRuntime.warnings.default_action);
1268 if (PyModule_AddObject(m, "_defaultaction",
1269 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001270 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001271
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001272 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001273 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001274}