blob: 5230318788c9ca115df897faffb73e07167fdd62 [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 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060089 Py_DECREF(_PyRuntime.warnings.once_registry);
90 _PyRuntime.warnings.once_registry = registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000091 return registry;
92}
93
94
Brett Cannon0759dd62009-04-01 18:13:07 +000095static PyObject *
96get_default_action(void)
97{
98 PyObject *default_action;
99
Victor Stinnere98445a2016-03-23 00:54:48 +0100100 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000101 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 if (PyErr_Occurred()) {
103 return NULL;
104 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600105 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000106 }
107
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600108 Py_DECREF(_PyRuntime.warnings.default_action);
109 _PyRuntime.warnings.default_action = default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000110 return default_action;
111}
112
113
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400114/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100115static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000116get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
117 PyObject *module, PyObject **item)
118{
Brett Cannon0759dd62009-04-01 18:13:07 +0000119 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000120 Py_ssize_t i;
121 PyObject *warnings_filters;
122
Victor Stinnere98445a2016-03-23 00:54:48 +0100123 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000124 if (warnings_filters == NULL) {
125 if (PyErr_Occurred())
126 return NULL;
127 }
128 else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600129 Py_DECREF(_PyRuntime.warnings.filters);
130 _PyRuntime.warnings.filters = warnings_filters;
Christian Heimes33fe8092008-04-13 13:53:33 +0000131 }
132
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600133 PyObject *filters = _PyRuntime.warnings.filters;
134 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000135 PyErr_SetString(PyExc_ValueError,
136 MODULE_NAME ".filters must be a list");
137 return NULL;
138 }
139
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600140 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
141 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000142 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
143 Py_ssize_t ln;
144 int is_subclass, good_msg, good_mod;
145
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600146 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400147 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000148 PyErr_Format(PyExc_ValueError,
149 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
150 return NULL;
151 }
152
153 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400154 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000155 action = PyTuple_GET_ITEM(tmp_item, 0);
156 msg = PyTuple_GET_ITEM(tmp_item, 1);
157 cat = PyTuple_GET_ITEM(tmp_item, 2);
158 mod = PyTuple_GET_ITEM(tmp_item, 3);
159 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
160
161 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400162 if (good_msg == -1) {
163 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100164 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400165 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100166
Christian Heimes33fe8092008-04-13 13:53:33 +0000167 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400168 if (good_mod == -1) {
169 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100170 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400171 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100172
Christian Heimes33fe8092008-04-13 13:53:33 +0000173 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400174 if (is_subclass == -1) {
175 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100176 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400177 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100178
Christian Heimes33fe8092008-04-13 13:53:33 +0000179 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400180 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400181 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000182 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400183 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000184
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400185 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
186 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100187 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400188 }
189
190 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000191 }
192
Brett Cannon0759dd62009-04-01 18:13:07 +0000193 action = get_default_action();
194 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400195 Py_INCREF(Py_None);
196 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100197 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000198 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000199
200 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000201 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000202 return NULL;
203}
204
Brett Cannon0759dd62009-04-01 18:13:07 +0000205
Christian Heimes33fe8092008-04-13 13:53:33 +0000206static int
207already_warned(PyObject *registry, PyObject *key, int should_set)
208{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200209 PyObject *version_obj, *already_warned;
210 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000211
212 if (key == NULL)
213 return -1;
214
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200215 version_obj = _PyDict_GetItemId(registry, &PyId_version);
216 if (version_obj == NULL
217 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600218 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200219 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600220 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200221 if (version_obj == NULL)
222 return -1;
223 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
224 Py_DECREF(version_obj);
225 return -1;
226 }
227 Py_DECREF(version_obj);
228 }
229 else {
230 already_warned = PyDict_GetItem(registry, key);
231 if (already_warned != NULL) {
232 int rc = PyObject_IsTrue(already_warned);
233 if (rc != 0)
234 return rc;
235 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000236 }
237
238 /* This warning wasn't found in the registry, set it. */
239 if (should_set)
240 return PyDict_SetItem(registry, key, Py_True);
241 return 0;
242}
243
244/* New reference. */
245static PyObject *
246normalize_module(PyObject *filename)
247{
248 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100249 int kind;
250 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000251 Py_ssize_t len;
252
Victor Stinner9e30aa52011-11-21 02:49:52 +0100253 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000254 if (len < 0)
255 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100256
257 if (len == 0)
258 return PyUnicode_FromString("<unknown>");
259
260 kind = PyUnicode_KIND(filename);
261 data = PyUnicode_DATA(filename);
262
263 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000264 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100265 PyUnicode_READ(kind, data, len-3) == '.' &&
266 PyUnicode_READ(kind, data, len-2) == 'p' &&
267 PyUnicode_READ(kind, data, len-1) == 'y')
268 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100269 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000270 }
271 else {
272 module = filename;
273 Py_INCREF(module);
274 }
275 return module;
276}
277
278static int
279update_registry(PyObject *registry, PyObject *text, PyObject *category,
280 int add_zero)
281{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300282 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000283 int rc;
284
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300285 if (add_zero)
286 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000287 else
288 altkey = PyTuple_Pack(2, text, category);
289
290 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000291 Py_XDECREF(altkey);
292 return rc;
293}
294
295static void
Victor Stinner914cde82016-03-19 01:03:51 +0100296show_warning(PyObject *filename, int lineno, PyObject *text,
297 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 PyObject *f_stderr;
300 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000301 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200302 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000303
304 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
305
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200306 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000307 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100308 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000309
Victor Stinnerbd303c12013-11-07 23:07:29 +0100310 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000311 if (f_stderr == NULL) {
312 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100313 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000314 }
315
316 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100317 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
318 goto error;
319 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
320 goto error;
321 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
322 goto error;
323 if (PyFile_WriteString(": ", f_stderr) < 0)
324 goto error;
325 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
326 goto error;
327 if (PyFile_WriteString("\n", f_stderr) < 0)
328 goto error;
329 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000330
331 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000332 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100333 int kind;
334 void *data;
335 Py_ssize_t i, len;
336 Py_UCS4 ch;
337 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000338
Victor Stinnera4c704b2013-10-29 23:43:41 +0100339 if (PyUnicode_READY(sourceline) < 1)
340 goto error;
341
342 kind = PyUnicode_KIND(sourceline);
343 data = PyUnicode_DATA(sourceline);
344 len = PyUnicode_GET_LENGTH(sourceline);
345 for (i=0; i<len; i++) {
346 ch = PyUnicode_READ(kind, data, i);
347 if (ch != ' ' && ch != '\t' && ch != '\014')
348 break;
349 }
350
351 truncated = PyUnicode_Substring(sourceline, i, len);
352 if (truncated == NULL)
353 goto error;
354
355 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
356 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000357 PyFile_WriteString("\n", f_stderr);
358 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200359 else {
360 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
361 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100362
363error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100364 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000365 PyErr_Clear();
366}
367
Victor Stinner1231a462016-03-19 00:47:17 +0100368static int
369call_show_warning(PyObject *category, PyObject *text, PyObject *message,
370 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100371 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100372{
373 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
374
Victor Stinnere98445a2016-03-23 00:54:48 +0100375 /* If the source parameter is set, try to get the Python implementation.
376 The Python implementation is able to log the traceback where the source
377 was allocated, whereas the C implementation doesnt. */
378 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100379 if (show_fn == NULL) {
380 if (PyErr_Occurred())
381 return -1;
382 show_warning(filename, lineno, text, category, sourceline);
383 return 0;
384 }
385
386 if (!PyCallable_Check(show_fn)) {
387 PyErr_SetString(PyExc_TypeError,
388 "warnings._showwarnmsg() must be set to a callable");
389 goto error;
390 }
391
Victor Stinnere98445a2016-03-23 00:54:48 +0100392 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100393 if (warnmsg_cls == NULL) {
394 PyErr_SetString(PyExc_RuntimeError,
395 "unable to get warnings.WarningMessage");
396 goto error;
397 }
398
399 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100400 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100401 NULL);
402 Py_DECREF(warnmsg_cls);
403 if (msg == NULL)
404 goto error;
405
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100406 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100407 Py_DECREF(show_fn);
408 Py_DECREF(msg);
409
410 if (res == NULL)
411 return -1;
412
413 Py_DECREF(res);
414 return 0;
415
416error:
417 Py_XDECREF(show_fn);
418 return -1;
419}
420
Christian Heimes33fe8092008-04-13 13:53:33 +0000421static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000423 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100424 PyObject *module, PyObject *registry, PyObject *sourceline,
425 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000426{
427 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400428 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100429 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000430 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100432 /* module can be None if a warning is emitted late during Python shutdown.
433 In this case, the Python warnings module was probably unloaded, filters
434 are no more available to choose as action. It is safer to ignore the
435 warning and do nothing. */
436 if (module == Py_None)
437 Py_RETURN_NONE;
438
Brett Cannondb734912008-06-27 00:52:15 +0000439 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
440 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
441 return NULL;
442 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000443
444 /* Normalize module. */
445 if (module == NULL) {
446 module = normalize_module(filename);
447 if (module == NULL)
448 return NULL;
449 }
450 else
451 Py_INCREF(module);
452
453 /* Normalize message. */
454 Py_INCREF(message); /* DECREF'ed in cleanup. */
455 rc = PyObject_IsInstance(message, PyExc_Warning);
456 if (rc == -1) {
457 goto cleanup;
458 }
459 if (rc == 1) {
460 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000461 if (text == NULL)
462 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000463 category = (PyObject*)message->ob_type;
464 }
465 else {
466 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100467 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000468 if (message == NULL)
469 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000470 }
471
472 lineno_obj = PyLong_FromLong(lineno);
473 if (lineno_obj == NULL)
474 goto cleanup;
475
Victor Stinner22f18752016-12-09 18:08:18 +0100476 if (source == Py_None) {
477 source = NULL;
478 }
479
Christian Heimes33fe8092008-04-13 13:53:33 +0000480 /* Create key. */
481 key = PyTuple_Pack(3, text, category, lineno_obj);
482 if (key == NULL)
483 goto cleanup;
484
Brett Cannondb734912008-06-27 00:52:15 +0000485 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000486 rc = already_warned(registry, key, 0);
487 if (rc == -1)
488 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000490 goto return_none;
491 /* Else this warning hasn't been generated before. */
492 }
493
494 action = get_filter(category, text, lineno, module, &item);
495 if (action == NULL)
496 goto cleanup;
497
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200498 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000499 PyErr_SetObject(category, message);
500 goto cleanup;
501 }
502
503 /* Store in the registry that we've been here, *except* when the action
504 is "always". */
505 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200506 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000507 if (registry != NULL && registry != Py_None &&
508 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000509 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200510 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000511 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200512 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000513 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000514 registry = get_once_registry();
515 if (registry == NULL)
516 goto cleanup;
517 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600518 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000520 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200521 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000522 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000523 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000525 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200526 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000527 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100528 "Unrecognized action (%R) in warnings.filters:\n %R",
529 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000530 goto cleanup;
531 }
532 }
533
Christian Heimes1a8501c2008-10-02 19:56:01 +0000534 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000535 goto return_none;
536 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100537 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100538 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100539 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000540 }
541 else /* if (rc == -1) */
542 goto cleanup;
543
544 return_none:
545 result = Py_None;
546 Py_INCREF(result);
547
548 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400549 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000550 Py_XDECREF(key);
551 Py_XDECREF(text);
552 Py_XDECREF(lineno_obj);
553 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000554 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000555 return result; /* Py_None or NULL. */
556}
557
Larry Hastings714e4932015-09-06 00:39:37 -0700558static int
559is_internal_frame(PyFrameObject *frame)
560{
561 static PyObject *importlib_string = NULL;
562 static PyObject *bootstrap_string = NULL;
563 PyObject *filename;
564 int contains;
565
566 if (importlib_string == NULL) {
567 importlib_string = PyUnicode_FromString("importlib");
568 if (importlib_string == NULL) {
569 return 0;
570 }
571
572 bootstrap_string = PyUnicode_FromString("_bootstrap");
573 if (bootstrap_string == NULL) {
574 Py_DECREF(importlib_string);
575 return 0;
576 }
577 Py_INCREF(importlib_string);
578 Py_INCREF(bootstrap_string);
579 }
580
581 if (frame == NULL || frame->f_code == NULL ||
582 frame->f_code->co_filename == NULL) {
583 return 0;
584 }
585 filename = frame->f_code->co_filename;
586 if (!PyUnicode_Check(filename)) {
587 return 0;
588 }
589 contains = PyUnicode_Contains(filename, importlib_string);
590 if (contains < 0) {
591 return 0;
592 }
593 else if (contains > 0) {
594 contains = PyUnicode_Contains(filename, bootstrap_string);
595 if (contains < 0) {
596 return 0;
597 }
598 else if (contains > 0) {
599 return 1;
600 }
601 }
602
603 return 0;
604}
605
606static PyFrameObject *
607next_external_frame(PyFrameObject *frame)
608{
609 do {
610 frame = frame->f_back;
611 } while (frame != NULL && is_internal_frame(frame));
612
613 return frame;
614}
615
Christian Heimes33fe8092008-04-13 13:53:33 +0000616/* filename, module, and registry are new refs, globals is borrowed */
617/* Returns 0 on error (no new refs), 1 on success */
618static int
619setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
620 PyObject **module, PyObject **registry)
621{
622 PyObject *globals;
623
624 /* Setup globals and lineno. */
625 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700626 // Stack level comparisons to Python code is off by one as there is no
627 // warnings-related stack level to avoid.
628 if (stack_level <= 0 || is_internal_frame(f)) {
629 while (--stack_level > 0 && f != NULL) {
630 f = f->f_back;
631 }
632 }
633 else {
634 while (--stack_level > 0 && f != NULL) {
635 f = next_external_frame(f);
636 }
637 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000638
639 if (f == NULL) {
640 globals = PyThreadState_Get()->interp->sysdict;
641 *lineno = 1;
642 }
643 else {
644 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000645 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000646 }
647
648 *module = NULL;
649
650 /* Setup registry. */
651 assert(globals != NULL);
652 assert(PyDict_Check(globals));
653 *registry = PyDict_GetItemString(globals, "__warningregistry__");
654 if (*registry == NULL) {
655 int rc;
656
657 *registry = PyDict_New();
658 if (*registry == NULL)
659 return 0;
660
661 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
662 if (rc < 0)
663 goto handle_error;
664 }
665 else
666 Py_INCREF(*registry);
667
668 /* Setup module. */
669 *module = PyDict_GetItemString(globals, "__name__");
670 if (*module == NULL) {
671 *module = PyUnicode_FromString("<string>");
672 if (*module == NULL)
673 goto handle_error;
674 }
675 else
676 Py_INCREF(*module);
677
678 /* Setup filename. */
679 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200680 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200681 Py_ssize_t len;
682 int kind;
683 void *data;
684
685 if (PyUnicode_READY(*filename))
686 goto handle_error;
687
Victor Stinner9e30aa52011-11-21 02:49:52 +0100688 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200689 kind = PyUnicode_KIND(*filename);
690 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000691
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500692#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400693 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000694 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200695 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500696 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
697 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400698 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000699 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200700 *filename = PyUnicode_Substring(*filename, 0,
701 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000702 if (*filename == NULL)
703 goto handle_error;
704 }
705 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000706 Py_INCREF(*filename);
707 }
708 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500709 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200710 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100711 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100712 /* PyList_Check() is needed because sys.argv is set to None during
713 Python finalization */
714 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000715 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000716 *filename = PyList_GetItem(argv, 0);
717 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000718 /* If sys.argv[0] is false, then use '__main__'. */
719 is_true = PyObject_IsTrue(*filename);
720 if (is_true < 0) {
721 Py_DECREF(*filename);
722 goto handle_error;
723 }
724 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300725 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000726 if (*filename == NULL)
727 goto handle_error;
728 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000729 }
730 else {
731 /* embedded interpreters don't have sys.argv, see bug #839151 */
732 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100733 if (*filename == NULL)
734 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000735 }
736 }
737 if (*filename == NULL) {
738 *filename = *module;
739 Py_INCREF(*filename);
740 }
741 }
742
743 return 1;
744
745 handle_error:
746 /* filename not XDECREF'ed here as there is no way to jump here with a
747 dangling reference. */
748 Py_XDECREF(*registry);
749 Py_XDECREF(*module);
750 return 0;
751}
752
753static PyObject *
754get_category(PyObject *message, PyObject *category)
755{
756 int rc;
757
758 /* Get category. */
759 rc = PyObject_IsInstance(message, PyExc_Warning);
760 if (rc == -1)
761 return NULL;
762
763 if (rc == 1)
764 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300765 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000766 category = PyExc_UserWarning;
767
768 /* Validate category. */
769 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300770 /* category is not a subclass of PyExc_Warning or
771 PyObject_IsSubclass raised an error */
772 if (rc == -1 || rc == 0) {
773 PyErr_Format(PyExc_TypeError,
774 "category must be a Warning subclass, not '%s'",
775 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000776 return NULL;
777 }
778
779 return category;
780}
781
782static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100783do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
784 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000785{
786 PyObject *filename, *module, *registry, *res;
787 int lineno;
788
789 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
790 return NULL;
791
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100792 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100793 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000794 Py_DECREF(filename);
795 Py_DECREF(registry);
796 Py_DECREF(module);
797 return res;
798}
799
Victor Stinner22f18752016-12-09 18:08:18 +0100800/*[clinic input]
801warn as warnings_warn
802
803 message: object
804 category: object = None
805 stacklevel: Py_ssize_t = 1
806 source: object = None
807
808Issue a warning, or maybe ignore it or raise an exception.
809[clinic start generated code]*/
810
Christian Heimes33fe8092008-04-13 13:53:33 +0000811static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100812warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
813 Py_ssize_t stacklevel, PyObject *source)
814/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000815{
Christian Heimes33fe8092008-04-13 13:53:33 +0000816 category = get_category(message, category);
817 if (category == NULL)
818 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100819 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000820}
821
822static PyObject *
823warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
824{
825 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100826 "module", "registry", "module_globals",
827 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000828 PyObject *message;
829 PyObject *category;
830 PyObject *filename;
831 int lineno;
832 PyObject *module = NULL;
833 PyObject *registry = NULL;
834 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100835 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000836
Victor Stinner914cde82016-03-19 01:03:51 +0100837 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000838 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100839 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000840 return NULL;
841
842 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200843 _Py_IDENTIFIER(get_source);
844 _Py_IDENTIFIER(splitlines);
845 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000846 PyObject *loader;
847 PyObject *module_name;
848 PyObject *source;
849 PyObject *source_list;
850 PyObject *source_line;
851 PyObject *returned;
852
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200853 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
854 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200855 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
856 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000857
858 /* Check/get the requisite pieces needed for the loader. */
859 loader = PyDict_GetItemString(module_globals, "__loader__");
860 module_name = PyDict_GetItemString(module_globals, "__name__");
861
862 if (loader == NULL || module_name == NULL)
863 goto standard_call;
864
865 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200866 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000867 goto standard_call;
868 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200869 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
870 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000871 if (!source)
872 return NULL;
873 else if (source == Py_None) {
874 Py_DECREF(Py_None);
875 goto standard_call;
876 }
877
878 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100879 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200880 PyId_splitlines.object,
881 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000882 Py_DECREF(source);
883 if (!source_list)
884 return NULL;
885
886 /* Get the source line. */
887 source_line = PyList_GetItem(source_list, lineno-1);
888 if (!source_line) {
889 Py_DECREF(source_list);
890 return NULL;
891 }
892
893 /* Handle the warning. */
894 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100895 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000896 Py_DECREF(source_list);
897 return returned;
898 }
899
900 standard_call:
901 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100902 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000903}
904
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200905static PyObject *
906warnings_filters_mutated(PyObject *self, PyObject *args)
907{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600908 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200909 Py_RETURN_NONE;
910}
911
Christian Heimes33fe8092008-04-13 13:53:33 +0000912
913/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000914
915static int
916warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100917 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000918{
919 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000920
921 if (category == NULL)
922 category = PyExc_RuntimeWarning;
923
Victor Stinner914cde82016-03-19 01:03:51 +0100924 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000925 if (res == NULL)
926 return -1;
927 Py_DECREF(res);
928
929 return 0;
930}
931
Victor Stinner914cde82016-03-19 01:03:51 +0100932static int
933_PyErr_WarnFormatV(PyObject *source,
934 PyObject *category, Py_ssize_t stack_level,
935 const char *format, va_list vargs)
936{
937 PyObject *message;
938 int res;
939
940 message = PyUnicode_FromFormatV(format, vargs);
941 if (message == NULL)
942 return -1;
943
944 res = warn_unicode(category, message, stack_level, source);
945 Py_DECREF(message);
946 return res;
947}
948
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000949int
950PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
951 const char *format, ...)
952{
Victor Stinner914cde82016-03-19 01:03:51 +0100953 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000954 va_list vargs;
955
956#ifdef HAVE_STDARG_PROTOTYPES
957 va_start(vargs, format);
958#else
959 va_start(vargs);
960#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100961 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000962 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100963 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000964}
965
966int
Victor Stinner914cde82016-03-19 01:03:51 +0100967PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
968 const char *format, ...)
969{
970 int res;
971 va_list vargs;
972
973#ifdef HAVE_STDARG_PROTOTYPES
974 va_start(vargs, format);
975#else
976 va_start(vargs);
977#endif
978 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
979 stack_level, format, vargs);
980 va_end(vargs);
981 return res;
982}
983
984
985int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000986PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
987{
988 int ret;
989 PyObject *message = PyUnicode_FromString(text);
990 if (message == NULL)
991 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +0100992 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000993 Py_DECREF(message);
994 return ret;
995}
996
Ezio Melotti42da6632011-03-15 05:18:48 +0200997/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +0000998 Use PyErr_WarnEx instead. */
999
1000#undef PyErr_Warn
1001
1002PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001003PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001004{
1005 return PyErr_WarnEx(category, text, 1);
1006}
1007
1008/* Warning with explicit origin */
1009int
Victor Stinner14e461d2013-08-26 22:28:21 +02001010PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1011 PyObject *filename, int lineno,
1012 PyObject *module, PyObject *registry)
1013{
1014 PyObject *res;
1015 if (category == NULL)
1016 category = PyExc_RuntimeWarning;
1017 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001018 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001019 if (res == NULL)
1020 return -1;
1021 Py_DECREF(res);
1022 return 0;
1023}
1024
1025int
Christian Heimes33fe8092008-04-13 13:53:33 +00001026PyErr_WarnExplicit(PyObject *category, const char *text,
1027 const char *filename_str, int lineno,
1028 const char *module_str, PyObject *registry)
1029{
Christian Heimes33fe8092008-04-13 13:53:33 +00001030 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001031 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001032 PyObject *module = NULL;
1033 int ret = -1;
1034
1035 if (message == NULL || filename == NULL)
1036 goto exit;
1037 if (module_str != NULL) {
1038 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001039 if (module == NULL)
1040 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001041 }
1042
Victor Stinner14e461d2013-08-26 22:28:21 +02001043 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1044 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001045
1046 exit:
1047 Py_XDECREF(message);
1048 Py_XDECREF(module);
1049 Py_XDECREF(filename);
1050 return ret;
1051}
1052
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001053int
1054PyErr_WarnExplicitFormat(PyObject *category,
1055 const char *filename_str, int lineno,
1056 const char *module_str, PyObject *registry,
1057 const char *format, ...)
1058{
1059 PyObject *message;
1060 PyObject *module = NULL;
1061 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1062 int ret = -1;
1063 va_list vargs;
1064
1065 if (filename == NULL)
1066 goto exit;
1067 if (module_str != NULL) {
1068 module = PyUnicode_FromString(module_str);
1069 if (module == NULL)
1070 goto exit;
1071 }
1072
1073#ifdef HAVE_STDARG_PROTOTYPES
1074 va_start(vargs, format);
1075#else
1076 va_start(vargs);
1077#endif
1078 message = PyUnicode_FromFormatV(format, vargs);
1079 if (message != NULL) {
1080 PyObject *res;
1081 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001082 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001083 Py_DECREF(message);
1084 if (res != NULL) {
1085 Py_DECREF(res);
1086 ret = 0;
1087 }
1088 }
1089 va_end(vargs);
1090exit:
1091 Py_XDECREF(module);
1092 Py_XDECREF(filename);
1093 return ret;
1094}
1095
Christian Heimes33fe8092008-04-13 13:53:33 +00001096
Christian Heimes33fe8092008-04-13 13:53:33 +00001097PyDoc_STRVAR(warn_explicit_doc,
1098"Low-level inferface to warnings functionality.");
1099
1100static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001101 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001102 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1103 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001104 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1105 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001106 /* XXX(brett.cannon): add showwarning? */
1107 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001109};
1110
1111
1112static PyObject *
1113create_filter(PyObject *category, const char *action)
1114{
1115 static PyObject *ignore_str = NULL;
1116 static PyObject *error_str = NULL;
1117 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001118 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001119 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001120
1121 if (!strcmp(action, "ignore")) {
1122 if (ignore_str == NULL) {
1123 ignore_str = PyUnicode_InternFromString("ignore");
1124 if (ignore_str == NULL)
1125 return NULL;
1126 }
1127 action_obj = ignore_str;
1128 }
1129 else if (!strcmp(action, "error")) {
1130 if (error_str == NULL) {
1131 error_str = PyUnicode_InternFromString("error");
1132 if (error_str == NULL)
1133 return NULL;
1134 }
1135 action_obj = error_str;
1136 }
1137 else if (!strcmp(action, "default")) {
1138 if (default_str == NULL) {
1139 default_str = PyUnicode_InternFromString("default");
1140 if (default_str == NULL)
1141 return NULL;
1142 }
1143 action_obj = default_str;
1144 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001145 else if (!strcmp(action, "always")) {
1146 if (always_str == NULL) {
1147 always_str = PyUnicode_InternFromString("always");
1148 if (always_str == NULL)
1149 return NULL;
1150 }
1151 action_obj = always_str;
1152 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001153 else {
1154 Py_FatalError("unknown action");
1155 }
1156
1157 /* This assumes the line number is zero for now. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001158 return PyTuple_Pack(5, action_obj, Py_None,
1159 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001160}
1161
1162static PyObject *
1163init_filters(void)
1164{
Georg Brandl08be72d2010-10-24 15:11:22 +00001165 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001166 unsigned int pos = 0; /* Post-incremented in each use. */
1167 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001168 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001169
Christian Heimes33fe8092008-04-13 13:53:33 +00001170 if (filters == NULL)
1171 return NULL;
1172
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001173 PyList_SET_ITEM(filters, pos++,
1174 create_filter(PyExc_DeprecationWarning, "ignore"));
1175 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001176 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001177 PyList_SET_ITEM(filters, pos++,
1178 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001179 if (Py_BytesWarningFlag > 1)
1180 bytes_action = "error";
1181 else if (Py_BytesWarningFlag)
1182 bytes_action = "default";
1183 else
1184 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001185 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001186 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001187 /* resource usage warnings are enabled by default in pydebug mode */
1188#ifdef Py_DEBUG
1189 resource_action = "always";
1190#else
1191 resource_action = "ignore";
1192#endif
1193 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1194 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001195 for (x = 0; x < pos; x += 1) {
1196 if (PyList_GET_ITEM(filters, x) == NULL) {
1197 Py_DECREF(filters);
1198 return NULL;
1199 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001200 }
1201
1202 return filters;
1203}
1204
Martin v. Löwis1a214512008-06-11 05:26:20 +00001205static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 PyModuleDef_HEAD_INIT,
1207 MODULE_NAME,
1208 warnings__doc__,
1209 0,
1210 warnings_functions,
1211 NULL,
1212 NULL,
1213 NULL,
1214 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001215};
1216
Christian Heimes33fe8092008-04-13 13:53:33 +00001217
1218PyMODINIT_FUNC
1219_PyWarnings_Init(void)
1220{
Brett Cannon0759dd62009-04-01 18:13:07 +00001221 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001222
Martin v. Löwis1a214512008-06-11 05:26:20 +00001223 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001224 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001225 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001226
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001227 if (_PyRuntime.warnings.filters == NULL) {
1228 _PyRuntime.warnings.filters = init_filters();
1229 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001230 return NULL;
1231 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001232 Py_INCREF(_PyRuntime.warnings.filters);
1233 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001234 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001235
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001236 if (_PyRuntime.warnings.once_registry == NULL) {
1237 _PyRuntime.warnings.once_registry = PyDict_New();
1238 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001239 return NULL;
1240 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001241 Py_INCREF(_PyRuntime.warnings.once_registry);
1242 if (PyModule_AddObject(m, "_onceregistry",
1243 _PyRuntime.warnings.once_registry) < 0)
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.default_action == NULL) {
1247 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1248 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001249 return NULL;
1250 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001251 Py_INCREF(_PyRuntime.warnings.default_action);
1252 if (PyModule_AddObject(m, "_defaultaction",
1253 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001254 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001255
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001256 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001257 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001258}