blob: 2b04b9081e2c387791e06f6a1621cc1caa4f8d55 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
2#include "frameobject.h"
3
4#define MODULE_NAME "_warnings"
Christian Heimes33fe8092008-04-13 13:53:33 +00005
6PyDoc_STRVAR(warnings__doc__,
7MODULE_NAME " provides basic warning filtering support.\n"
8"It is a helper module to speed up interpreter start-up.");
9
10/* Both 'filters' and 'onceregistry' can be set in warnings.py;
11 get_warnings_attr() will reset these variables accordingly. */
12static PyObject *_filters; /* List */
13static PyObject *_once_registry; /* Dict */
Brett Cannon0759dd62009-04-01 18:13:07 +000014static PyObject *_default_action; /* String */
Antoine Pitroucb0a0062014-09-18 02:40:46 +020015static long _filters_version;
Christian Heimes33fe8092008-04-13 13:53:33 +000016
Victor Stinnerbd303c12013-11-07 23:07:29 +010017_Py_IDENTIFIER(argv);
18_Py_IDENTIFIER(stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +000019
20static int
21check_matched(PyObject *obj, PyObject *arg)
22{
23 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020024 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000025 int rc;
26
27 if (obj == Py_None)
28 return 1;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020029 result = _PyObject_CallMethodId(obj, &PyId_match, "O", arg);
Christian Heimes33fe8092008-04-13 13:53:33 +000030 if (result == NULL)
31 return -1;
32
33 rc = PyObject_IsTrue(result);
34 Py_DECREF(result);
35 return rc;
36}
37
38/*
39 Returns a new reference.
40 A NULL return value can mean false or an error.
41*/
42static PyObject *
Victor Stinnere98445a2016-03-23 00:54:48 +010043get_warnings_attr(const char *attr, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000044{
45 static PyObject *warnings_str = NULL;
46 PyObject *all_modules;
Victor Stinnere98445a2016-03-23 00:54:48 +010047 PyObject *warnings_module, *obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000048
49 if (warnings_str == NULL) {
50 warnings_str = PyUnicode_InternFromString("warnings");
51 if (warnings_str == NULL)
52 return NULL;
53 }
54
Victor Stinnere98445a2016-03-23 00:54:48 +010055 /* don't try to import after the start of the Python finallization */
56 if (try_import && _Py_Finalizing == NULL) {
57 warnings_module = PyImport_Import(warnings_str);
58 if (warnings_module == NULL) {
59 /* Fallback to the C implementation if we cannot get
60 the Python implementation */
61 PyErr_Clear();
Christian Heimes33fe8092008-04-13 13:53:33 +000062 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010063 }
64 }
65 else {
66 all_modules = PyImport_GetModuleDict();
Victor Stinnere98445a2016-03-23 00:54:48 +010067
68 warnings_module = PyDict_GetItem(all_modules, warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010069 if (warnings_module == NULL)
70 return NULL;
71
Victor Stinnere98445a2016-03-23 00:54:48 +010072 Py_INCREF(warnings_module);
73 }
74
75 if (!PyObject_HasAttrString(warnings_module, attr)) {
76 Py_DECREF(warnings_module);
77 return NULL;
78 }
79
80 obj = PyObject_GetAttrString(warnings_module, attr);
81 Py_DECREF(warnings_module);
82 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000083}
84
85
Neal Norwitz32dde222008-04-15 06:43:13 +000086static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000087get_once_registry(void)
88{
89 PyObject *registry;
90
Victor Stinnere98445a2016-03-23 00:54:48 +010091 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000092 if (registry == NULL) {
93 if (PyErr_Occurred())
94 return NULL;
95 return _once_registry;
96 }
Serhiy Storchaka7972ed22017-09-11 10:01:47 +030097 if (!PyDict_Check(registry)) {
98 PyErr_SetString(PyExc_TypeError,
99 "warnings.onceregistry must be a dict");
100 Py_DECREF(registry);
101 return NULL;
102 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000103 Py_DECREF(_once_registry);
104 _once_registry = registry;
105 return registry;
106}
107
108
Brett Cannon0759dd62009-04-01 18:13:07 +0000109static PyObject *
110get_default_action(void)
111{
112 PyObject *default_action;
113
Victor Stinnere98445a2016-03-23 00:54:48 +0100114 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000115 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (PyErr_Occurred()) {
117 return NULL;
118 }
119 return _default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000120 }
121
122 Py_DECREF(_default_action);
123 _default_action = default_action;
124 return default_action;
125}
126
127
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400128/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100129static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000130get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
131 PyObject *module, PyObject **item)
132{
Brett Cannon0759dd62009-04-01 18:13:07 +0000133 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000134 Py_ssize_t i;
135 PyObject *warnings_filters;
136
Victor Stinnere98445a2016-03-23 00:54:48 +0100137 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000138 if (warnings_filters == NULL) {
139 if (PyErr_Occurred())
140 return NULL;
141 }
142 else {
143 Py_DECREF(_filters);
144 _filters = warnings_filters;
145 }
146
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000147 if (_filters == NULL || !PyList_Check(_filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000148 PyErr_SetString(PyExc_ValueError,
149 MODULE_NAME ".filters must be a list");
150 return NULL;
151 }
152
153 /* _filters could change while we are iterating over it. */
154 for (i = 0; i < PyList_GET_SIZE(_filters); i++) {
155 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
156 Py_ssize_t ln;
157 int is_subclass, good_msg, good_mod;
158
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400159 tmp_item = PyList_GET_ITEM(_filters, i);
160 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000161 PyErr_Format(PyExc_ValueError,
162 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
163 return NULL;
164 }
165
166 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400167 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000168 action = PyTuple_GET_ITEM(tmp_item, 0);
169 msg = PyTuple_GET_ITEM(tmp_item, 1);
170 cat = PyTuple_GET_ITEM(tmp_item, 2);
171 mod = PyTuple_GET_ITEM(tmp_item, 3);
172 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
173
174 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400175 if (good_msg == -1) {
176 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100177 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400178 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100179
Christian Heimes33fe8092008-04-13 13:53:33 +0000180 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400181 if (good_mod == -1) {
182 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100183 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400184 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100185
Christian Heimes33fe8092008-04-13 13:53:33 +0000186 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400187 if (is_subclass == -1) {
188 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100189 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400190 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100191
Christian Heimes33fe8092008-04-13 13:53:33 +0000192 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400193 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400194 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000195 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400196 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000197
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400198 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
199 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100200 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400201 }
202
203 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000204 }
205
Brett Cannon0759dd62009-04-01 18:13:07 +0000206 action = get_default_action();
207 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400208 Py_INCREF(Py_None);
209 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100210 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000211 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000212
213 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000214 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000215 return NULL;
216}
217
Brett Cannon0759dd62009-04-01 18:13:07 +0000218
Christian Heimes33fe8092008-04-13 13:53:33 +0000219static int
220already_warned(PyObject *registry, PyObject *key, int should_set)
221{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200222 PyObject *version_obj, *already_warned;
223 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000224
225 if (key == NULL)
226 return -1;
227
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200228 version_obj = _PyDict_GetItemId(registry, &PyId_version);
229 if (version_obj == NULL
230 || !PyLong_CheckExact(version_obj)
231 || PyLong_AsLong(version_obj) != _filters_version) {
232 PyDict_Clear(registry);
233 version_obj = PyLong_FromLong(_filters_version);
234 if (version_obj == NULL)
235 return -1;
236 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
237 Py_DECREF(version_obj);
238 return -1;
239 }
240 Py_DECREF(version_obj);
241 }
242 else {
243 already_warned = PyDict_GetItem(registry, key);
244 if (already_warned != NULL) {
245 int rc = PyObject_IsTrue(already_warned);
246 if (rc != 0)
247 return rc;
248 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000249 }
250
251 /* This warning wasn't found in the registry, set it. */
252 if (should_set)
253 return PyDict_SetItem(registry, key, Py_True);
254 return 0;
255}
256
257/* New reference. */
258static PyObject *
259normalize_module(PyObject *filename)
260{
261 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100262 int kind;
263 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000264 Py_ssize_t len;
265
Victor Stinner9e30aa52011-11-21 02:49:52 +0100266 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000267 if (len < 0)
268 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100269
270 if (len == 0)
271 return PyUnicode_FromString("<unknown>");
272
273 kind = PyUnicode_KIND(filename);
274 data = PyUnicode_DATA(filename);
275
276 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000277 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100278 PyUnicode_READ(kind, data, len-3) == '.' &&
279 PyUnicode_READ(kind, data, len-2) == 'p' &&
280 PyUnicode_READ(kind, data, len-1) == 'y')
281 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100282 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000283 }
284 else {
285 module = filename;
286 Py_INCREF(module);
287 }
288 return module;
289}
290
291static int
292update_registry(PyObject *registry, PyObject *text, PyObject *category,
293 int add_zero)
294{
295 PyObject *altkey, *zero = NULL;
296 int rc;
297
298 if (add_zero) {
299 zero = PyLong_FromLong(0);
300 if (zero == NULL)
301 return -1;
302 altkey = PyTuple_Pack(3, text, category, zero);
303 }
304 else
305 altkey = PyTuple_Pack(2, text, category);
306
307 rc = already_warned(registry, altkey, 1);
308 Py_XDECREF(zero);
309 Py_XDECREF(altkey);
310 return rc;
311}
312
313static void
Victor Stinner914cde82016-03-19 01:03:51 +0100314show_warning(PyObject *filename, int lineno, PyObject *text,
315 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyObject *f_stderr;
318 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000319 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200320 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000321
322 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
323
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200324 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000325 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100326 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000327
Victor Stinnerbd303c12013-11-07 23:07:29 +0100328 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000329 if (f_stderr == NULL) {
330 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100331 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000332 }
333
334 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100335 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
336 goto error;
337 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
338 goto error;
339 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
340 goto error;
341 if (PyFile_WriteString(": ", f_stderr) < 0)
342 goto error;
343 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
344 goto error;
345 if (PyFile_WriteString("\n", f_stderr) < 0)
346 goto error;
347 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000348
349 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000350 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100351 int kind;
352 void *data;
353 Py_ssize_t i, len;
354 Py_UCS4 ch;
355 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000356
Victor Stinnera4c704b2013-10-29 23:43:41 +0100357 if (PyUnicode_READY(sourceline) < 1)
358 goto error;
359
360 kind = PyUnicode_KIND(sourceline);
361 data = PyUnicode_DATA(sourceline);
362 len = PyUnicode_GET_LENGTH(sourceline);
363 for (i=0; i<len; i++) {
364 ch = PyUnicode_READ(kind, data, i);
365 if (ch != ' ' && ch != '\t' && ch != '\014')
366 break;
367 }
368
369 truncated = PyUnicode_Substring(sourceline, i, len);
370 if (truncated == NULL)
371 goto error;
372
373 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
374 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000375 PyFile_WriteString("\n", f_stderr);
376 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200377 else {
378 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
379 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100380
381error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100382 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000383 PyErr_Clear();
384}
385
Victor Stinner1231a462016-03-19 00:47:17 +0100386static int
387call_show_warning(PyObject *category, PyObject *text, PyObject *message,
388 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100389 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100390{
391 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
392
Victor Stinnere98445a2016-03-23 00:54:48 +0100393 /* If the source parameter is set, try to get the Python implementation.
394 The Python implementation is able to log the traceback where the source
395 was allocated, whereas the C implementation doesnt. */
396 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100397 if (show_fn == NULL) {
398 if (PyErr_Occurred())
399 return -1;
400 show_warning(filename, lineno, text, category, sourceline);
401 return 0;
402 }
403
404 if (!PyCallable_Check(show_fn)) {
405 PyErr_SetString(PyExc_TypeError,
406 "warnings._showwarnmsg() must be set to a callable");
407 goto error;
408 }
409
Victor Stinnere98445a2016-03-23 00:54:48 +0100410 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100411 if (warnmsg_cls == NULL) {
412 PyErr_SetString(PyExc_RuntimeError,
413 "unable to get warnings.WarningMessage");
414 goto error;
415 }
416
417 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100418 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100419 NULL);
420 Py_DECREF(warnmsg_cls);
421 if (msg == NULL)
422 goto error;
423
424 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
425 Py_DECREF(show_fn);
426 Py_DECREF(msg);
427
428 if (res == NULL)
429 return -1;
430
431 Py_DECREF(res);
432 return 0;
433
434error:
435 Py_XDECREF(show_fn);
436 return -1;
437}
438
Christian Heimes33fe8092008-04-13 13:53:33 +0000439static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000441 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100442 PyObject *module, PyObject *registry, PyObject *sourceline,
443 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000444{
445 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400446 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100447 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000448 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100450 /* module can be None if a warning is emitted late during Python shutdown.
451 In this case, the Python warnings module was probably unloaded, filters
452 are no more available to choose as action. It is safer to ignore the
453 warning and do nothing. */
454 if (module == Py_None)
455 Py_RETURN_NONE;
456
Brett Cannondb734912008-06-27 00:52:15 +0000457 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Serhiy Storchaka7972ed22017-09-11 10:01:47 +0300458 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000459 return NULL;
460 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000461
462 /* Normalize module. */
463 if (module == NULL) {
464 module = normalize_module(filename);
465 if (module == NULL)
466 return NULL;
467 }
468 else
469 Py_INCREF(module);
470
471 /* Normalize message. */
472 Py_INCREF(message); /* DECREF'ed in cleanup. */
473 rc = PyObject_IsInstance(message, PyExc_Warning);
474 if (rc == -1) {
475 goto cleanup;
476 }
477 if (rc == 1) {
478 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000479 if (text == NULL)
480 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000481 category = (PyObject*)message->ob_type;
482 }
483 else {
484 text = message;
485 message = PyObject_CallFunction(category, "O", message);
Brett Cannondb734912008-06-27 00:52:15 +0000486 if (message == NULL)
487 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000488 }
489
490 lineno_obj = PyLong_FromLong(lineno);
491 if (lineno_obj == NULL)
492 goto cleanup;
493
494 /* Create key. */
495 key = PyTuple_Pack(3, text, category, lineno_obj);
496 if (key == NULL)
497 goto cleanup;
498
Brett Cannondb734912008-06-27 00:52:15 +0000499 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000500 rc = already_warned(registry, key, 0);
501 if (rc == -1)
502 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000504 goto return_none;
505 /* Else this warning hasn't been generated before. */
506 }
507
508 action = get_filter(category, text, lineno, module, &item);
509 if (action == NULL)
510 goto cleanup;
511
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200512 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000513 PyErr_SetObject(category, message);
514 goto cleanup;
515 }
516
517 /* Store in the registry that we've been here, *except* when the action
518 is "always". */
519 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200520 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000521 if (registry != NULL && registry != Py_None &&
522 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000523 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200524 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000525 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200526 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000527 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000528 registry = get_once_registry();
529 if (registry == NULL)
530 goto cleanup;
531 }
532 /* _once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000534 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200535 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000536 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000537 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000539 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200540 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000541 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100542 "Unrecognized action (%R) in warnings.filters:\n %R",
543 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000544 goto cleanup;
545 }
546 }
547
Christian Heimes1a8501c2008-10-02 19:56:01 +0000548 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000549 goto return_none;
550 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100551 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100552 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100553 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000554 }
555 else /* if (rc == -1) */
556 goto cleanup;
557
558 return_none:
559 result = Py_None;
560 Py_INCREF(result);
561
562 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400563 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000564 Py_XDECREF(key);
565 Py_XDECREF(text);
566 Py_XDECREF(lineno_obj);
567 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000568 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000569 return result; /* Py_None or NULL. */
570}
571
Larry Hastings714e4932015-09-06 00:39:37 -0700572static int
573is_internal_frame(PyFrameObject *frame)
574{
575 static PyObject *importlib_string = NULL;
576 static PyObject *bootstrap_string = NULL;
577 PyObject *filename;
578 int contains;
579
580 if (importlib_string == NULL) {
581 importlib_string = PyUnicode_FromString("importlib");
582 if (importlib_string == NULL) {
583 return 0;
584 }
585
586 bootstrap_string = PyUnicode_FromString("_bootstrap");
587 if (bootstrap_string == NULL) {
588 Py_DECREF(importlib_string);
589 return 0;
590 }
591 Py_INCREF(importlib_string);
592 Py_INCREF(bootstrap_string);
593 }
594
595 if (frame == NULL || frame->f_code == NULL ||
596 frame->f_code->co_filename == NULL) {
597 return 0;
598 }
599 filename = frame->f_code->co_filename;
600 if (!PyUnicode_Check(filename)) {
601 return 0;
602 }
603 contains = PyUnicode_Contains(filename, importlib_string);
604 if (contains < 0) {
605 return 0;
606 }
607 else if (contains > 0) {
608 contains = PyUnicode_Contains(filename, bootstrap_string);
609 if (contains < 0) {
610 return 0;
611 }
612 else if (contains > 0) {
613 return 1;
614 }
615 }
616
617 return 0;
618}
619
620static PyFrameObject *
621next_external_frame(PyFrameObject *frame)
622{
623 do {
624 frame = frame->f_back;
625 } while (frame != NULL && is_internal_frame(frame));
626
627 return frame;
628}
629
Christian Heimes33fe8092008-04-13 13:53:33 +0000630/* filename, module, and registry are new refs, globals is borrowed */
631/* Returns 0 on error (no new refs), 1 on success */
632static int
633setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
634 PyObject **module, PyObject **registry)
635{
636 PyObject *globals;
637
638 /* Setup globals and lineno. */
639 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700640 // Stack level comparisons to Python code is off by one as there is no
641 // warnings-related stack level to avoid.
642 if (stack_level <= 0 || is_internal_frame(f)) {
643 while (--stack_level > 0 && f != NULL) {
644 f = f->f_back;
645 }
646 }
647 else {
648 while (--stack_level > 0 && f != NULL) {
649 f = next_external_frame(f);
650 }
651 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000652
653 if (f == NULL) {
654 globals = PyThreadState_Get()->interp->sysdict;
655 *lineno = 1;
656 }
657 else {
658 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000659 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000660 }
661
662 *module = NULL;
663
664 /* Setup registry. */
665 assert(globals != NULL);
666 assert(PyDict_Check(globals));
667 *registry = PyDict_GetItemString(globals, "__warningregistry__");
668 if (*registry == NULL) {
669 int rc;
670
671 *registry = PyDict_New();
672 if (*registry == NULL)
673 return 0;
674
675 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
676 if (rc < 0)
677 goto handle_error;
678 }
679 else
680 Py_INCREF(*registry);
681
682 /* Setup module. */
683 *module = PyDict_GetItemString(globals, "__name__");
684 if (*module == NULL) {
685 *module = PyUnicode_FromString("<string>");
686 if (*module == NULL)
687 goto handle_error;
688 }
689 else
690 Py_INCREF(*module);
691
692 /* Setup filename. */
693 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200694 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200695 Py_ssize_t len;
696 int kind;
697 void *data;
698
699 if (PyUnicode_READY(*filename))
700 goto handle_error;
701
Victor Stinner9e30aa52011-11-21 02:49:52 +0100702 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200703 kind = PyUnicode_KIND(*filename);
704 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000705
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500706#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400707 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000708 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200709 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500710 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
711 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400712 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000713 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200714 *filename = PyUnicode_Substring(*filename, 0,
715 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000716 if (*filename == NULL)
717 goto handle_error;
718 }
719 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000720 Py_INCREF(*filename);
721 }
722 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500723 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200724 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100725 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100726 /* PyList_Check() is needed because sys.argv is set to None during
727 Python finalization */
728 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000729 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000730 *filename = PyList_GetItem(argv, 0);
731 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000732 /* If sys.argv[0] is false, then use '__main__'. */
733 is_true = PyObject_IsTrue(*filename);
734 if (is_true < 0) {
735 Py_DECREF(*filename);
736 goto handle_error;
737 }
738 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300739 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000740 if (*filename == NULL)
741 goto handle_error;
742 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000743 }
744 else {
745 /* embedded interpreters don't have sys.argv, see bug #839151 */
746 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100747 if (*filename == NULL)
748 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000749 }
750 }
751 if (*filename == NULL) {
752 *filename = *module;
753 Py_INCREF(*filename);
754 }
755 }
756
757 return 1;
758
759 handle_error:
760 /* filename not XDECREF'ed here as there is no way to jump here with a
761 dangling reference. */
762 Py_XDECREF(*registry);
763 Py_XDECREF(*module);
764 return 0;
765}
766
767static PyObject *
768get_category(PyObject *message, PyObject *category)
769{
770 int rc;
771
772 /* Get category. */
773 rc = PyObject_IsInstance(message, PyExc_Warning);
774 if (rc == -1)
775 return NULL;
776
777 if (rc == 1)
778 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300779 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000780 category = PyExc_UserWarning;
781
782 /* Validate category. */
783 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300784 /* category is not a subclass of PyExc_Warning or
785 PyObject_IsSubclass raised an error */
786 if (rc == -1 || rc == 0) {
787 PyErr_Format(PyExc_TypeError,
788 "category must be a Warning subclass, not '%s'",
789 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000790 return NULL;
791 }
792
793 return category;
794}
795
796static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100797do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
798 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000799{
800 PyObject *filename, *module, *registry, *res;
801 int lineno;
802
803 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
804 return NULL;
805
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100806 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100807 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000808 Py_DECREF(filename);
809 Py_DECREF(registry);
810 Py_DECREF(module);
811 return res;
812}
813
814static PyObject *
815warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
816{
Victor Stinnere19558a2016-03-23 00:28:08 +0100817 static char *kw_list[] = {"message", "category", "stacklevel",
818 "source", NULL};
819 PyObject *message, *category = NULL, *source = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000820 Py_ssize_t stack_level = 1;
821
Victor Stinnere19558a2016-03-23 00:28:08 +0100822 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OnO:warn", kw_list,
823 &message, &category, &stack_level, &source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000824 return NULL;
825
826 category = get_category(message, category);
827 if (category == NULL)
828 return NULL;
Victor Stinnere19558a2016-03-23 00:28:08 +0100829 return do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000830}
831
832static PyObject *
833warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
834{
835 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100836 "module", "registry", "module_globals",
837 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000838 PyObject *message;
839 PyObject *category;
840 PyObject *filename;
841 int lineno;
842 PyObject *module = NULL;
843 PyObject *registry = NULL;
844 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100845 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000846
Victor Stinner914cde82016-03-19 01:03:51 +0100847 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000848 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100849 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000850 return NULL;
851
852 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200853 _Py_IDENTIFIER(get_source);
854 _Py_IDENTIFIER(splitlines);
855 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000856 PyObject *loader;
857 PyObject *module_name;
858 PyObject *source;
859 PyObject *source_list;
860 PyObject *source_line;
861 PyObject *returned;
862
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200863 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
864 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200865 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
866 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000867
868 /* Check/get the requisite pieces needed for the loader. */
869 loader = PyDict_GetItemString(module_globals, "__loader__");
870 module_name = PyDict_GetItemString(module_globals, "__name__");
871
872 if (loader == NULL || module_name == NULL)
873 goto standard_call;
874
875 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200876 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000877 goto standard_call;
878 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200879 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
880 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000881 if (!source)
882 return NULL;
883 else if (source == Py_None) {
884 Py_DECREF(Py_None);
885 goto standard_call;
886 }
887
888 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100889 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200890 PyId_splitlines.object,
891 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000892 Py_DECREF(source);
893 if (!source_list)
894 return NULL;
895
896 /* Get the source line. */
897 source_line = PyList_GetItem(source_list, lineno-1);
898 if (!source_line) {
899 Py_DECREF(source_list);
900 return NULL;
901 }
902
903 /* Handle the warning. */
904 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100905 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000906 Py_DECREF(source_list);
907 return returned;
908 }
909
910 standard_call:
911 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100912 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000913}
914
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200915static PyObject *
916warnings_filters_mutated(PyObject *self, PyObject *args)
917{
918 _filters_version++;
919 Py_RETURN_NONE;
920}
921
Christian Heimes33fe8092008-04-13 13:53:33 +0000922
923/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000924
925static int
926warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100927 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000928{
929 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000930
931 if (category == NULL)
932 category = PyExc_RuntimeWarning;
933
Victor Stinner914cde82016-03-19 01:03:51 +0100934 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000935 if (res == NULL)
936 return -1;
937 Py_DECREF(res);
938
939 return 0;
940}
941
Victor Stinner914cde82016-03-19 01:03:51 +0100942static int
943_PyErr_WarnFormatV(PyObject *source,
944 PyObject *category, Py_ssize_t stack_level,
945 const char *format, va_list vargs)
946{
947 PyObject *message;
948 int res;
949
950 message = PyUnicode_FromFormatV(format, vargs);
951 if (message == NULL)
952 return -1;
953
954 res = warn_unicode(category, message, stack_level, source);
955 Py_DECREF(message);
956 return res;
957}
958
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000959int
960PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
961 const char *format, ...)
962{
Victor Stinner914cde82016-03-19 01:03:51 +0100963 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000964 va_list vargs;
965
966#ifdef HAVE_STDARG_PROTOTYPES
967 va_start(vargs, format);
968#else
969 va_start(vargs);
970#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100971 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000972 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100973 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000974}
975
976int
Victor Stinner914cde82016-03-19 01:03:51 +0100977PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
978 const char *format, ...)
979{
980 int res;
981 va_list vargs;
982
983#ifdef HAVE_STDARG_PROTOTYPES
984 va_start(vargs, format);
985#else
986 va_start(vargs);
987#endif
988 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
989 stack_level, format, vargs);
990 va_end(vargs);
991 return res;
992}
993
994
995int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000996PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
997{
998 int ret;
999 PyObject *message = PyUnicode_FromString(text);
1000 if (message == NULL)
1001 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001002 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001003 Py_DECREF(message);
1004 return ret;
1005}
1006
Ezio Melotti42da6632011-03-15 05:18:48 +02001007/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001008 Use PyErr_WarnEx instead. */
1009
1010#undef PyErr_Warn
1011
1012PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001013PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001014{
1015 return PyErr_WarnEx(category, text, 1);
1016}
1017
1018/* Warning with explicit origin */
1019int
Victor Stinner14e461d2013-08-26 22:28:21 +02001020PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1021 PyObject *filename, int lineno,
1022 PyObject *module, PyObject *registry)
1023{
1024 PyObject *res;
1025 if (category == NULL)
1026 category = PyExc_RuntimeWarning;
1027 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001028 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001029 if (res == NULL)
1030 return -1;
1031 Py_DECREF(res);
1032 return 0;
1033}
1034
1035int
Christian Heimes33fe8092008-04-13 13:53:33 +00001036PyErr_WarnExplicit(PyObject *category, const char *text,
1037 const char *filename_str, int lineno,
1038 const char *module_str, PyObject *registry)
1039{
Christian Heimes33fe8092008-04-13 13:53:33 +00001040 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001041 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001042 PyObject *module = NULL;
1043 int ret = -1;
1044
1045 if (message == NULL || filename == NULL)
1046 goto exit;
1047 if (module_str != NULL) {
1048 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001049 if (module == NULL)
1050 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001051 }
1052
Victor Stinner14e461d2013-08-26 22:28:21 +02001053 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1054 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001055
1056 exit:
1057 Py_XDECREF(message);
1058 Py_XDECREF(module);
1059 Py_XDECREF(filename);
1060 return ret;
1061}
1062
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001063int
1064PyErr_WarnExplicitFormat(PyObject *category,
1065 const char *filename_str, int lineno,
1066 const char *module_str, PyObject *registry,
1067 const char *format, ...)
1068{
1069 PyObject *message;
1070 PyObject *module = NULL;
1071 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1072 int ret = -1;
1073 va_list vargs;
1074
1075 if (filename == NULL)
1076 goto exit;
1077 if (module_str != NULL) {
1078 module = PyUnicode_FromString(module_str);
1079 if (module == NULL)
1080 goto exit;
1081 }
1082
1083#ifdef HAVE_STDARG_PROTOTYPES
1084 va_start(vargs, format);
1085#else
1086 va_start(vargs);
1087#endif
1088 message = PyUnicode_FromFormatV(format, vargs);
1089 if (message != NULL) {
1090 PyObject *res;
1091 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001092 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001093 Py_DECREF(message);
1094 if (res != NULL) {
1095 Py_DECREF(res);
1096 ret = 0;
1097 }
1098 }
1099 va_end(vargs);
1100exit:
1101 Py_XDECREF(module);
1102 Py_XDECREF(filename);
1103 return ret;
1104}
1105
Christian Heimes33fe8092008-04-13 13:53:33 +00001106
1107PyDoc_STRVAR(warn_doc,
1108"Issue a warning, or maybe ignore it or raise an exception.");
1109
1110PyDoc_STRVAR(warn_explicit_doc,
1111"Low-level inferface to warnings functionality.");
1112
1113static PyMethodDef warnings_functions[] = {
1114 {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS,
1115 warn_doc},
1116 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1117 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001118 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1119 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001120 /* XXX(brett.cannon): add showwarning? */
1121 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001123};
1124
1125
1126static PyObject *
1127create_filter(PyObject *category, const char *action)
1128{
1129 static PyObject *ignore_str = NULL;
1130 static PyObject *error_str = NULL;
1131 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001132 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001133 PyObject *action_obj = NULL;
1134 PyObject *lineno, *result;
1135
1136 if (!strcmp(action, "ignore")) {
1137 if (ignore_str == NULL) {
1138 ignore_str = PyUnicode_InternFromString("ignore");
1139 if (ignore_str == NULL)
1140 return NULL;
1141 }
1142 action_obj = ignore_str;
1143 }
1144 else if (!strcmp(action, "error")) {
1145 if (error_str == NULL) {
1146 error_str = PyUnicode_InternFromString("error");
1147 if (error_str == NULL)
1148 return NULL;
1149 }
1150 action_obj = error_str;
1151 }
1152 else if (!strcmp(action, "default")) {
1153 if (default_str == NULL) {
1154 default_str = PyUnicode_InternFromString("default");
1155 if (default_str == NULL)
1156 return NULL;
1157 }
1158 action_obj = default_str;
1159 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001160 else if (!strcmp(action, "always")) {
1161 if (always_str == NULL) {
1162 always_str = PyUnicode_InternFromString("always");
1163 if (always_str == NULL)
1164 return NULL;
1165 }
1166 action_obj = always_str;
1167 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001168 else {
1169 Py_FatalError("unknown action");
1170 }
1171
1172 /* This assumes the line number is zero for now. */
1173 lineno = PyLong_FromLong(0);
1174 if (lineno == NULL)
1175 return NULL;
1176 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
1177 Py_DECREF(lineno);
1178 return result;
1179}
1180
1181static PyObject *
1182init_filters(void)
1183{
Georg Brandl08be72d2010-10-24 15:11:22 +00001184 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001185 unsigned int pos = 0; /* Post-incremented in each use. */
1186 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001187 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001188
Christian Heimes33fe8092008-04-13 13:53:33 +00001189 if (filters == NULL)
1190 return NULL;
1191
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001192 PyList_SET_ITEM(filters, pos++,
1193 create_filter(PyExc_DeprecationWarning, "ignore"));
1194 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001195 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001196 PyList_SET_ITEM(filters, pos++,
1197 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001198 if (Py_BytesWarningFlag > 1)
1199 bytes_action = "error";
1200 else if (Py_BytesWarningFlag)
1201 bytes_action = "default";
1202 else
1203 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001204 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001205 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001206 /* resource usage warnings are enabled by default in pydebug mode */
1207#ifdef Py_DEBUG
1208 resource_action = "always";
1209#else
1210 resource_action = "ignore";
1211#endif
1212 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1213 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001214 for (x = 0; x < pos; x += 1) {
1215 if (PyList_GET_ITEM(filters, x) == NULL) {
1216 Py_DECREF(filters);
1217 return NULL;
1218 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001219 }
1220
1221 return filters;
1222}
1223
Martin v. Löwis1a214512008-06-11 05:26:20 +00001224static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 PyModuleDef_HEAD_INIT,
1226 MODULE_NAME,
1227 warnings__doc__,
1228 0,
1229 warnings_functions,
1230 NULL,
1231 NULL,
1232 NULL,
1233 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001234};
1235
Christian Heimes33fe8092008-04-13 13:53:33 +00001236
1237PyMODINIT_FUNC
1238_PyWarnings_Init(void)
1239{
Brett Cannon0759dd62009-04-01 18:13:07 +00001240 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001241
Martin v. Löwis1a214512008-06-11 05:26:20 +00001242 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001243 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001244 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001245
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001246 if (_filters == NULL) {
1247 _filters = init_filters();
1248 if (_filters == NULL)
1249 return NULL;
1250 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001251 Py_INCREF(_filters);
1252 if (PyModule_AddObject(m, "filters", _filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001253 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001254
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001255 if (_once_registry == NULL) {
1256 _once_registry = PyDict_New();
1257 if (_once_registry == NULL)
1258 return NULL;
1259 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001260 Py_INCREF(_once_registry);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001261 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001262 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001263
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001264 if (_default_action == NULL) {
1265 _default_action = PyUnicode_FromString("default");
1266 if (_default_action == NULL)
1267 return NULL;
1268 }
1269 Py_INCREF(_default_action);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001270 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001271 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001272
1273 _filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001274 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001275}