blob: 67f4c6bbe023be683be9893c52200d8e97799d41 [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
11/* Both 'filters' and 'onceregistry' can be set in warnings.py;
12 get_warnings_attr() will reset these variables accordingly. */
13static PyObject *_filters; /* List */
14static PyObject *_once_registry; /* Dict */
Brett Cannon0759dd62009-04-01 18:13:07 +000015static PyObject *_default_action; /* String */
Antoine Pitroucb0a0062014-09-18 02:40:46 +020016static long _filters_version;
Christian Heimes33fe8092008-04-13 13:53:33 +000017
Victor Stinnerbd303c12013-11-07 23:07:29 +010018_Py_IDENTIFIER(argv);
19_Py_IDENTIFIER(stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +000020
21static int
22check_matched(PyObject *obj, PyObject *arg)
23{
24 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020025 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000026 int rc;
27
28 if (obj == Py_None)
29 return 1;
Victor Stinner55ba38a2016-12-09 16:09:30 +010030 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000031 if (result == NULL)
32 return -1;
33
34 rc = PyObject_IsTrue(result);
35 Py_DECREF(result);
36 return rc;
37}
38
39/*
40 Returns a new reference.
41 A NULL return value can mean false or an error.
42*/
43static PyObject *
Victor Stinnere98445a2016-03-23 00:54:48 +010044get_warnings_attr(const char *attr, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000045{
46 static PyObject *warnings_str = NULL;
47 PyObject *all_modules;
Victor Stinnere98445a2016-03-23 00:54:48 +010048 PyObject *warnings_module, *obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000049
50 if (warnings_str == NULL) {
51 warnings_str = PyUnicode_InternFromString("warnings");
52 if (warnings_str == NULL)
53 return NULL;
54 }
55
Victor Stinnere98445a2016-03-23 00:54:48 +010056 /* don't try to import after the start of the Python finallization */
57 if (try_import && _Py_Finalizing == NULL) {
58 warnings_module = PyImport_Import(warnings_str);
59 if (warnings_module == NULL) {
60 /* Fallback to the C implementation if we cannot get
61 the Python implementation */
62 PyErr_Clear();
Christian Heimes33fe8092008-04-13 13:53:33 +000063 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010064 }
65 }
66 else {
67 all_modules = PyImport_GetModuleDict();
Victor Stinnere98445a2016-03-23 00:54:48 +010068
69 warnings_module = PyDict_GetItem(all_modules, warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010070 if (warnings_module == NULL)
71 return NULL;
72
Victor Stinnere98445a2016-03-23 00:54:48 +010073 Py_INCREF(warnings_module);
74 }
75
76 if (!PyObject_HasAttrString(warnings_module, attr)) {
77 Py_DECREF(warnings_module);
78 return NULL;
79 }
80
81 obj = PyObject_GetAttrString(warnings_module, attr);
82 Py_DECREF(warnings_module);
83 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000084}
85
86
Neal Norwitz32dde222008-04-15 06:43:13 +000087static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000088get_once_registry(void)
89{
90 PyObject *registry;
91
Victor Stinnere98445a2016-03-23 00:54:48 +010092 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000093 if (registry == NULL) {
94 if (PyErr_Occurred())
95 return NULL;
96 return _once_registry;
97 }
98 Py_DECREF(_once_registry);
99 _once_registry = registry;
100 return registry;
101}
102
103
Brett Cannon0759dd62009-04-01 18:13:07 +0000104static PyObject *
105get_default_action(void)
106{
107 PyObject *default_action;
108
Victor Stinnere98445a2016-03-23 00:54:48 +0100109 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000110 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 if (PyErr_Occurred()) {
112 return NULL;
113 }
114 return _default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000115 }
116
117 Py_DECREF(_default_action);
118 _default_action = default_action;
119 return default_action;
120}
121
122
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400123/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100124static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000125get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
126 PyObject *module, PyObject **item)
127{
Brett Cannon0759dd62009-04-01 18:13:07 +0000128 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000129 Py_ssize_t i;
130 PyObject *warnings_filters;
131
Victor Stinnere98445a2016-03-23 00:54:48 +0100132 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000133 if (warnings_filters == NULL) {
134 if (PyErr_Occurred())
135 return NULL;
136 }
137 else {
138 Py_DECREF(_filters);
139 _filters = warnings_filters;
140 }
141
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000142 if (_filters == NULL || !PyList_Check(_filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000143 PyErr_SetString(PyExc_ValueError,
144 MODULE_NAME ".filters must be a list");
145 return NULL;
146 }
147
148 /* _filters could change while we are iterating over it. */
149 for (i = 0; i < PyList_GET_SIZE(_filters); i++) {
150 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
151 Py_ssize_t ln;
152 int is_subclass, good_msg, good_mod;
153
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400154 tmp_item = PyList_GET_ITEM(_filters, i);
155 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000156 PyErr_Format(PyExc_ValueError,
157 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
158 return NULL;
159 }
160
161 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400162 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000163 action = PyTuple_GET_ITEM(tmp_item, 0);
164 msg = PyTuple_GET_ITEM(tmp_item, 1);
165 cat = PyTuple_GET_ITEM(tmp_item, 2);
166 mod = PyTuple_GET_ITEM(tmp_item, 3);
167 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
168
169 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400170 if (good_msg == -1) {
171 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100172 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400173 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100174
Christian Heimes33fe8092008-04-13 13:53:33 +0000175 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400176 if (good_mod == -1) {
177 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100178 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400179 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100180
Christian Heimes33fe8092008-04-13 13:53:33 +0000181 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400182 if (is_subclass == -1) {
183 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100184 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400185 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100186
Christian Heimes33fe8092008-04-13 13:53:33 +0000187 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400188 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400189 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000190 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400191 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000192
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400193 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
194 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100195 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400196 }
197
198 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000199 }
200
Brett Cannon0759dd62009-04-01 18:13:07 +0000201 action = get_default_action();
202 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400203 Py_INCREF(Py_None);
204 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100205 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000206 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000207
208 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000209 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000210 return NULL;
211}
212
Brett Cannon0759dd62009-04-01 18:13:07 +0000213
Christian Heimes33fe8092008-04-13 13:53:33 +0000214static int
215already_warned(PyObject *registry, PyObject *key, int should_set)
216{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200217 PyObject *version_obj, *already_warned;
218 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000219
220 if (key == NULL)
221 return -1;
222
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200223 version_obj = _PyDict_GetItemId(registry, &PyId_version);
224 if (version_obj == NULL
225 || !PyLong_CheckExact(version_obj)
226 || PyLong_AsLong(version_obj) != _filters_version) {
227 PyDict_Clear(registry);
228 version_obj = PyLong_FromLong(_filters_version);
229 if (version_obj == NULL)
230 return -1;
231 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
232 Py_DECREF(version_obj);
233 return -1;
234 }
235 Py_DECREF(version_obj);
236 }
237 else {
238 already_warned = PyDict_GetItem(registry, key);
239 if (already_warned != NULL) {
240 int rc = PyObject_IsTrue(already_warned);
241 if (rc != 0)
242 return rc;
243 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000244 }
245
246 /* This warning wasn't found in the registry, set it. */
247 if (should_set)
248 return PyDict_SetItem(registry, key, Py_True);
249 return 0;
250}
251
252/* New reference. */
253static PyObject *
254normalize_module(PyObject *filename)
255{
256 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100257 int kind;
258 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000259 Py_ssize_t len;
260
Victor Stinner9e30aa52011-11-21 02:49:52 +0100261 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000262 if (len < 0)
263 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100264
265 if (len == 0)
266 return PyUnicode_FromString("<unknown>");
267
268 kind = PyUnicode_KIND(filename);
269 data = PyUnicode_DATA(filename);
270
271 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000272 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100273 PyUnicode_READ(kind, data, len-3) == '.' &&
274 PyUnicode_READ(kind, data, len-2) == 'p' &&
275 PyUnicode_READ(kind, data, len-1) == 'y')
276 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100277 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000278 }
279 else {
280 module = filename;
281 Py_INCREF(module);
282 }
283 return module;
284}
285
286static int
287update_registry(PyObject *registry, PyObject *text, PyObject *category,
288 int add_zero)
289{
290 PyObject *altkey, *zero = NULL;
291 int rc;
292
293 if (add_zero) {
294 zero = PyLong_FromLong(0);
295 if (zero == NULL)
296 return -1;
297 altkey = PyTuple_Pack(3, text, category, zero);
298 }
299 else
300 altkey = PyTuple_Pack(2, text, category);
301
302 rc = already_warned(registry, altkey, 1);
303 Py_XDECREF(zero);
304 Py_XDECREF(altkey);
305 return rc;
306}
307
308static void
Victor Stinner914cde82016-03-19 01:03:51 +0100309show_warning(PyObject *filename, int lineno, PyObject *text,
310 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyObject *f_stderr;
313 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000314 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200315 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000316
317 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
318
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200319 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000320 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100321 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000322
Victor Stinnerbd303c12013-11-07 23:07:29 +0100323 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000324 if (f_stderr == NULL) {
325 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100326 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000327 }
328
329 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100330 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
331 goto error;
332 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
333 goto error;
334 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
335 goto error;
336 if (PyFile_WriteString(": ", f_stderr) < 0)
337 goto error;
338 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
339 goto error;
340 if (PyFile_WriteString("\n", f_stderr) < 0)
341 goto error;
342 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000343
344 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000345 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100346 int kind;
347 void *data;
348 Py_ssize_t i, len;
349 Py_UCS4 ch;
350 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000351
Victor Stinnera4c704b2013-10-29 23:43:41 +0100352 if (PyUnicode_READY(sourceline) < 1)
353 goto error;
354
355 kind = PyUnicode_KIND(sourceline);
356 data = PyUnicode_DATA(sourceline);
357 len = PyUnicode_GET_LENGTH(sourceline);
358 for (i=0; i<len; i++) {
359 ch = PyUnicode_READ(kind, data, i);
360 if (ch != ' ' && ch != '\t' && ch != '\014')
361 break;
362 }
363
364 truncated = PyUnicode_Substring(sourceline, i, len);
365 if (truncated == NULL)
366 goto error;
367
368 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
369 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000370 PyFile_WriteString("\n", f_stderr);
371 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200372 else {
373 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
374 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100375
376error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100377 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000378 PyErr_Clear();
379}
380
Victor Stinner1231a462016-03-19 00:47:17 +0100381static int
382call_show_warning(PyObject *category, PyObject *text, PyObject *message,
383 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100384 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100385{
386 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
387
Victor Stinnere98445a2016-03-23 00:54:48 +0100388 /* If the source parameter is set, try to get the Python implementation.
389 The Python implementation is able to log the traceback where the source
390 was allocated, whereas the C implementation doesnt. */
391 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100392 if (show_fn == NULL) {
393 if (PyErr_Occurred())
394 return -1;
395 show_warning(filename, lineno, text, category, sourceline);
396 return 0;
397 }
398
399 if (!PyCallable_Check(show_fn)) {
400 PyErr_SetString(PyExc_TypeError,
401 "warnings._showwarnmsg() must be set to a callable");
402 goto error;
403 }
404
Victor Stinnere98445a2016-03-23 00:54:48 +0100405 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100406 if (warnmsg_cls == NULL) {
407 PyErr_SetString(PyExc_RuntimeError,
408 "unable to get warnings.WarningMessage");
409 goto error;
410 }
411
412 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100413 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100414 NULL);
415 Py_DECREF(warnmsg_cls);
416 if (msg == NULL)
417 goto error;
418
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100419 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100420 Py_DECREF(show_fn);
421 Py_DECREF(msg);
422
423 if (res == NULL)
424 return -1;
425
426 Py_DECREF(res);
427 return 0;
428
429error:
430 Py_XDECREF(show_fn);
431 return -1;
432}
433
Christian Heimes33fe8092008-04-13 13:53:33 +0000434static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000436 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100437 PyObject *module, PyObject *registry, PyObject *sourceline,
438 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000439{
440 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400441 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100442 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000443 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100445 /* module can be None if a warning is emitted late during Python shutdown.
446 In this case, the Python warnings module was probably unloaded, filters
447 are no more available to choose as action. It is safer to ignore the
448 warning and do nothing. */
449 if (module == Py_None)
450 Py_RETURN_NONE;
451
Brett Cannondb734912008-06-27 00:52:15 +0000452 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
453 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
454 return NULL;
455 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000456
457 /* Normalize module. */
458 if (module == NULL) {
459 module = normalize_module(filename);
460 if (module == NULL)
461 return NULL;
462 }
463 else
464 Py_INCREF(module);
465
466 /* Normalize message. */
467 Py_INCREF(message); /* DECREF'ed in cleanup. */
468 rc = PyObject_IsInstance(message, PyExc_Warning);
469 if (rc == -1) {
470 goto cleanup;
471 }
472 if (rc == 1) {
473 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000474 if (text == NULL)
475 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000476 category = (PyObject*)message->ob_type;
477 }
478 else {
479 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100480 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000481 if (message == NULL)
482 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000483 }
484
485 lineno_obj = PyLong_FromLong(lineno);
486 if (lineno_obj == NULL)
487 goto cleanup;
488
Victor Stinner22f18752016-12-09 18:08:18 +0100489 if (source == Py_None) {
490 source = NULL;
491 }
492
Christian Heimes33fe8092008-04-13 13:53:33 +0000493 /* Create key. */
494 key = PyTuple_Pack(3, text, category, lineno_obj);
495 if (key == NULL)
496 goto cleanup;
497
Brett Cannondb734912008-06-27 00:52:15 +0000498 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000499 rc = already_warned(registry, key, 0);
500 if (rc == -1)
501 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000503 goto return_none;
504 /* Else this warning hasn't been generated before. */
505 }
506
507 action = get_filter(category, text, lineno, module, &item);
508 if (action == NULL)
509 goto cleanup;
510
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200511 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000512 PyErr_SetObject(category, message);
513 goto cleanup;
514 }
515
516 /* Store in the registry that we've been here, *except* when the action
517 is "always". */
518 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200519 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000520 if (registry != NULL && registry != Py_None &&
521 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000522 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200523 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000524 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200525 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000526 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000527 registry = get_once_registry();
528 if (registry == NULL)
529 goto cleanup;
530 }
531 /* _once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000533 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200534 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000535 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000536 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000538 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200539 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000540 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100541 "Unrecognized action (%R) in warnings.filters:\n %R",
542 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000543 goto cleanup;
544 }
545 }
546
Christian Heimes1a8501c2008-10-02 19:56:01 +0000547 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000548 goto return_none;
549 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100550 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100551 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100552 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000553 }
554 else /* if (rc == -1) */
555 goto cleanup;
556
557 return_none:
558 result = Py_None;
559 Py_INCREF(result);
560
561 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400562 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000563 Py_XDECREF(key);
564 Py_XDECREF(text);
565 Py_XDECREF(lineno_obj);
566 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000567 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000568 return result; /* Py_None or NULL. */
569}
570
Larry Hastings714e4932015-09-06 00:39:37 -0700571static int
572is_internal_frame(PyFrameObject *frame)
573{
574 static PyObject *importlib_string = NULL;
575 static PyObject *bootstrap_string = NULL;
576 PyObject *filename;
577 int contains;
578
579 if (importlib_string == NULL) {
580 importlib_string = PyUnicode_FromString("importlib");
581 if (importlib_string == NULL) {
582 return 0;
583 }
584
585 bootstrap_string = PyUnicode_FromString("_bootstrap");
586 if (bootstrap_string == NULL) {
587 Py_DECREF(importlib_string);
588 return 0;
589 }
590 Py_INCREF(importlib_string);
591 Py_INCREF(bootstrap_string);
592 }
593
594 if (frame == NULL || frame->f_code == NULL ||
595 frame->f_code->co_filename == NULL) {
596 return 0;
597 }
598 filename = frame->f_code->co_filename;
599 if (!PyUnicode_Check(filename)) {
600 return 0;
601 }
602 contains = PyUnicode_Contains(filename, importlib_string);
603 if (contains < 0) {
604 return 0;
605 }
606 else if (contains > 0) {
607 contains = PyUnicode_Contains(filename, bootstrap_string);
608 if (contains < 0) {
609 return 0;
610 }
611 else if (contains > 0) {
612 return 1;
613 }
614 }
615
616 return 0;
617}
618
619static PyFrameObject *
620next_external_frame(PyFrameObject *frame)
621{
622 do {
623 frame = frame->f_back;
624 } while (frame != NULL && is_internal_frame(frame));
625
626 return frame;
627}
628
Christian Heimes33fe8092008-04-13 13:53:33 +0000629/* filename, module, and registry are new refs, globals is borrowed */
630/* Returns 0 on error (no new refs), 1 on success */
631static int
632setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
633 PyObject **module, PyObject **registry)
634{
635 PyObject *globals;
636
637 /* Setup globals and lineno. */
638 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700639 // Stack level comparisons to Python code is off by one as there is no
640 // warnings-related stack level to avoid.
641 if (stack_level <= 0 || is_internal_frame(f)) {
642 while (--stack_level > 0 && f != NULL) {
643 f = f->f_back;
644 }
645 }
646 else {
647 while (--stack_level > 0 && f != NULL) {
648 f = next_external_frame(f);
649 }
650 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000651
652 if (f == NULL) {
653 globals = PyThreadState_Get()->interp->sysdict;
654 *lineno = 1;
655 }
656 else {
657 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000658 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000659 }
660
661 *module = NULL;
662
663 /* Setup registry. */
664 assert(globals != NULL);
665 assert(PyDict_Check(globals));
666 *registry = PyDict_GetItemString(globals, "__warningregistry__");
667 if (*registry == NULL) {
668 int rc;
669
670 *registry = PyDict_New();
671 if (*registry == NULL)
672 return 0;
673
674 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
675 if (rc < 0)
676 goto handle_error;
677 }
678 else
679 Py_INCREF(*registry);
680
681 /* Setup module. */
682 *module = PyDict_GetItemString(globals, "__name__");
683 if (*module == NULL) {
684 *module = PyUnicode_FromString("<string>");
685 if (*module == NULL)
686 goto handle_error;
687 }
688 else
689 Py_INCREF(*module);
690
691 /* Setup filename. */
692 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200693 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200694 Py_ssize_t len;
695 int kind;
696 void *data;
697
698 if (PyUnicode_READY(*filename))
699 goto handle_error;
700
Victor Stinner9e30aa52011-11-21 02:49:52 +0100701 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200702 kind = PyUnicode_KIND(*filename);
703 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000704
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500705#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400706 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000707 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200708 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500709 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
710 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400711 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000712 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 *filename = PyUnicode_Substring(*filename, 0,
714 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000715 if (*filename == NULL)
716 goto handle_error;
717 }
718 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000719 Py_INCREF(*filename);
720 }
721 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500722 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200723 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100724 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100725 /* PyList_Check() is needed because sys.argv is set to None during
726 Python finalization */
727 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000728 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000729 *filename = PyList_GetItem(argv, 0);
730 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000731 /* If sys.argv[0] is false, then use '__main__'. */
732 is_true = PyObject_IsTrue(*filename);
733 if (is_true < 0) {
734 Py_DECREF(*filename);
735 goto handle_error;
736 }
737 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300738 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000739 if (*filename == NULL)
740 goto handle_error;
741 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000742 }
743 else {
744 /* embedded interpreters don't have sys.argv, see bug #839151 */
745 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100746 if (*filename == NULL)
747 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000748 }
749 }
750 if (*filename == NULL) {
751 *filename = *module;
752 Py_INCREF(*filename);
753 }
754 }
755
756 return 1;
757
758 handle_error:
759 /* filename not XDECREF'ed here as there is no way to jump here with a
760 dangling reference. */
761 Py_XDECREF(*registry);
762 Py_XDECREF(*module);
763 return 0;
764}
765
766static PyObject *
767get_category(PyObject *message, PyObject *category)
768{
769 int rc;
770
771 /* Get category. */
772 rc = PyObject_IsInstance(message, PyExc_Warning);
773 if (rc == -1)
774 return NULL;
775
776 if (rc == 1)
777 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300778 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000779 category = PyExc_UserWarning;
780
781 /* Validate category. */
782 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300783 /* category is not a subclass of PyExc_Warning or
784 PyObject_IsSubclass raised an error */
785 if (rc == -1 || rc == 0) {
786 PyErr_Format(PyExc_TypeError,
787 "category must be a Warning subclass, not '%s'",
788 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000789 return NULL;
790 }
791
792 return category;
793}
794
795static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100796do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
797 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000798{
799 PyObject *filename, *module, *registry, *res;
800 int lineno;
801
802 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
803 return NULL;
804
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100805 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100806 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000807 Py_DECREF(filename);
808 Py_DECREF(registry);
809 Py_DECREF(module);
810 return res;
811}
812
Victor Stinner22f18752016-12-09 18:08:18 +0100813/*[clinic input]
814warn as warnings_warn
815
816 message: object
817 category: object = None
818 stacklevel: Py_ssize_t = 1
819 source: object = None
820
821Issue a warning, or maybe ignore it or raise an exception.
822[clinic start generated code]*/
823
Christian Heimes33fe8092008-04-13 13:53:33 +0000824static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100825warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
826 Py_ssize_t stacklevel, PyObject *source)
827/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000828{
Christian Heimes33fe8092008-04-13 13:53:33 +0000829 category = get_category(message, category);
830 if (category == NULL)
831 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100832 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000833}
834
835static PyObject *
836warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
837{
838 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100839 "module", "registry", "module_globals",
840 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000841 PyObject *message;
842 PyObject *category;
843 PyObject *filename;
844 int lineno;
845 PyObject *module = NULL;
846 PyObject *registry = NULL;
847 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100848 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000849
Victor Stinner914cde82016-03-19 01:03:51 +0100850 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000851 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100852 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000853 return NULL;
854
855 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200856 _Py_IDENTIFIER(get_source);
857 _Py_IDENTIFIER(splitlines);
858 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000859 PyObject *loader;
860 PyObject *module_name;
861 PyObject *source;
862 PyObject *source_list;
863 PyObject *source_line;
864 PyObject *returned;
865
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200866 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
867 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200868 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
869 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000870
871 /* Check/get the requisite pieces needed for the loader. */
872 loader = PyDict_GetItemString(module_globals, "__loader__");
873 module_name = PyDict_GetItemString(module_globals, "__name__");
874
875 if (loader == NULL || module_name == NULL)
876 goto standard_call;
877
878 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200879 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000880 goto standard_call;
881 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200882 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
883 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000884 if (!source)
885 return NULL;
886 else if (source == Py_None) {
887 Py_DECREF(Py_None);
888 goto standard_call;
889 }
890
891 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100892 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200893 PyId_splitlines.object,
894 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000895 Py_DECREF(source);
896 if (!source_list)
897 return NULL;
898
899 /* Get the source line. */
900 source_line = PyList_GetItem(source_list, lineno-1);
901 if (!source_line) {
902 Py_DECREF(source_list);
903 return NULL;
904 }
905
906 /* Handle the warning. */
907 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100908 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000909 Py_DECREF(source_list);
910 return returned;
911 }
912
913 standard_call:
914 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100915 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000916}
917
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200918static PyObject *
919warnings_filters_mutated(PyObject *self, PyObject *args)
920{
921 _filters_version++;
922 Py_RETURN_NONE;
923}
924
Christian Heimes33fe8092008-04-13 13:53:33 +0000925
926/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000927
928static int
929warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100930 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000931{
932 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000933
934 if (category == NULL)
935 category = PyExc_RuntimeWarning;
936
Victor Stinner914cde82016-03-19 01:03:51 +0100937 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000938 if (res == NULL)
939 return -1;
940 Py_DECREF(res);
941
942 return 0;
943}
944
Victor Stinner914cde82016-03-19 01:03:51 +0100945static int
946_PyErr_WarnFormatV(PyObject *source,
947 PyObject *category, Py_ssize_t stack_level,
948 const char *format, va_list vargs)
949{
950 PyObject *message;
951 int res;
952
953 message = PyUnicode_FromFormatV(format, vargs);
954 if (message == NULL)
955 return -1;
956
957 res = warn_unicode(category, message, stack_level, source);
958 Py_DECREF(message);
959 return res;
960}
961
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000962int
963PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
964 const char *format, ...)
965{
Victor Stinner914cde82016-03-19 01:03:51 +0100966 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000967 va_list vargs;
968
969#ifdef HAVE_STDARG_PROTOTYPES
970 va_start(vargs, format);
971#else
972 va_start(vargs);
973#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100974 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000975 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100976 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000977}
978
979int
Victor Stinner914cde82016-03-19 01:03:51 +0100980PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
981 const char *format, ...)
982{
983 int res;
984 va_list vargs;
985
986#ifdef HAVE_STDARG_PROTOTYPES
987 va_start(vargs, format);
988#else
989 va_start(vargs);
990#endif
991 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
992 stack_level, format, vargs);
993 va_end(vargs);
994 return res;
995}
996
997
998int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000999PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1000{
1001 int ret;
1002 PyObject *message = PyUnicode_FromString(text);
1003 if (message == NULL)
1004 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001005 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001006 Py_DECREF(message);
1007 return ret;
1008}
1009
Ezio Melotti42da6632011-03-15 05:18:48 +02001010/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001011 Use PyErr_WarnEx instead. */
1012
1013#undef PyErr_Warn
1014
1015PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001016PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001017{
1018 return PyErr_WarnEx(category, text, 1);
1019}
1020
1021/* Warning with explicit origin */
1022int
Victor Stinner14e461d2013-08-26 22:28:21 +02001023PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1024 PyObject *filename, int lineno,
1025 PyObject *module, PyObject *registry)
1026{
1027 PyObject *res;
1028 if (category == NULL)
1029 category = PyExc_RuntimeWarning;
1030 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001031 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001032 if (res == NULL)
1033 return -1;
1034 Py_DECREF(res);
1035 return 0;
1036}
1037
1038int
Christian Heimes33fe8092008-04-13 13:53:33 +00001039PyErr_WarnExplicit(PyObject *category, const char *text,
1040 const char *filename_str, int lineno,
1041 const char *module_str, PyObject *registry)
1042{
Christian Heimes33fe8092008-04-13 13:53:33 +00001043 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001044 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001045 PyObject *module = NULL;
1046 int ret = -1;
1047
1048 if (message == NULL || filename == NULL)
1049 goto exit;
1050 if (module_str != NULL) {
1051 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001052 if (module == NULL)
1053 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001054 }
1055
Victor Stinner14e461d2013-08-26 22:28:21 +02001056 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1057 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001058
1059 exit:
1060 Py_XDECREF(message);
1061 Py_XDECREF(module);
1062 Py_XDECREF(filename);
1063 return ret;
1064}
1065
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001066int
1067PyErr_WarnExplicitFormat(PyObject *category,
1068 const char *filename_str, int lineno,
1069 const char *module_str, PyObject *registry,
1070 const char *format, ...)
1071{
1072 PyObject *message;
1073 PyObject *module = NULL;
1074 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1075 int ret = -1;
1076 va_list vargs;
1077
1078 if (filename == NULL)
1079 goto exit;
1080 if (module_str != NULL) {
1081 module = PyUnicode_FromString(module_str);
1082 if (module == NULL)
1083 goto exit;
1084 }
1085
1086#ifdef HAVE_STDARG_PROTOTYPES
1087 va_start(vargs, format);
1088#else
1089 va_start(vargs);
1090#endif
1091 message = PyUnicode_FromFormatV(format, vargs);
1092 if (message != NULL) {
1093 PyObject *res;
1094 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001095 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001096 Py_DECREF(message);
1097 if (res != NULL) {
1098 Py_DECREF(res);
1099 ret = 0;
1100 }
1101 }
1102 va_end(vargs);
1103exit:
1104 Py_XDECREF(module);
1105 Py_XDECREF(filename);
1106 return ret;
1107}
1108
Christian Heimes33fe8092008-04-13 13:53:33 +00001109
Christian Heimes33fe8092008-04-13 13:53:33 +00001110PyDoc_STRVAR(warn_explicit_doc,
1111"Low-level inferface to warnings functionality.");
1112
1113static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001114 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001115 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1116 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001117 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1118 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001119 /* XXX(brett.cannon): add showwarning? */
1120 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001122};
1123
1124
1125static PyObject *
1126create_filter(PyObject *category, const char *action)
1127{
1128 static PyObject *ignore_str = NULL;
1129 static PyObject *error_str = NULL;
1130 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001131 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001132 PyObject *action_obj = NULL;
1133 PyObject *lineno, *result;
1134
1135 if (!strcmp(action, "ignore")) {
1136 if (ignore_str == NULL) {
1137 ignore_str = PyUnicode_InternFromString("ignore");
1138 if (ignore_str == NULL)
1139 return NULL;
1140 }
1141 action_obj = ignore_str;
1142 }
1143 else if (!strcmp(action, "error")) {
1144 if (error_str == NULL) {
1145 error_str = PyUnicode_InternFromString("error");
1146 if (error_str == NULL)
1147 return NULL;
1148 }
1149 action_obj = error_str;
1150 }
1151 else if (!strcmp(action, "default")) {
1152 if (default_str == NULL) {
1153 default_str = PyUnicode_InternFromString("default");
1154 if (default_str == NULL)
1155 return NULL;
1156 }
1157 action_obj = default_str;
1158 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001159 else if (!strcmp(action, "always")) {
1160 if (always_str == NULL) {
1161 always_str = PyUnicode_InternFromString("always");
1162 if (always_str == NULL)
1163 return NULL;
1164 }
1165 action_obj = always_str;
1166 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001167 else {
1168 Py_FatalError("unknown action");
1169 }
1170
1171 /* This assumes the line number is zero for now. */
1172 lineno = PyLong_FromLong(0);
1173 if (lineno == NULL)
1174 return NULL;
1175 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
1176 Py_DECREF(lineno);
1177 return result;
1178}
1179
1180static PyObject *
1181init_filters(void)
1182{
Georg Brandl08be72d2010-10-24 15:11:22 +00001183 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001184 unsigned int pos = 0; /* Post-incremented in each use. */
1185 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001186 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001187
Christian Heimes33fe8092008-04-13 13:53:33 +00001188 if (filters == NULL)
1189 return NULL;
1190
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001191 PyList_SET_ITEM(filters, pos++,
1192 create_filter(PyExc_DeprecationWarning, "ignore"));
1193 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001194 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001195 PyList_SET_ITEM(filters, pos++,
1196 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001197 if (Py_BytesWarningFlag > 1)
1198 bytes_action = "error";
1199 else if (Py_BytesWarningFlag)
1200 bytes_action = "default";
1201 else
1202 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001203 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001204 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001205 /* resource usage warnings are enabled by default in pydebug mode */
1206#ifdef Py_DEBUG
1207 resource_action = "always";
1208#else
1209 resource_action = "ignore";
1210#endif
1211 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1212 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001213 for (x = 0; x < pos; x += 1) {
1214 if (PyList_GET_ITEM(filters, x) == NULL) {
1215 Py_DECREF(filters);
1216 return NULL;
1217 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001218 }
1219
1220 return filters;
1221}
1222
Martin v. Löwis1a214512008-06-11 05:26:20 +00001223static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 PyModuleDef_HEAD_INIT,
1225 MODULE_NAME,
1226 warnings__doc__,
1227 0,
1228 warnings_functions,
1229 NULL,
1230 NULL,
1231 NULL,
1232 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001233};
1234
Christian Heimes33fe8092008-04-13 13:53:33 +00001235
1236PyMODINIT_FUNC
1237_PyWarnings_Init(void)
1238{
Brett Cannon0759dd62009-04-01 18:13:07 +00001239 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001240
Martin v. Löwis1a214512008-06-11 05:26:20 +00001241 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001242 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001243 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001244
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001245 if (_filters == NULL) {
1246 _filters = init_filters();
1247 if (_filters == NULL)
1248 return NULL;
1249 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001250 Py_INCREF(_filters);
1251 if (PyModule_AddObject(m, "filters", _filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001252 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001253
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001254 if (_once_registry == NULL) {
1255 _once_registry = PyDict_New();
1256 if (_once_registry == NULL)
1257 return NULL;
1258 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001259 Py_INCREF(_once_registry);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001260 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001261 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001262
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001263 if (_default_action == NULL) {
1264 _default_action = PyUnicode_FromString("default");
1265 if (_default_action == NULL)
1266 return NULL;
1267 }
1268 Py_INCREF(_default_action);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001269 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001270 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001271
1272 _filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001273 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001274}