blob: a5e42a31dc4a38e336e80ad78a509352fe0d9b58 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
2#include "frameobject.h"
Victor Stinner22f18752016-12-09 18:08:18 +01003#include "clinic/_warnings.c.h"
Christian Heimes33fe8092008-04-13 13:53:33 +00004
5#define MODULE_NAME "_warnings"
Christian Heimes33fe8092008-04-13 13:53:33 +00006
7PyDoc_STRVAR(warnings__doc__,
8MODULE_NAME " provides basic warning filtering support.\n"
9"It is a helper module to speed up interpreter start-up.");
10
Victor Stinnerbd303c12013-11-07 23:07:29 +010011_Py_IDENTIFIER(argv);
12_Py_IDENTIFIER(stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +000013
14static int
15check_matched(PyObject *obj, PyObject *arg)
16{
17 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020018 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000019 int rc;
20
21 if (obj == Py_None)
22 return 1;
Victor Stinner55ba38a2016-12-09 16:09:30 +010023 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000024 if (result == NULL)
25 return -1;
26
27 rc = PyObject_IsTrue(result);
28 Py_DECREF(result);
29 return rc;
30}
31
32/*
33 Returns a new reference.
34 A NULL return value can mean false or an error.
35*/
36static PyObject *
Victor Stinnere98445a2016-03-23 00:54:48 +010037get_warnings_attr(const char *attr, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000038{
39 static PyObject *warnings_str = NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010040 PyObject *warnings_module, *obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000041
42 if (warnings_str == NULL) {
43 warnings_str = PyUnicode_InternFromString("warnings");
44 if (warnings_str == NULL)
45 return NULL;
46 }
47
Victor Stinnere98445a2016-03-23 00:54:48 +010048 /* don't try to import after the start of the Python finallization */
Eric Snow76d5abc2017-09-05 18:26:16 -070049 if (try_import && !_Py_IS_FINALIZING()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010050 warnings_module = PyImport_Import(warnings_str);
51 if (warnings_module == NULL) {
52 /* Fallback to the C implementation if we cannot get
53 the Python implementation */
54 PyErr_Clear();
Christian Heimes33fe8092008-04-13 13:53:33 +000055 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010056 }
57 }
58 else {
Eric Snow86b7afd2017-09-04 17:54:09 -060059 warnings_module = _PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010060 if (warnings_module == NULL)
61 return NULL;
62
Victor Stinnere98445a2016-03-23 00:54:48 +010063 Py_INCREF(warnings_module);
64 }
65
66 if (!PyObject_HasAttrString(warnings_module, attr)) {
67 Py_DECREF(warnings_module);
68 return NULL;
69 }
70
71 obj = PyObject_GetAttrString(warnings_module, attr);
72 Py_DECREF(warnings_module);
73 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000074}
75
76
Neal Norwitz32dde222008-04-15 06:43:13 +000077static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000078get_once_registry(void)
79{
80 PyObject *registry;
81
Victor Stinnere98445a2016-03-23 00:54:48 +010082 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000083 if (registry == NULL) {
84 if (PyErr_Occurred())
85 return NULL;
Eric Snow76d5abc2017-09-05 18:26:16 -070086 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000087 }
Eric Snow76d5abc2017-09-05 18:26:16 -070088 Py_DECREF(_PyRuntime.warnings.once_registry);
89 _PyRuntime.warnings.once_registry = registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000090 return registry;
91}
92
93
Brett Cannon0759dd62009-04-01 18:13:07 +000094static PyObject *
95get_default_action(void)
96{
97 PyObject *default_action;
98
Victor Stinnere98445a2016-03-23 00:54:48 +010099 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000100 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (PyErr_Occurred()) {
102 return NULL;
103 }
Eric Snow76d5abc2017-09-05 18:26:16 -0700104 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000105 }
106
Eric Snow76d5abc2017-09-05 18:26:16 -0700107 Py_DECREF(_PyRuntime.warnings.default_action);
108 _PyRuntime.warnings.default_action = default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000109 return default_action;
110}
111
112
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400113/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100114static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000115get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
116 PyObject *module, PyObject **item)
117{
Brett Cannon0759dd62009-04-01 18:13:07 +0000118 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000119 Py_ssize_t i;
120 PyObject *warnings_filters;
121
Victor Stinnere98445a2016-03-23 00:54:48 +0100122 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000123 if (warnings_filters == NULL) {
124 if (PyErr_Occurred())
125 return NULL;
126 }
127 else {
Eric Snow76d5abc2017-09-05 18:26:16 -0700128 Py_DECREF(_PyRuntime.warnings.filters);
129 _PyRuntime.warnings.filters = warnings_filters;
Christian Heimes33fe8092008-04-13 13:53:33 +0000130 }
131
Eric Snow76d5abc2017-09-05 18:26:16 -0700132 PyObject *filters = _PyRuntime.warnings.filters;
133 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000134 PyErr_SetString(PyExc_ValueError,
135 MODULE_NAME ".filters must be a list");
136 return NULL;
137 }
138
Eric Snow76d5abc2017-09-05 18:26:16 -0700139 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
140 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000141 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
142 Py_ssize_t ln;
143 int is_subclass, good_msg, good_mod;
144
Eric Snow76d5abc2017-09-05 18:26:16 -0700145 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400146 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000147 PyErr_Format(PyExc_ValueError,
148 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
149 return NULL;
150 }
151
152 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400153 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000154 action = PyTuple_GET_ITEM(tmp_item, 0);
155 msg = PyTuple_GET_ITEM(tmp_item, 1);
156 cat = PyTuple_GET_ITEM(tmp_item, 2);
157 mod = PyTuple_GET_ITEM(tmp_item, 3);
158 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
159
160 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400161 if (good_msg == -1) {
162 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100163 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400164 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100165
Christian Heimes33fe8092008-04-13 13:53:33 +0000166 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400167 if (good_mod == -1) {
168 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100169 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400170 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100171
Christian Heimes33fe8092008-04-13 13:53:33 +0000172 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400173 if (is_subclass == -1) {
174 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100175 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400176 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100177
Christian Heimes33fe8092008-04-13 13:53:33 +0000178 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400179 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400180 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000181 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400182 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000183
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400184 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
185 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100186 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400187 }
188
189 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000190 }
191
Brett Cannon0759dd62009-04-01 18:13:07 +0000192 action = get_default_action();
193 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400194 Py_INCREF(Py_None);
195 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100196 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000197 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000198
199 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000200 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000201 return NULL;
202}
203
Brett Cannon0759dd62009-04-01 18:13:07 +0000204
Christian Heimes33fe8092008-04-13 13:53:33 +0000205static int
206already_warned(PyObject *registry, PyObject *key, int should_set)
207{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200208 PyObject *version_obj, *already_warned;
209 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000210
211 if (key == NULL)
212 return -1;
213
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200214 version_obj = _PyDict_GetItemId(registry, &PyId_version);
215 if (version_obj == NULL
216 || !PyLong_CheckExact(version_obj)
Eric Snow76d5abc2017-09-05 18:26:16 -0700217 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200218 PyDict_Clear(registry);
Eric Snow76d5abc2017-09-05 18:26:16 -0700219 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200220 if (version_obj == NULL)
221 return -1;
222 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
223 Py_DECREF(version_obj);
224 return -1;
225 }
226 Py_DECREF(version_obj);
227 }
228 else {
229 already_warned = PyDict_GetItem(registry, key);
230 if (already_warned != NULL) {
231 int rc = PyObject_IsTrue(already_warned);
232 if (rc != 0)
233 return rc;
234 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000235 }
236
237 /* This warning wasn't found in the registry, set it. */
238 if (should_set)
239 return PyDict_SetItem(registry, key, Py_True);
240 return 0;
241}
242
243/* New reference. */
244static PyObject *
245normalize_module(PyObject *filename)
246{
247 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100248 int kind;
249 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000250 Py_ssize_t len;
251
Victor Stinner9e30aa52011-11-21 02:49:52 +0100252 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000253 if (len < 0)
254 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100255
256 if (len == 0)
257 return PyUnicode_FromString("<unknown>");
258
259 kind = PyUnicode_KIND(filename);
260 data = PyUnicode_DATA(filename);
261
262 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000263 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100264 PyUnicode_READ(kind, data, len-3) == '.' &&
265 PyUnicode_READ(kind, data, len-2) == 'p' &&
266 PyUnicode_READ(kind, data, len-1) == 'y')
267 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100268 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000269 }
270 else {
271 module = filename;
272 Py_INCREF(module);
273 }
274 return module;
275}
276
277static int
278update_registry(PyObject *registry, PyObject *text, PyObject *category,
279 int add_zero)
280{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300281 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000282 int rc;
283
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300284 if (add_zero)
285 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000286 else
287 altkey = PyTuple_Pack(2, text, category);
288
289 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000290 Py_XDECREF(altkey);
291 return rc;
292}
293
294static void
Victor Stinner914cde82016-03-19 01:03:51 +0100295show_warning(PyObject *filename, int lineno, PyObject *text,
296 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PyObject *f_stderr;
299 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000300 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200301 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000302
303 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
304
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200305 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000306 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100307 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000308
Victor Stinnerbd303c12013-11-07 23:07:29 +0100309 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000310 if (f_stderr == NULL) {
311 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100312 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000313 }
314
315 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100316 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
317 goto error;
318 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
319 goto error;
320 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
321 goto error;
322 if (PyFile_WriteString(": ", f_stderr) < 0)
323 goto error;
324 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
325 goto error;
326 if (PyFile_WriteString("\n", f_stderr) < 0)
327 goto error;
328 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000329
330 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000331 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100332 int kind;
333 void *data;
334 Py_ssize_t i, len;
335 Py_UCS4 ch;
336 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000337
Victor Stinnera4c704b2013-10-29 23:43:41 +0100338 if (PyUnicode_READY(sourceline) < 1)
339 goto error;
340
341 kind = PyUnicode_KIND(sourceline);
342 data = PyUnicode_DATA(sourceline);
343 len = PyUnicode_GET_LENGTH(sourceline);
344 for (i=0; i<len; i++) {
345 ch = PyUnicode_READ(kind, data, i);
346 if (ch != ' ' && ch != '\t' && ch != '\014')
347 break;
348 }
349
350 truncated = PyUnicode_Substring(sourceline, i, len);
351 if (truncated == NULL)
352 goto error;
353
354 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
355 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000356 PyFile_WriteString("\n", f_stderr);
357 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200358 else {
359 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
360 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100361
362error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100363 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000364 PyErr_Clear();
365}
366
Victor Stinner1231a462016-03-19 00:47:17 +0100367static int
368call_show_warning(PyObject *category, PyObject *text, PyObject *message,
369 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100370 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100371{
372 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
373
Victor Stinnere98445a2016-03-23 00:54:48 +0100374 /* If the source parameter is set, try to get the Python implementation.
375 The Python implementation is able to log the traceback where the source
376 was allocated, whereas the C implementation doesnt. */
377 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100378 if (show_fn == NULL) {
379 if (PyErr_Occurred())
380 return -1;
381 show_warning(filename, lineno, text, category, sourceline);
382 return 0;
383 }
384
385 if (!PyCallable_Check(show_fn)) {
386 PyErr_SetString(PyExc_TypeError,
387 "warnings._showwarnmsg() must be set to a callable");
388 goto error;
389 }
390
Victor Stinnere98445a2016-03-23 00:54:48 +0100391 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100392 if (warnmsg_cls == NULL) {
393 PyErr_SetString(PyExc_RuntimeError,
394 "unable to get warnings.WarningMessage");
395 goto error;
396 }
397
398 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100399 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100400 NULL);
401 Py_DECREF(warnmsg_cls);
402 if (msg == NULL)
403 goto error;
404
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100405 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100406 Py_DECREF(show_fn);
407 Py_DECREF(msg);
408
409 if (res == NULL)
410 return -1;
411
412 Py_DECREF(res);
413 return 0;
414
415error:
416 Py_XDECREF(show_fn);
417 return -1;
418}
419
Christian Heimes33fe8092008-04-13 13:53:33 +0000420static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000422 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100423 PyObject *module, PyObject *registry, PyObject *sourceline,
424 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000425{
426 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400427 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100428 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000429 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100431 /* module can be None if a warning is emitted late during Python shutdown.
432 In this case, the Python warnings module was probably unloaded, filters
433 are no more available to choose as action. It is safer to ignore the
434 warning and do nothing. */
435 if (module == Py_None)
436 Py_RETURN_NONE;
437
Brett Cannondb734912008-06-27 00:52:15 +0000438 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
439 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
440 return NULL;
441 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000442
443 /* Normalize module. */
444 if (module == NULL) {
445 module = normalize_module(filename);
446 if (module == NULL)
447 return NULL;
448 }
449 else
450 Py_INCREF(module);
451
452 /* Normalize message. */
453 Py_INCREF(message); /* DECREF'ed in cleanup. */
454 rc = PyObject_IsInstance(message, PyExc_Warning);
455 if (rc == -1) {
456 goto cleanup;
457 }
458 if (rc == 1) {
459 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000460 if (text == NULL)
461 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000462 category = (PyObject*)message->ob_type;
463 }
464 else {
465 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100466 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000467 if (message == NULL)
468 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000469 }
470
471 lineno_obj = PyLong_FromLong(lineno);
472 if (lineno_obj == NULL)
473 goto cleanup;
474
Victor Stinner22f18752016-12-09 18:08:18 +0100475 if (source == Py_None) {
476 source = NULL;
477 }
478
Christian Heimes33fe8092008-04-13 13:53:33 +0000479 /* Create key. */
480 key = PyTuple_Pack(3, text, category, lineno_obj);
481 if (key == NULL)
482 goto cleanup;
483
Brett Cannondb734912008-06-27 00:52:15 +0000484 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000485 rc = already_warned(registry, key, 0);
486 if (rc == -1)
487 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000489 goto return_none;
490 /* Else this warning hasn't been generated before. */
491 }
492
493 action = get_filter(category, text, lineno, module, &item);
494 if (action == NULL)
495 goto cleanup;
496
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200497 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000498 PyErr_SetObject(category, message);
499 goto cleanup;
500 }
501
502 /* Store in the registry that we've been here, *except* when the action
503 is "always". */
504 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200505 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000506 if (registry != NULL && registry != Py_None &&
507 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000508 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200509 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000510 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200511 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000512 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000513 registry = get_once_registry();
514 if (registry == NULL)
515 goto cleanup;
516 }
Eric Snow76d5abc2017-09-05 18:26:16 -0700517 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000519 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200520 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000521 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000522 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000524 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200525 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000526 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100527 "Unrecognized action (%R) in warnings.filters:\n %R",
528 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000529 goto cleanup;
530 }
531 }
532
Christian Heimes1a8501c2008-10-02 19:56:01 +0000533 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000534 goto return_none;
535 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100536 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100537 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100538 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000539 }
540 else /* if (rc == -1) */
541 goto cleanup;
542
543 return_none:
544 result = Py_None;
545 Py_INCREF(result);
546
547 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400548 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000549 Py_XDECREF(key);
550 Py_XDECREF(text);
551 Py_XDECREF(lineno_obj);
552 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000553 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000554 return result; /* Py_None or NULL. */
555}
556
Larry Hastings714e4932015-09-06 00:39:37 -0700557static int
558is_internal_frame(PyFrameObject *frame)
559{
560 static PyObject *importlib_string = NULL;
561 static PyObject *bootstrap_string = NULL;
562 PyObject *filename;
563 int contains;
564
565 if (importlib_string == NULL) {
566 importlib_string = PyUnicode_FromString("importlib");
567 if (importlib_string == NULL) {
568 return 0;
569 }
570
571 bootstrap_string = PyUnicode_FromString("_bootstrap");
572 if (bootstrap_string == NULL) {
573 Py_DECREF(importlib_string);
574 return 0;
575 }
576 Py_INCREF(importlib_string);
577 Py_INCREF(bootstrap_string);
578 }
579
580 if (frame == NULL || frame->f_code == NULL ||
581 frame->f_code->co_filename == NULL) {
582 return 0;
583 }
584 filename = frame->f_code->co_filename;
585 if (!PyUnicode_Check(filename)) {
586 return 0;
587 }
588 contains = PyUnicode_Contains(filename, importlib_string);
589 if (contains < 0) {
590 return 0;
591 }
592 else if (contains > 0) {
593 contains = PyUnicode_Contains(filename, bootstrap_string);
594 if (contains < 0) {
595 return 0;
596 }
597 else if (contains > 0) {
598 return 1;
599 }
600 }
601
602 return 0;
603}
604
605static PyFrameObject *
606next_external_frame(PyFrameObject *frame)
607{
608 do {
609 frame = frame->f_back;
610 } while (frame != NULL && is_internal_frame(frame));
611
612 return frame;
613}
614
Christian Heimes33fe8092008-04-13 13:53:33 +0000615/* filename, module, and registry are new refs, globals is borrowed */
616/* Returns 0 on error (no new refs), 1 on success */
617static int
618setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
619 PyObject **module, PyObject **registry)
620{
621 PyObject *globals;
622
623 /* Setup globals and lineno. */
624 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700625 // Stack level comparisons to Python code is off by one as there is no
626 // warnings-related stack level to avoid.
627 if (stack_level <= 0 || is_internal_frame(f)) {
628 while (--stack_level > 0 && f != NULL) {
629 f = f->f_back;
630 }
631 }
632 else {
633 while (--stack_level > 0 && f != NULL) {
634 f = next_external_frame(f);
635 }
636 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000637
638 if (f == NULL) {
639 globals = PyThreadState_Get()->interp->sysdict;
640 *lineno = 1;
641 }
642 else {
643 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000644 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000645 }
646
647 *module = NULL;
648
649 /* Setup registry. */
650 assert(globals != NULL);
651 assert(PyDict_Check(globals));
652 *registry = PyDict_GetItemString(globals, "__warningregistry__");
653 if (*registry == NULL) {
654 int rc;
655
656 *registry = PyDict_New();
657 if (*registry == NULL)
658 return 0;
659
660 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
661 if (rc < 0)
662 goto handle_error;
663 }
664 else
665 Py_INCREF(*registry);
666
667 /* Setup module. */
668 *module = PyDict_GetItemString(globals, "__name__");
669 if (*module == NULL) {
670 *module = PyUnicode_FromString("<string>");
671 if (*module == NULL)
672 goto handle_error;
673 }
674 else
675 Py_INCREF(*module);
676
677 /* Setup filename. */
678 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200679 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200680 Py_ssize_t len;
681 int kind;
682 void *data;
683
684 if (PyUnicode_READY(*filename))
685 goto handle_error;
686
Victor Stinner9e30aa52011-11-21 02:49:52 +0100687 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200688 kind = PyUnicode_KIND(*filename);
689 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000690
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500691#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400692 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000693 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200694 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500695 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
696 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400697 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000698 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200699 *filename = PyUnicode_Substring(*filename, 0,
700 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000701 if (*filename == NULL)
702 goto handle_error;
703 }
704 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000705 Py_INCREF(*filename);
706 }
707 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500708 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200709 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100710 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100711 /* PyList_Check() is needed because sys.argv is set to None during
712 Python finalization */
713 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000714 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000715 *filename = PyList_GetItem(argv, 0);
716 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000717 /* If sys.argv[0] is false, then use '__main__'. */
718 is_true = PyObject_IsTrue(*filename);
719 if (is_true < 0) {
720 Py_DECREF(*filename);
721 goto handle_error;
722 }
723 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300724 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000725 if (*filename == NULL)
726 goto handle_error;
727 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000728 }
729 else {
730 /* embedded interpreters don't have sys.argv, see bug #839151 */
731 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100732 if (*filename == NULL)
733 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000734 }
735 }
736 if (*filename == NULL) {
737 *filename = *module;
738 Py_INCREF(*filename);
739 }
740 }
741
742 return 1;
743
744 handle_error:
745 /* filename not XDECREF'ed here as there is no way to jump here with a
746 dangling reference. */
747 Py_XDECREF(*registry);
748 Py_XDECREF(*module);
749 return 0;
750}
751
752static PyObject *
753get_category(PyObject *message, PyObject *category)
754{
755 int rc;
756
757 /* Get category. */
758 rc = PyObject_IsInstance(message, PyExc_Warning);
759 if (rc == -1)
760 return NULL;
761
762 if (rc == 1)
763 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300764 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000765 category = PyExc_UserWarning;
766
767 /* Validate category. */
768 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300769 /* category is not a subclass of PyExc_Warning or
770 PyObject_IsSubclass raised an error */
771 if (rc == -1 || rc == 0) {
772 PyErr_Format(PyExc_TypeError,
773 "category must be a Warning subclass, not '%s'",
774 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000775 return NULL;
776 }
777
778 return category;
779}
780
781static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100782do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
783 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000784{
785 PyObject *filename, *module, *registry, *res;
786 int lineno;
787
788 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
789 return NULL;
790
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100791 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100792 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000793 Py_DECREF(filename);
794 Py_DECREF(registry);
795 Py_DECREF(module);
796 return res;
797}
798
Victor Stinner22f18752016-12-09 18:08:18 +0100799/*[clinic input]
800warn as warnings_warn
801
802 message: object
803 category: object = None
804 stacklevel: Py_ssize_t = 1
805 source: object = None
806
807Issue a warning, or maybe ignore it or raise an exception.
808[clinic start generated code]*/
809
Christian Heimes33fe8092008-04-13 13:53:33 +0000810static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100811warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
812 Py_ssize_t stacklevel, PyObject *source)
813/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000814{
Christian Heimes33fe8092008-04-13 13:53:33 +0000815 category = get_category(message, category);
816 if (category == NULL)
817 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100818 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000819}
820
821static PyObject *
822warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
823{
824 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100825 "module", "registry", "module_globals",
826 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000827 PyObject *message;
828 PyObject *category;
829 PyObject *filename;
830 int lineno;
831 PyObject *module = NULL;
832 PyObject *registry = NULL;
833 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100834 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000835
Victor Stinner914cde82016-03-19 01:03:51 +0100836 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000837 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100838 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000839 return NULL;
840
841 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200842 _Py_IDENTIFIER(get_source);
843 _Py_IDENTIFIER(splitlines);
844 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000845 PyObject *loader;
846 PyObject *module_name;
847 PyObject *source;
848 PyObject *source_list;
849 PyObject *source_line;
850 PyObject *returned;
851
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200852 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
853 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200854 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
855 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000856
857 /* Check/get the requisite pieces needed for the loader. */
858 loader = PyDict_GetItemString(module_globals, "__loader__");
859 module_name = PyDict_GetItemString(module_globals, "__name__");
860
861 if (loader == NULL || module_name == NULL)
862 goto standard_call;
863
864 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200865 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000866 goto standard_call;
867 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200868 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
869 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000870 if (!source)
871 return NULL;
872 else if (source == Py_None) {
873 Py_DECREF(Py_None);
874 goto standard_call;
875 }
876
877 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100878 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200879 PyId_splitlines.object,
880 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000881 Py_DECREF(source);
882 if (!source_list)
883 return NULL;
884
885 /* Get the source line. */
886 source_line = PyList_GetItem(source_list, lineno-1);
887 if (!source_line) {
888 Py_DECREF(source_list);
889 return NULL;
890 }
891
892 /* Handle the warning. */
893 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100894 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000895 Py_DECREF(source_list);
896 return returned;
897 }
898
899 standard_call:
900 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100901 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000902}
903
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200904static PyObject *
905warnings_filters_mutated(PyObject *self, PyObject *args)
906{
Eric Snow76d5abc2017-09-05 18:26:16 -0700907 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200908 Py_RETURN_NONE;
909}
910
Christian Heimes33fe8092008-04-13 13:53:33 +0000911
912/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000913
914static int
915warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100916 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000917{
918 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000919
920 if (category == NULL)
921 category = PyExc_RuntimeWarning;
922
Victor Stinner914cde82016-03-19 01:03:51 +0100923 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000924 if (res == NULL)
925 return -1;
926 Py_DECREF(res);
927
928 return 0;
929}
930
Victor Stinner914cde82016-03-19 01:03:51 +0100931static int
932_PyErr_WarnFormatV(PyObject *source,
933 PyObject *category, Py_ssize_t stack_level,
934 const char *format, va_list vargs)
935{
936 PyObject *message;
937 int res;
938
939 message = PyUnicode_FromFormatV(format, vargs);
940 if (message == NULL)
941 return -1;
942
943 res = warn_unicode(category, message, stack_level, source);
944 Py_DECREF(message);
945 return res;
946}
947
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000948int
949PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
950 const char *format, ...)
951{
Victor Stinner914cde82016-03-19 01:03:51 +0100952 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000953 va_list vargs;
954
955#ifdef HAVE_STDARG_PROTOTYPES
956 va_start(vargs, format);
957#else
958 va_start(vargs);
959#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100960 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000961 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100962 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000963}
964
965int
Victor Stinner914cde82016-03-19 01:03:51 +0100966PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
967 const char *format, ...)
968{
969 int res;
970 va_list vargs;
971
972#ifdef HAVE_STDARG_PROTOTYPES
973 va_start(vargs, format);
974#else
975 va_start(vargs);
976#endif
977 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
978 stack_level, format, vargs);
979 va_end(vargs);
980 return res;
981}
982
983
984int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000985PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
986{
987 int ret;
988 PyObject *message = PyUnicode_FromString(text);
989 if (message == NULL)
990 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +0100991 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000992 Py_DECREF(message);
993 return ret;
994}
995
Ezio Melotti42da6632011-03-15 05:18:48 +0200996/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +0000997 Use PyErr_WarnEx instead. */
998
999#undef PyErr_Warn
1000
1001PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001002PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001003{
1004 return PyErr_WarnEx(category, text, 1);
1005}
1006
1007/* Warning with explicit origin */
1008int
Victor Stinner14e461d2013-08-26 22:28:21 +02001009PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1010 PyObject *filename, int lineno,
1011 PyObject *module, PyObject *registry)
1012{
1013 PyObject *res;
1014 if (category == NULL)
1015 category = PyExc_RuntimeWarning;
1016 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001017 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001018 if (res == NULL)
1019 return -1;
1020 Py_DECREF(res);
1021 return 0;
1022}
1023
1024int
Christian Heimes33fe8092008-04-13 13:53:33 +00001025PyErr_WarnExplicit(PyObject *category, const char *text,
1026 const char *filename_str, int lineno,
1027 const char *module_str, PyObject *registry)
1028{
Christian Heimes33fe8092008-04-13 13:53:33 +00001029 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001030 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001031 PyObject *module = NULL;
1032 int ret = -1;
1033
1034 if (message == NULL || filename == NULL)
1035 goto exit;
1036 if (module_str != NULL) {
1037 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001038 if (module == NULL)
1039 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001040 }
1041
Victor Stinner14e461d2013-08-26 22:28:21 +02001042 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1043 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001044
1045 exit:
1046 Py_XDECREF(message);
1047 Py_XDECREF(module);
1048 Py_XDECREF(filename);
1049 return ret;
1050}
1051
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001052int
1053PyErr_WarnExplicitFormat(PyObject *category,
1054 const char *filename_str, int lineno,
1055 const char *module_str, PyObject *registry,
1056 const char *format, ...)
1057{
1058 PyObject *message;
1059 PyObject *module = NULL;
1060 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1061 int ret = -1;
1062 va_list vargs;
1063
1064 if (filename == NULL)
1065 goto exit;
1066 if (module_str != NULL) {
1067 module = PyUnicode_FromString(module_str);
1068 if (module == NULL)
1069 goto exit;
1070 }
1071
1072#ifdef HAVE_STDARG_PROTOTYPES
1073 va_start(vargs, format);
1074#else
1075 va_start(vargs);
1076#endif
1077 message = PyUnicode_FromFormatV(format, vargs);
1078 if (message != NULL) {
1079 PyObject *res;
1080 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001081 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001082 Py_DECREF(message);
1083 if (res != NULL) {
1084 Py_DECREF(res);
1085 ret = 0;
1086 }
1087 }
1088 va_end(vargs);
1089exit:
1090 Py_XDECREF(module);
1091 Py_XDECREF(filename);
1092 return ret;
1093}
1094
Christian Heimes33fe8092008-04-13 13:53:33 +00001095
Christian Heimes33fe8092008-04-13 13:53:33 +00001096PyDoc_STRVAR(warn_explicit_doc,
1097"Low-level inferface to warnings functionality.");
1098
1099static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001100 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001101 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1102 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001103 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1104 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001105 /* XXX(brett.cannon): add showwarning? */
1106 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001108};
1109
1110
1111static PyObject *
1112create_filter(PyObject *category, const char *action)
1113{
1114 static PyObject *ignore_str = NULL;
1115 static PyObject *error_str = NULL;
1116 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001117 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001118 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001119
1120 if (!strcmp(action, "ignore")) {
1121 if (ignore_str == NULL) {
1122 ignore_str = PyUnicode_InternFromString("ignore");
1123 if (ignore_str == NULL)
1124 return NULL;
1125 }
1126 action_obj = ignore_str;
1127 }
1128 else if (!strcmp(action, "error")) {
1129 if (error_str == NULL) {
1130 error_str = PyUnicode_InternFromString("error");
1131 if (error_str == NULL)
1132 return NULL;
1133 }
1134 action_obj = error_str;
1135 }
1136 else if (!strcmp(action, "default")) {
1137 if (default_str == NULL) {
1138 default_str = PyUnicode_InternFromString("default");
1139 if (default_str == NULL)
1140 return NULL;
1141 }
1142 action_obj = default_str;
1143 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001144 else if (!strcmp(action, "always")) {
1145 if (always_str == NULL) {
1146 always_str = PyUnicode_InternFromString("always");
1147 if (always_str == NULL)
1148 return NULL;
1149 }
1150 action_obj = always_str;
1151 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001152 else {
1153 Py_FatalError("unknown action");
1154 }
1155
1156 /* This assumes the line number is zero for now. */
Eric Snow76d5abc2017-09-05 18:26:16 -07001157 return PyTuple_Pack(5, action_obj, Py_None,
1158 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001159}
1160
1161static PyObject *
1162init_filters(void)
1163{
Georg Brandl08be72d2010-10-24 15:11:22 +00001164 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001165 unsigned int pos = 0; /* Post-incremented in each use. */
1166 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001167 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001168
Christian Heimes33fe8092008-04-13 13:53:33 +00001169 if (filters == NULL)
1170 return NULL;
1171
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001172 PyList_SET_ITEM(filters, pos++,
1173 create_filter(PyExc_DeprecationWarning, "ignore"));
1174 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001175 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001176 PyList_SET_ITEM(filters, pos++,
1177 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001178 if (Py_BytesWarningFlag > 1)
1179 bytes_action = "error";
1180 else if (Py_BytesWarningFlag)
1181 bytes_action = "default";
1182 else
1183 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001184 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001185 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001186 /* resource usage warnings are enabled by default in pydebug mode */
1187#ifdef Py_DEBUG
1188 resource_action = "always";
1189#else
1190 resource_action = "ignore";
1191#endif
1192 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1193 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001194 for (x = 0; x < pos; x += 1) {
1195 if (PyList_GET_ITEM(filters, x) == NULL) {
1196 Py_DECREF(filters);
1197 return NULL;
1198 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001199 }
1200
1201 return filters;
1202}
1203
Martin v. Löwis1a214512008-06-11 05:26:20 +00001204static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 PyModuleDef_HEAD_INIT,
1206 MODULE_NAME,
1207 warnings__doc__,
1208 0,
1209 warnings_functions,
1210 NULL,
1211 NULL,
1212 NULL,
1213 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001214};
1215
Christian Heimes33fe8092008-04-13 13:53:33 +00001216
1217PyMODINIT_FUNC
1218_PyWarnings_Init(void)
1219{
Brett Cannon0759dd62009-04-01 18:13:07 +00001220 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001221
Martin v. Löwis1a214512008-06-11 05:26:20 +00001222 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001223 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001224 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001225
Eric Snow76d5abc2017-09-05 18:26:16 -07001226 if (_PyRuntime.warnings.filters == NULL) {
1227 _PyRuntime.warnings.filters = init_filters();
1228 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001229 return NULL;
1230 }
Eric Snow76d5abc2017-09-05 18:26:16 -07001231 Py_INCREF(_PyRuntime.warnings.filters);
1232 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001233 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001234
Eric Snow76d5abc2017-09-05 18:26:16 -07001235 if (_PyRuntime.warnings.once_registry == NULL) {
1236 _PyRuntime.warnings.once_registry = PyDict_New();
1237 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001238 return NULL;
1239 }
Eric Snow76d5abc2017-09-05 18:26:16 -07001240 Py_INCREF(_PyRuntime.warnings.once_registry);
1241 if (PyModule_AddObject(m, "_onceregistry",
1242 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001243 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001244
Eric Snow76d5abc2017-09-05 18:26:16 -07001245 if (_PyRuntime.warnings.default_action == NULL) {
1246 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1247 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001248 return NULL;
1249 }
Eric Snow76d5abc2017-09-05 18:26:16 -07001250 Py_INCREF(_PyRuntime.warnings.default_action);
1251 if (PyModule_AddObject(m, "_defaultaction",
1252 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001253 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001254
Eric Snow76d5abc2017-09-05 18:26:16 -07001255 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001256 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001257}