blob: 6dad08068672ee3e0ec934c915caf6f63216b1fc [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 Snow86b7afd2017-09-04 17:54:09 -060060 warnings_module = _PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010061 if (warnings_module == NULL)
62 return NULL;
63
Victor Stinnere98445a2016-03-23 00:54:48 +010064 Py_INCREF(warnings_module);
65 }
66
67 if (!PyObject_HasAttrString(warnings_module, attr)) {
68 Py_DECREF(warnings_module);
69 return NULL;
70 }
71
72 obj = PyObject_GetAttrString(warnings_module, attr);
73 Py_DECREF(warnings_module);
74 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000075}
76
77
Neal Norwitz32dde222008-04-15 06:43:13 +000078static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000079get_once_registry(void)
80{
81 PyObject *registry;
82
Victor Stinnere98445a2016-03-23 00:54:48 +010083 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000084 if (registry == NULL) {
85 if (PyErr_Occurred())
86 return NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060087 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000088 }
Oren Milman252033d2017-09-11 09:28:39 +030089 if (!PyDict_Check(registry)) {
90 PyErr_SetString(PyExc_TypeError,
91 "warnings.onceregistry must be a dict");
92 Py_DECREF(registry);
93 return NULL;
94 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060095 Py_DECREF(_PyRuntime.warnings.once_registry);
96 _PyRuntime.warnings.once_registry = registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000097 return registry;
98}
99
100
Brett Cannon0759dd62009-04-01 18:13:07 +0000101static PyObject *
102get_default_action(void)
103{
104 PyObject *default_action;
105
Victor Stinnere98445a2016-03-23 00:54:48 +0100106 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000107 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (PyErr_Occurred()) {
109 return NULL;
110 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600111 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000112 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300113 if (!PyUnicode_Check(default_action)) {
114 PyErr_Format(PyExc_TypeError,
115 MODULE_NAME ".defaultaction must be a string, "
116 "not '%.200s'",
117 Py_TYPE(default_action)->tp_name);
118 Py_DECREF(default_action);
119 return NULL;
120 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600121 Py_DECREF(_PyRuntime.warnings.default_action);
122 _PyRuntime.warnings.default_action = default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000123 return default_action;
124}
125
126
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400127/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100128static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000129get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
130 PyObject *module, PyObject **item)
131{
Brett Cannon0759dd62009-04-01 18:13:07 +0000132 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000133 Py_ssize_t i;
134 PyObject *warnings_filters;
135
Victor Stinnere98445a2016-03-23 00:54:48 +0100136 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000137 if (warnings_filters == NULL) {
138 if (PyErr_Occurred())
139 return NULL;
140 }
141 else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600142 Py_DECREF(_PyRuntime.warnings.filters);
143 _PyRuntime.warnings.filters = warnings_filters;
Christian Heimes33fe8092008-04-13 13:53:33 +0000144 }
145
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600146 PyObject *filters = _PyRuntime.warnings.filters;
147 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000148 PyErr_SetString(PyExc_ValueError,
149 MODULE_NAME ".filters must be a list");
150 return NULL;
151 }
152
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600153 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
154 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000155 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
156 Py_ssize_t ln;
157 int is_subclass, good_msg, good_mod;
158
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600159 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400160 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000161 PyErr_Format(PyExc_ValueError,
162 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
163 return NULL;
164 }
165
166 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400167 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000168 action = PyTuple_GET_ITEM(tmp_item, 0);
169 msg = PyTuple_GET_ITEM(tmp_item, 1);
170 cat = PyTuple_GET_ITEM(tmp_item, 2);
171 mod = PyTuple_GET_ITEM(tmp_item, 3);
172 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
173
Oren Milman9d984fd2017-09-12 00:18:09 +0300174 if (!PyUnicode_Check(action)) {
175 PyErr_Format(PyExc_TypeError,
176 "action must be a string, not '%.200s'",
177 Py_TYPE(action)->tp_name);
178 Py_DECREF(tmp_item);
179 return NULL;
180 }
181
Christian Heimes33fe8092008-04-13 13:53:33 +0000182 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400183 if (good_msg == -1) {
184 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100185 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400186 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100187
Christian Heimes33fe8092008-04-13 13:53:33 +0000188 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400189 if (good_mod == -1) {
190 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100191 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400192 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100193
Christian Heimes33fe8092008-04-13 13:53:33 +0000194 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400195 if (is_subclass == -1) {
196 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100197 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400198 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100199
Christian Heimes33fe8092008-04-13 13:53:33 +0000200 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400201 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400202 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000203 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400204 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000205
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400206 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
207 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100208 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400209 }
210
211 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000212 }
213
Brett Cannon0759dd62009-04-01 18:13:07 +0000214 action = get_default_action();
215 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400216 Py_INCREF(Py_None);
217 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100218 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000219 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000220
Christian Heimes33fe8092008-04-13 13:53:33 +0000221 return NULL;
222}
223
Brett Cannon0759dd62009-04-01 18:13:07 +0000224
Christian Heimes33fe8092008-04-13 13:53:33 +0000225static int
226already_warned(PyObject *registry, PyObject *key, int should_set)
227{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200228 PyObject *version_obj, *already_warned;
229 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000230
231 if (key == NULL)
232 return -1;
233
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200234 version_obj = _PyDict_GetItemId(registry, &PyId_version);
235 if (version_obj == NULL
236 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600237 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200238 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600239 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200240 if (version_obj == NULL)
241 return -1;
242 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
243 Py_DECREF(version_obj);
244 return -1;
245 }
246 Py_DECREF(version_obj);
247 }
248 else {
249 already_warned = PyDict_GetItem(registry, key);
250 if (already_warned != NULL) {
251 int rc = PyObject_IsTrue(already_warned);
252 if (rc != 0)
253 return rc;
254 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000255 }
256
257 /* This warning wasn't found in the registry, set it. */
258 if (should_set)
259 return PyDict_SetItem(registry, key, Py_True);
260 return 0;
261}
262
263/* New reference. */
264static PyObject *
265normalize_module(PyObject *filename)
266{
267 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100268 int kind;
269 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000270 Py_ssize_t len;
271
Victor Stinner9e30aa52011-11-21 02:49:52 +0100272 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000273 if (len < 0)
274 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100275
276 if (len == 0)
277 return PyUnicode_FromString("<unknown>");
278
279 kind = PyUnicode_KIND(filename);
280 data = PyUnicode_DATA(filename);
281
282 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000283 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100284 PyUnicode_READ(kind, data, len-3) == '.' &&
285 PyUnicode_READ(kind, data, len-2) == 'p' &&
286 PyUnicode_READ(kind, data, len-1) == 'y')
287 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100288 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000289 }
290 else {
291 module = filename;
292 Py_INCREF(module);
293 }
294 return module;
295}
296
297static int
298update_registry(PyObject *registry, PyObject *text, PyObject *category,
299 int add_zero)
300{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300301 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000302 int rc;
303
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300304 if (add_zero)
305 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000306 else
307 altkey = PyTuple_Pack(2, text, category);
308
309 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000310 Py_XDECREF(altkey);
311 return rc;
312}
313
314static void
Victor Stinner914cde82016-03-19 01:03:51 +0100315show_warning(PyObject *filename, int lineno, PyObject *text,
316 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyObject *f_stderr;
319 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000320 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200321 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000322
323 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
324
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200325 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000326 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100327 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000328
Victor Stinnerbd303c12013-11-07 23:07:29 +0100329 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000330 if (f_stderr == NULL) {
331 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100332 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000333 }
334
335 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100336 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
337 goto error;
338 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
339 goto error;
340 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
341 goto error;
342 if (PyFile_WriteString(": ", f_stderr) < 0)
343 goto error;
344 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
345 goto error;
346 if (PyFile_WriteString("\n", f_stderr) < 0)
347 goto error;
348 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000349
350 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000351 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100352 int kind;
353 void *data;
354 Py_ssize_t i, len;
355 Py_UCS4 ch;
356 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000357
Victor Stinnera4c704b2013-10-29 23:43:41 +0100358 if (PyUnicode_READY(sourceline) < 1)
359 goto error;
360
361 kind = PyUnicode_KIND(sourceline);
362 data = PyUnicode_DATA(sourceline);
363 len = PyUnicode_GET_LENGTH(sourceline);
364 for (i=0; i<len; i++) {
365 ch = PyUnicode_READ(kind, data, i);
366 if (ch != ' ' && ch != '\t' && ch != '\014')
367 break;
368 }
369
370 truncated = PyUnicode_Substring(sourceline, i, len);
371 if (truncated == NULL)
372 goto error;
373
374 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
375 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000376 PyFile_WriteString("\n", f_stderr);
377 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200378 else {
379 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
380 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100381
382error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100383 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000384 PyErr_Clear();
385}
386
Victor Stinner1231a462016-03-19 00:47:17 +0100387static int
388call_show_warning(PyObject *category, PyObject *text, PyObject *message,
389 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100390 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100391{
392 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
393
Victor Stinnere98445a2016-03-23 00:54:48 +0100394 /* If the source parameter is set, try to get the Python implementation.
395 The Python implementation is able to log the traceback where the source
396 was allocated, whereas the C implementation doesnt. */
397 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100398 if (show_fn == NULL) {
399 if (PyErr_Occurred())
400 return -1;
401 show_warning(filename, lineno, text, category, sourceline);
402 return 0;
403 }
404
405 if (!PyCallable_Check(show_fn)) {
406 PyErr_SetString(PyExc_TypeError,
407 "warnings._showwarnmsg() must be set to a callable");
408 goto error;
409 }
410
Victor Stinnere98445a2016-03-23 00:54:48 +0100411 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100412 if (warnmsg_cls == NULL) {
413 PyErr_SetString(PyExc_RuntimeError,
414 "unable to get warnings.WarningMessage");
415 goto error;
416 }
417
418 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100419 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100420 NULL);
421 Py_DECREF(warnmsg_cls);
422 if (msg == NULL)
423 goto error;
424
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100425 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100426 Py_DECREF(show_fn);
427 Py_DECREF(msg);
428
429 if (res == NULL)
430 return -1;
431
432 Py_DECREF(res);
433 return 0;
434
435error:
436 Py_XDECREF(show_fn);
437 return -1;
438}
439
Christian Heimes33fe8092008-04-13 13:53:33 +0000440static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000442 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100443 PyObject *module, PyObject *registry, PyObject *sourceline,
444 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000445{
446 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400447 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100448 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000449 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100451 /* module can be None if a warning is emitted late during Python shutdown.
452 In this case, the Python warnings module was probably unloaded, filters
453 are no more available to choose as action. It is safer to ignore the
454 warning and do nothing. */
455 if (module == Py_None)
456 Py_RETURN_NONE;
457
Brett Cannondb734912008-06-27 00:52:15 +0000458 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300459 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000460 return NULL;
461 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000462
463 /* Normalize module. */
464 if (module == NULL) {
465 module = normalize_module(filename);
466 if (module == NULL)
467 return NULL;
468 }
469 else
470 Py_INCREF(module);
471
472 /* Normalize message. */
473 Py_INCREF(message); /* DECREF'ed in cleanup. */
474 rc = PyObject_IsInstance(message, PyExc_Warning);
475 if (rc == -1) {
476 goto cleanup;
477 }
478 if (rc == 1) {
479 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000480 if (text == NULL)
481 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000482 category = (PyObject*)message->ob_type;
483 }
484 else {
485 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100486 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000487 if (message == NULL)
488 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000489 }
490
491 lineno_obj = PyLong_FromLong(lineno);
492 if (lineno_obj == NULL)
493 goto cleanup;
494
Victor Stinner22f18752016-12-09 18:08:18 +0100495 if (source == Py_None) {
496 source = NULL;
497 }
498
Christian Heimes33fe8092008-04-13 13:53:33 +0000499 /* Create key. */
500 key = PyTuple_Pack(3, text, category, lineno_obj);
501 if (key == NULL)
502 goto cleanup;
503
Brett Cannondb734912008-06-27 00:52:15 +0000504 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000505 rc = already_warned(registry, key, 0);
506 if (rc == -1)
507 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000509 goto return_none;
510 /* Else this warning hasn't been generated before. */
511 }
512
513 action = get_filter(category, text, lineno, module, &item);
514 if (action == NULL)
515 goto cleanup;
516
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200517 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000518 PyErr_SetObject(category, message);
519 goto cleanup;
520 }
521
522 /* Store in the registry that we've been here, *except* when the action
523 is "always". */
524 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200525 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000526 if (registry != NULL && registry != Py_None &&
527 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000528 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200529 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000530 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200531 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000532 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000533 registry = get_once_registry();
534 if (registry == NULL)
535 goto cleanup;
536 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600537 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000539 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200540 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000541 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000542 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000544 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200545 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000546 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100547 "Unrecognized action (%R) in warnings.filters:\n %R",
548 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000549 goto cleanup;
550 }
551 }
552
Christian Heimes1a8501c2008-10-02 19:56:01 +0000553 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000554 goto return_none;
555 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100556 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100557 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100558 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000559 }
560 else /* if (rc == -1) */
561 goto cleanup;
562
563 return_none:
564 result = Py_None;
565 Py_INCREF(result);
566
567 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400568 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000569 Py_XDECREF(key);
570 Py_XDECREF(text);
571 Py_XDECREF(lineno_obj);
572 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000573 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000574 return result; /* Py_None or NULL. */
575}
576
Larry Hastings714e4932015-09-06 00:39:37 -0700577static int
578is_internal_frame(PyFrameObject *frame)
579{
580 static PyObject *importlib_string = NULL;
581 static PyObject *bootstrap_string = NULL;
582 PyObject *filename;
583 int contains;
584
585 if (importlib_string == NULL) {
586 importlib_string = PyUnicode_FromString("importlib");
587 if (importlib_string == NULL) {
588 return 0;
589 }
590
591 bootstrap_string = PyUnicode_FromString("_bootstrap");
592 if (bootstrap_string == NULL) {
593 Py_DECREF(importlib_string);
594 return 0;
595 }
596 Py_INCREF(importlib_string);
597 Py_INCREF(bootstrap_string);
598 }
599
600 if (frame == NULL || frame->f_code == NULL ||
601 frame->f_code->co_filename == NULL) {
602 return 0;
603 }
604 filename = frame->f_code->co_filename;
605 if (!PyUnicode_Check(filename)) {
606 return 0;
607 }
608 contains = PyUnicode_Contains(filename, importlib_string);
609 if (contains < 0) {
610 return 0;
611 }
612 else if (contains > 0) {
613 contains = PyUnicode_Contains(filename, bootstrap_string);
614 if (contains < 0) {
615 return 0;
616 }
617 else if (contains > 0) {
618 return 1;
619 }
620 }
621
622 return 0;
623}
624
625static PyFrameObject *
626next_external_frame(PyFrameObject *frame)
627{
628 do {
629 frame = frame->f_back;
630 } while (frame != NULL && is_internal_frame(frame));
631
632 return frame;
633}
634
Christian Heimes33fe8092008-04-13 13:53:33 +0000635/* filename, module, and registry are new refs, globals is borrowed */
636/* Returns 0 on error (no new refs), 1 on success */
637static int
638setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
639 PyObject **module, PyObject **registry)
640{
641 PyObject *globals;
642
643 /* Setup globals and lineno. */
644 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700645 // Stack level comparisons to Python code is off by one as there is no
646 // warnings-related stack level to avoid.
647 if (stack_level <= 0 || is_internal_frame(f)) {
648 while (--stack_level > 0 && f != NULL) {
649 f = f->f_back;
650 }
651 }
652 else {
653 while (--stack_level > 0 && f != NULL) {
654 f = next_external_frame(f);
655 }
656 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000657
658 if (f == NULL) {
659 globals = PyThreadState_Get()->interp->sysdict;
660 *lineno = 1;
661 }
662 else {
663 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000664 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000665 }
666
667 *module = NULL;
668
669 /* Setup registry. */
670 assert(globals != NULL);
671 assert(PyDict_Check(globals));
672 *registry = PyDict_GetItemString(globals, "__warningregistry__");
673 if (*registry == NULL) {
674 int rc;
675
676 *registry = PyDict_New();
677 if (*registry == NULL)
678 return 0;
679
680 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
681 if (rc < 0)
682 goto handle_error;
683 }
684 else
685 Py_INCREF(*registry);
686
687 /* Setup module. */
688 *module = PyDict_GetItemString(globals, "__name__");
689 if (*module == NULL) {
690 *module = PyUnicode_FromString("<string>");
691 if (*module == NULL)
692 goto handle_error;
693 }
694 else
695 Py_INCREF(*module);
696
697 /* Setup filename. */
698 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200699 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200700 Py_ssize_t len;
701 int kind;
702 void *data;
703
704 if (PyUnicode_READY(*filename))
705 goto handle_error;
706
Victor Stinner9e30aa52011-11-21 02:49:52 +0100707 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200708 kind = PyUnicode_KIND(*filename);
709 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000710
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500711#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400712 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000713 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200714 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500715 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
716 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400717 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000718 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200719 *filename = PyUnicode_Substring(*filename, 0,
720 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000721 if (*filename == NULL)
722 goto handle_error;
723 }
724 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000725 Py_INCREF(*filename);
726 }
727 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500728 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200729 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100730 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100731 /* PyList_Check() is needed because sys.argv is set to None during
732 Python finalization */
733 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000734 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000735 *filename = PyList_GetItem(argv, 0);
736 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000737 /* If sys.argv[0] is false, then use '__main__'. */
738 is_true = PyObject_IsTrue(*filename);
739 if (is_true < 0) {
740 Py_DECREF(*filename);
741 goto handle_error;
742 }
743 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300744 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000745 if (*filename == NULL)
746 goto handle_error;
747 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000748 }
749 else {
750 /* embedded interpreters don't have sys.argv, see bug #839151 */
751 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100752 if (*filename == NULL)
753 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000754 }
755 }
756 if (*filename == NULL) {
757 *filename = *module;
758 Py_INCREF(*filename);
759 }
760 }
761
762 return 1;
763
764 handle_error:
765 /* filename not XDECREF'ed here as there is no way to jump here with a
766 dangling reference. */
767 Py_XDECREF(*registry);
768 Py_XDECREF(*module);
769 return 0;
770}
771
772static PyObject *
773get_category(PyObject *message, PyObject *category)
774{
775 int rc;
776
777 /* Get category. */
778 rc = PyObject_IsInstance(message, PyExc_Warning);
779 if (rc == -1)
780 return NULL;
781
782 if (rc == 1)
783 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300784 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000785 category = PyExc_UserWarning;
786
787 /* Validate category. */
788 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300789 /* category is not a subclass of PyExc_Warning or
790 PyObject_IsSubclass raised an error */
791 if (rc == -1 || rc == 0) {
792 PyErr_Format(PyExc_TypeError,
793 "category must be a Warning subclass, not '%s'",
794 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000795 return NULL;
796 }
797
798 return category;
799}
800
801static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100802do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
803 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000804{
805 PyObject *filename, *module, *registry, *res;
806 int lineno;
807
808 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
809 return NULL;
810
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100811 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100812 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000813 Py_DECREF(filename);
814 Py_DECREF(registry);
815 Py_DECREF(module);
816 return res;
817}
818
Victor Stinner22f18752016-12-09 18:08:18 +0100819/*[clinic input]
820warn as warnings_warn
821
822 message: object
823 category: object = None
824 stacklevel: Py_ssize_t = 1
825 source: object = None
826
827Issue a warning, or maybe ignore it or raise an exception.
828[clinic start generated code]*/
829
Christian Heimes33fe8092008-04-13 13:53:33 +0000830static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100831warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
832 Py_ssize_t stacklevel, PyObject *source)
833/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000834{
Christian Heimes33fe8092008-04-13 13:53:33 +0000835 category = get_category(message, category);
836 if (category == NULL)
837 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100838 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000839}
840
841static PyObject *
842warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
843{
844 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100845 "module", "registry", "module_globals",
846 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000847 PyObject *message;
848 PyObject *category;
849 PyObject *filename;
850 int lineno;
851 PyObject *module = NULL;
852 PyObject *registry = NULL;
853 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100854 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000855
Victor Stinner914cde82016-03-19 01:03:51 +0100856 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000857 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100858 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000859 return NULL;
860
861 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200862 _Py_IDENTIFIER(get_source);
863 _Py_IDENTIFIER(splitlines);
864 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000865 PyObject *loader;
866 PyObject *module_name;
867 PyObject *source;
868 PyObject *source_list;
869 PyObject *source_line;
870 PyObject *returned;
871
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200872 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
873 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200874 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
875 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000876
877 /* Check/get the requisite pieces needed for the loader. */
878 loader = PyDict_GetItemString(module_globals, "__loader__");
879 module_name = PyDict_GetItemString(module_globals, "__name__");
880
881 if (loader == NULL || module_name == NULL)
882 goto standard_call;
883
884 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200885 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000886 goto standard_call;
887 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200888 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
889 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000890 if (!source)
891 return NULL;
892 else if (source == Py_None) {
893 Py_DECREF(Py_None);
894 goto standard_call;
895 }
896
897 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100898 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200899 PyId_splitlines.object,
900 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000901 Py_DECREF(source);
902 if (!source_list)
903 return NULL;
904
905 /* Get the source line. */
906 source_line = PyList_GetItem(source_list, lineno-1);
907 if (!source_line) {
908 Py_DECREF(source_list);
909 return NULL;
910 }
911
912 /* Handle the warning. */
913 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100914 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000915 Py_DECREF(source_list);
916 return returned;
917 }
918
919 standard_call:
920 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100921 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000922}
923
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200924static PyObject *
925warnings_filters_mutated(PyObject *self, PyObject *args)
926{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600927 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200928 Py_RETURN_NONE;
929}
930
Christian Heimes33fe8092008-04-13 13:53:33 +0000931
932/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000933
934static int
935warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100936 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000937{
938 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000939
940 if (category == NULL)
941 category = PyExc_RuntimeWarning;
942
Victor Stinner914cde82016-03-19 01:03:51 +0100943 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000944 if (res == NULL)
945 return -1;
946 Py_DECREF(res);
947
948 return 0;
949}
950
Victor Stinner914cde82016-03-19 01:03:51 +0100951static int
952_PyErr_WarnFormatV(PyObject *source,
953 PyObject *category, Py_ssize_t stack_level,
954 const char *format, va_list vargs)
955{
956 PyObject *message;
957 int res;
958
959 message = PyUnicode_FromFormatV(format, vargs);
960 if (message == NULL)
961 return -1;
962
963 res = warn_unicode(category, message, stack_level, source);
964 Py_DECREF(message);
965 return res;
966}
967
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000968int
969PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
970 const char *format, ...)
971{
Victor Stinner914cde82016-03-19 01:03:51 +0100972 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000973 va_list vargs;
974
975#ifdef HAVE_STDARG_PROTOTYPES
976 va_start(vargs, format);
977#else
978 va_start(vargs);
979#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100980 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000981 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100982 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000983}
984
985int
Victor Stinner914cde82016-03-19 01:03:51 +0100986PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
987 const char *format, ...)
988{
989 int res;
990 va_list vargs;
991
992#ifdef HAVE_STDARG_PROTOTYPES
993 va_start(vargs, format);
994#else
995 va_start(vargs);
996#endif
997 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
998 stack_level, format, vargs);
999 va_end(vargs);
1000 return res;
1001}
1002
1003
1004int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001005PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1006{
1007 int ret;
1008 PyObject *message = PyUnicode_FromString(text);
1009 if (message == NULL)
1010 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001011 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001012 Py_DECREF(message);
1013 return ret;
1014}
1015
Ezio Melotti42da6632011-03-15 05:18:48 +02001016/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001017 Use PyErr_WarnEx instead. */
1018
1019#undef PyErr_Warn
1020
1021PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001022PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001023{
1024 return PyErr_WarnEx(category, text, 1);
1025}
1026
1027/* Warning with explicit origin */
1028int
Victor Stinner14e461d2013-08-26 22:28:21 +02001029PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1030 PyObject *filename, int lineno,
1031 PyObject *module, PyObject *registry)
1032{
1033 PyObject *res;
1034 if (category == NULL)
1035 category = PyExc_RuntimeWarning;
1036 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001037 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001038 if (res == NULL)
1039 return -1;
1040 Py_DECREF(res);
1041 return 0;
1042}
1043
1044int
Christian Heimes33fe8092008-04-13 13:53:33 +00001045PyErr_WarnExplicit(PyObject *category, const char *text,
1046 const char *filename_str, int lineno,
1047 const char *module_str, PyObject *registry)
1048{
Christian Heimes33fe8092008-04-13 13:53:33 +00001049 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001050 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001051 PyObject *module = NULL;
1052 int ret = -1;
1053
1054 if (message == NULL || filename == NULL)
1055 goto exit;
1056 if (module_str != NULL) {
1057 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001058 if (module == NULL)
1059 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001060 }
1061
Victor Stinner14e461d2013-08-26 22:28:21 +02001062 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1063 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001064
1065 exit:
1066 Py_XDECREF(message);
1067 Py_XDECREF(module);
1068 Py_XDECREF(filename);
1069 return ret;
1070}
1071
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001072int
1073PyErr_WarnExplicitFormat(PyObject *category,
1074 const char *filename_str, int lineno,
1075 const char *module_str, PyObject *registry,
1076 const char *format, ...)
1077{
1078 PyObject *message;
1079 PyObject *module = NULL;
1080 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1081 int ret = -1;
1082 va_list vargs;
1083
1084 if (filename == NULL)
1085 goto exit;
1086 if (module_str != NULL) {
1087 module = PyUnicode_FromString(module_str);
1088 if (module == NULL)
1089 goto exit;
1090 }
1091
1092#ifdef HAVE_STDARG_PROTOTYPES
1093 va_start(vargs, format);
1094#else
1095 va_start(vargs);
1096#endif
1097 message = PyUnicode_FromFormatV(format, vargs);
1098 if (message != NULL) {
1099 PyObject *res;
1100 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001101 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001102 Py_DECREF(message);
1103 if (res != NULL) {
1104 Py_DECREF(res);
1105 ret = 0;
1106 }
1107 }
1108 va_end(vargs);
1109exit:
1110 Py_XDECREF(module);
1111 Py_XDECREF(filename);
1112 return ret;
1113}
1114
Christian Heimes33fe8092008-04-13 13:53:33 +00001115
Christian Heimes33fe8092008-04-13 13:53:33 +00001116PyDoc_STRVAR(warn_explicit_doc,
1117"Low-level inferface to warnings functionality.");
1118
1119static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001120 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001121 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1122 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001123 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1124 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001125 /* XXX(brett.cannon): add showwarning? */
1126 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001128};
1129
1130
1131static PyObject *
1132create_filter(PyObject *category, const char *action)
1133{
1134 static PyObject *ignore_str = NULL;
1135 static PyObject *error_str = NULL;
1136 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001137 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001138 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001139
1140 if (!strcmp(action, "ignore")) {
1141 if (ignore_str == NULL) {
1142 ignore_str = PyUnicode_InternFromString("ignore");
1143 if (ignore_str == NULL)
1144 return NULL;
1145 }
1146 action_obj = ignore_str;
1147 }
1148 else if (!strcmp(action, "error")) {
1149 if (error_str == NULL) {
1150 error_str = PyUnicode_InternFromString("error");
1151 if (error_str == NULL)
1152 return NULL;
1153 }
1154 action_obj = error_str;
1155 }
1156 else if (!strcmp(action, "default")) {
1157 if (default_str == NULL) {
1158 default_str = PyUnicode_InternFromString("default");
1159 if (default_str == NULL)
1160 return NULL;
1161 }
1162 action_obj = default_str;
1163 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001164 else if (!strcmp(action, "always")) {
1165 if (always_str == NULL) {
1166 always_str = PyUnicode_InternFromString("always");
1167 if (always_str == NULL)
1168 return NULL;
1169 }
1170 action_obj = always_str;
1171 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001172 else {
1173 Py_FatalError("unknown action");
1174 }
1175
1176 /* This assumes the line number is zero for now. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001177 return PyTuple_Pack(5, action_obj, Py_None,
1178 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001179}
1180
1181static PyObject *
1182init_filters(void)
1183{
Georg Brandl08be72d2010-10-24 15:11:22 +00001184 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001185 unsigned int pos = 0; /* Post-incremented in each use. */
1186 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001187 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001188
Christian Heimes33fe8092008-04-13 13:53:33 +00001189 if (filters == NULL)
1190 return NULL;
1191
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001192 PyList_SET_ITEM(filters, pos++,
1193 create_filter(PyExc_DeprecationWarning, "ignore"));
1194 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001195 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001196 PyList_SET_ITEM(filters, pos++,
1197 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001198 if (Py_BytesWarningFlag > 1)
1199 bytes_action = "error";
1200 else if (Py_BytesWarningFlag)
1201 bytes_action = "default";
1202 else
1203 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001204 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001205 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001206 /* resource usage warnings are enabled by default in pydebug mode */
1207#ifdef Py_DEBUG
1208 resource_action = "always";
1209#else
1210 resource_action = "ignore";
1211#endif
1212 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1213 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001214 for (x = 0; x < pos; x += 1) {
1215 if (PyList_GET_ITEM(filters, x) == NULL) {
1216 Py_DECREF(filters);
1217 return NULL;
1218 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001219 }
1220
1221 return filters;
1222}
1223
Martin v. Löwis1a214512008-06-11 05:26:20 +00001224static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 PyModuleDef_HEAD_INIT,
1226 MODULE_NAME,
1227 warnings__doc__,
1228 0,
1229 warnings_functions,
1230 NULL,
1231 NULL,
1232 NULL,
1233 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001234};
1235
Christian Heimes33fe8092008-04-13 13:53:33 +00001236
1237PyMODINIT_FUNC
1238_PyWarnings_Init(void)
1239{
Brett Cannon0759dd62009-04-01 18:13:07 +00001240 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001241
Martin v. Löwis1a214512008-06-11 05:26:20 +00001242 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001243 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001244 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001245
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001246 if (_PyRuntime.warnings.filters == NULL) {
1247 _PyRuntime.warnings.filters = init_filters();
1248 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001249 return NULL;
1250 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001251 Py_INCREF(_PyRuntime.warnings.filters);
1252 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001253 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001254
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001255 if (_PyRuntime.warnings.once_registry == NULL) {
1256 _PyRuntime.warnings.once_registry = PyDict_New();
1257 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001258 return NULL;
1259 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001260 Py_INCREF(_PyRuntime.warnings.once_registry);
1261 if (PyModule_AddObject(m, "_onceregistry",
1262 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001263 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001264
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001265 if (_PyRuntime.warnings.default_action == NULL) {
1266 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1267 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001268 return NULL;
1269 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001270 Py_INCREF(_PyRuntime.warnings.default_action);
1271 if (PyModule_AddObject(m, "_defaultaction",
1272 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001273 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001274
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001275 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001276 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001277}