blob: ba004859df82f72ff2d7c64315aa5223eb7a6712 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002#include "internal/pystate.h"
Christian Heimes33fe8092008-04-13 13:53:33 +00003#include "frameobject.h"
Victor Stinner22f18752016-12-09 18:08:18 +01004#include "clinic/_warnings.c.h"
Christian Heimes33fe8092008-04-13 13:53:33 +00005
6#define MODULE_NAME "_warnings"
Christian Heimes33fe8092008-04-13 13:53:33 +00007
8PyDoc_STRVAR(warnings__doc__,
9MODULE_NAME " provides basic warning filtering support.\n"
10"It is a helper module to speed up interpreter start-up.");
11
Victor Stinnerbd303c12013-11-07 23:07:29 +010012_Py_IDENTIFIER(argv);
13_Py_IDENTIFIER(stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +000014
15static int
16check_matched(PyObject *obj, PyObject *arg)
17{
18 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020019 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000020 int rc;
21
22 if (obj == Py_None)
23 return 1;
Victor Stinner55ba38a2016-12-09 16:09:30 +010024 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000025 if (result == NULL)
26 return -1;
27
28 rc = PyObject_IsTrue(result);
29 Py_DECREF(result);
30 return rc;
31}
32
33/*
34 Returns a new reference.
35 A NULL return value can mean false or an error.
36*/
37static PyObject *
Victor Stinnere98445a2016-03-23 00:54:48 +010038get_warnings_attr(const char *attr, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000039{
40 static PyObject *warnings_str = NULL;
Eric Snow93c92f72017-09-13 23:46:04 -070041 PyObject *all_modules;
Victor Stinnere98445a2016-03-23 00:54:48 +010042 PyObject *warnings_module, *obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000043
44 if (warnings_str == NULL) {
45 warnings_str = PyUnicode_InternFromString("warnings");
46 if (warnings_str == NULL)
47 return NULL;
48 }
49
Victor Stinnere98445a2016-03-23 00:54:48 +010050 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060051 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010052 warnings_module = PyImport_Import(warnings_str);
53 if (warnings_module == NULL) {
54 /* Fallback to the C implementation if we cannot get
55 the Python implementation */
56 PyErr_Clear();
Christian Heimes33fe8092008-04-13 13:53:33 +000057 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010058 }
59 }
60 else {
Eric Snow93c92f72017-09-13 23:46:04 -070061 all_modules = PyImport_GetModuleDict();
62
63 warnings_module = PyDict_GetItem(all_modules, warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010064 if (warnings_module == NULL)
65 return NULL;
66
Victor Stinnere98445a2016-03-23 00:54:48 +010067 Py_INCREF(warnings_module);
68 }
69
70 if (!PyObject_HasAttrString(warnings_module, attr)) {
71 Py_DECREF(warnings_module);
72 return NULL;
73 }
74
75 obj = PyObject_GetAttrString(warnings_module, attr);
76 Py_DECREF(warnings_module);
77 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000078}
79
80
Neal Norwitz32dde222008-04-15 06:43:13 +000081static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000082get_once_registry(void)
83{
84 PyObject *registry;
85
Victor Stinnere98445a2016-03-23 00:54:48 +010086 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000087 if (registry == NULL) {
88 if (PyErr_Occurred())
89 return NULL;
Eric Snow2ebc5ce2017-09-07 23:51:28 -060090 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000091 }
Oren Milman252033d2017-09-11 09:28:39 +030092 if (!PyDict_Check(registry)) {
93 PyErr_SetString(PyExc_TypeError,
94 "warnings.onceregistry must be a dict");
95 Py_DECREF(registry);
96 return NULL;
97 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060098 Py_DECREF(_PyRuntime.warnings.once_registry);
99 _PyRuntime.warnings.once_registry = registry;
Christian Heimes33fe8092008-04-13 13:53:33 +0000100 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 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000115 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300116 if (!PyUnicode_Check(default_action)) {
117 PyErr_Format(PyExc_TypeError,
118 MODULE_NAME ".defaultaction must be a string, "
119 "not '%.200s'",
120 Py_TYPE(default_action)->tp_name);
121 Py_DECREF(default_action);
122 return NULL;
123 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600124 Py_DECREF(_PyRuntime.warnings.default_action);
125 _PyRuntime.warnings.default_action = default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000126 return default_action;
127}
128
129
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400130/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100131static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000132get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
133 PyObject *module, PyObject **item)
134{
Brett Cannon0759dd62009-04-01 18:13:07 +0000135 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000136 Py_ssize_t i;
137 PyObject *warnings_filters;
138
Victor Stinnere98445a2016-03-23 00:54:48 +0100139 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000140 if (warnings_filters == NULL) {
141 if (PyErr_Occurred())
142 return NULL;
143 }
144 else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600145 Py_DECREF(_PyRuntime.warnings.filters);
146 _PyRuntime.warnings.filters = warnings_filters;
Christian Heimes33fe8092008-04-13 13:53:33 +0000147 }
148
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600149 PyObject *filters = _PyRuntime.warnings.filters;
150 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000151 PyErr_SetString(PyExc_ValueError,
152 MODULE_NAME ".filters must be a list");
153 return NULL;
154 }
155
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600156 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
157 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000158 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
159 Py_ssize_t ln;
160 int is_subclass, good_msg, good_mod;
161
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600162 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400163 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000164 PyErr_Format(PyExc_ValueError,
165 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
166 return NULL;
167 }
168
169 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400170 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000171 action = PyTuple_GET_ITEM(tmp_item, 0);
172 msg = PyTuple_GET_ITEM(tmp_item, 1);
173 cat = PyTuple_GET_ITEM(tmp_item, 2);
174 mod = PyTuple_GET_ITEM(tmp_item, 3);
175 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
176
Oren Milman9d984fd2017-09-12 00:18:09 +0300177 if (!PyUnicode_Check(action)) {
178 PyErr_Format(PyExc_TypeError,
179 "action must be a string, not '%.200s'",
180 Py_TYPE(action)->tp_name);
181 Py_DECREF(tmp_item);
182 return NULL;
183 }
184
Christian Heimes33fe8092008-04-13 13:53:33 +0000185 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400186 if (good_msg == -1) {
187 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100188 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400189 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100190
Christian Heimes33fe8092008-04-13 13:53:33 +0000191 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400192 if (good_mod == -1) {
193 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100194 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400195 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100196
Christian Heimes33fe8092008-04-13 13:53:33 +0000197 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400198 if (is_subclass == -1) {
199 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100200 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400201 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100202
Christian Heimes33fe8092008-04-13 13:53:33 +0000203 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400204 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400205 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000206 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400207 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000208
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400209 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
210 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100211 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400212 }
213
214 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000215 }
216
Brett Cannon0759dd62009-04-01 18:13:07 +0000217 action = get_default_action();
218 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400219 Py_INCREF(Py_None);
220 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100221 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000222 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000223
Christian Heimes33fe8092008-04-13 13:53:33 +0000224 return NULL;
225}
226
Brett Cannon0759dd62009-04-01 18:13:07 +0000227
Christian Heimes33fe8092008-04-13 13:53:33 +0000228static int
229already_warned(PyObject *registry, PyObject *key, int should_set)
230{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200231 PyObject *version_obj, *already_warned;
232 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000233
234 if (key == NULL)
235 return -1;
236
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200237 version_obj = _PyDict_GetItemId(registry, &PyId_version);
238 if (version_obj == NULL
239 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600240 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200241 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600242 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200243 if (version_obj == NULL)
244 return -1;
245 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
246 Py_DECREF(version_obj);
247 return -1;
248 }
249 Py_DECREF(version_obj);
250 }
251 else {
252 already_warned = PyDict_GetItem(registry, key);
253 if (already_warned != NULL) {
254 int rc = PyObject_IsTrue(already_warned);
255 if (rc != 0)
256 return rc;
257 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000258 }
259
260 /* This warning wasn't found in the registry, set it. */
261 if (should_set)
262 return PyDict_SetItem(registry, key, Py_True);
263 return 0;
264}
265
266/* New reference. */
267static PyObject *
268normalize_module(PyObject *filename)
269{
270 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100271 int kind;
272 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000273 Py_ssize_t len;
274
Victor Stinner9e30aa52011-11-21 02:49:52 +0100275 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000276 if (len < 0)
277 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100278
279 if (len == 0)
280 return PyUnicode_FromString("<unknown>");
281
282 kind = PyUnicode_KIND(filename);
283 data = PyUnicode_DATA(filename);
284
285 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000286 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100287 PyUnicode_READ(kind, data, len-3) == '.' &&
288 PyUnicode_READ(kind, data, len-2) == 'p' &&
289 PyUnicode_READ(kind, data, len-1) == 'y')
290 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100291 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000292 }
293 else {
294 module = filename;
295 Py_INCREF(module);
296 }
297 return module;
298}
299
300static int
301update_registry(PyObject *registry, PyObject *text, PyObject *category,
302 int add_zero)
303{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300304 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000305 int rc;
306
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300307 if (add_zero)
308 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000309 else
310 altkey = PyTuple_Pack(2, text, category);
311
312 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000313 Py_XDECREF(altkey);
314 return rc;
315}
316
317static void
Victor Stinner914cde82016-03-19 01:03:51 +0100318show_warning(PyObject *filename, int lineno, PyObject *text,
319 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 PyObject *f_stderr;
322 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000323 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200324 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000325
326 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
327
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200328 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000329 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100330 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000331
Victor Stinnerbd303c12013-11-07 23:07:29 +0100332 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000333 if (f_stderr == NULL) {
334 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100335 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000336 }
337
338 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100339 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
340 goto error;
341 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
342 goto error;
343 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
344 goto error;
345 if (PyFile_WriteString(": ", f_stderr) < 0)
346 goto error;
347 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
348 goto error;
349 if (PyFile_WriteString("\n", f_stderr) < 0)
350 goto error;
351 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000352
353 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000354 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100355 int kind;
356 void *data;
357 Py_ssize_t i, len;
358 Py_UCS4 ch;
359 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000360
Victor Stinnera4c704b2013-10-29 23:43:41 +0100361 if (PyUnicode_READY(sourceline) < 1)
362 goto error;
363
364 kind = PyUnicode_KIND(sourceline);
365 data = PyUnicode_DATA(sourceline);
366 len = PyUnicode_GET_LENGTH(sourceline);
367 for (i=0; i<len; i++) {
368 ch = PyUnicode_READ(kind, data, i);
369 if (ch != ' ' && ch != '\t' && ch != '\014')
370 break;
371 }
372
373 truncated = PyUnicode_Substring(sourceline, i, len);
374 if (truncated == NULL)
375 goto error;
376
377 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
378 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000379 PyFile_WriteString("\n", f_stderr);
380 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200381 else {
382 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
383 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100384
385error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100386 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000387 PyErr_Clear();
388}
389
Victor Stinner1231a462016-03-19 00:47:17 +0100390static int
391call_show_warning(PyObject *category, PyObject *text, PyObject *message,
392 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100393 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100394{
395 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
396
Victor Stinnere98445a2016-03-23 00:54:48 +0100397 /* If the source parameter is set, try to get the Python implementation.
398 The Python implementation is able to log the traceback where the source
399 was allocated, whereas the C implementation doesnt. */
400 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100401 if (show_fn == NULL) {
402 if (PyErr_Occurred())
403 return -1;
404 show_warning(filename, lineno, text, category, sourceline);
405 return 0;
406 }
407
408 if (!PyCallable_Check(show_fn)) {
409 PyErr_SetString(PyExc_TypeError,
410 "warnings._showwarnmsg() must be set to a callable");
411 goto error;
412 }
413
Victor Stinnere98445a2016-03-23 00:54:48 +0100414 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100415 if (warnmsg_cls == NULL) {
416 PyErr_SetString(PyExc_RuntimeError,
417 "unable to get warnings.WarningMessage");
418 goto error;
419 }
420
421 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100422 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100423 NULL);
424 Py_DECREF(warnmsg_cls);
425 if (msg == NULL)
426 goto error;
427
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100428 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100429 Py_DECREF(show_fn);
430 Py_DECREF(msg);
431
432 if (res == NULL)
433 return -1;
434
435 Py_DECREF(res);
436 return 0;
437
438error:
439 Py_XDECREF(show_fn);
440 return -1;
441}
442
Christian Heimes33fe8092008-04-13 13:53:33 +0000443static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000445 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100446 PyObject *module, PyObject *registry, PyObject *sourceline,
447 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000448{
449 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400450 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100451 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000452 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100454 /* module can be None if a warning is emitted late during Python shutdown.
455 In this case, the Python warnings module was probably unloaded, filters
456 are no more available to choose as action. It is safer to ignore the
457 warning and do nothing. */
458 if (module == Py_None)
459 Py_RETURN_NONE;
460
Brett Cannondb734912008-06-27 00:52:15 +0000461 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300462 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000463 return NULL;
464 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000465
466 /* Normalize module. */
467 if (module == NULL) {
468 module = normalize_module(filename);
469 if (module == NULL)
470 return NULL;
471 }
472 else
473 Py_INCREF(module);
474
475 /* Normalize message. */
476 Py_INCREF(message); /* DECREF'ed in cleanup. */
477 rc = PyObject_IsInstance(message, PyExc_Warning);
478 if (rc == -1) {
479 goto cleanup;
480 }
481 if (rc == 1) {
482 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000483 if (text == NULL)
484 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000485 category = (PyObject*)message->ob_type;
486 }
487 else {
488 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100489 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000490 if (message == NULL)
491 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000492 }
493
494 lineno_obj = PyLong_FromLong(lineno);
495 if (lineno_obj == NULL)
496 goto cleanup;
497
Victor Stinner22f18752016-12-09 18:08:18 +0100498 if (source == Py_None) {
499 source = NULL;
500 }
501
Christian Heimes33fe8092008-04-13 13:53:33 +0000502 /* Create key. */
503 key = PyTuple_Pack(3, text, category, lineno_obj);
504 if (key == NULL)
505 goto cleanup;
506
Brett Cannondb734912008-06-27 00:52:15 +0000507 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000508 rc = already_warned(registry, key, 0);
509 if (rc == -1)
510 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000512 goto return_none;
513 /* Else this warning hasn't been generated before. */
514 }
515
516 action = get_filter(category, text, lineno, module, &item);
517 if (action == NULL)
518 goto cleanup;
519
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200520 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000521 PyErr_SetObject(category, message);
522 goto cleanup;
523 }
524
525 /* Store in the registry that we've been here, *except* when the action
526 is "always". */
527 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200528 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000529 if (registry != NULL && registry != Py_None &&
530 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000531 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200532 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000533 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200534 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000535 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000536 registry = get_once_registry();
537 if (registry == NULL)
538 goto cleanup;
539 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600540 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000542 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200543 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000544 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000545 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000547 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200548 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000549 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100550 "Unrecognized action (%R) in warnings.filters:\n %R",
551 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000552 goto cleanup;
553 }
554 }
555
Christian Heimes1a8501c2008-10-02 19:56:01 +0000556 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000557 goto return_none;
558 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100559 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100560 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100561 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000562 }
563 else /* if (rc == -1) */
564 goto cleanup;
565
566 return_none:
567 result = Py_None;
568 Py_INCREF(result);
569
570 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400571 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000572 Py_XDECREF(key);
573 Py_XDECREF(text);
574 Py_XDECREF(lineno_obj);
575 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000576 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000577 return result; /* Py_None or NULL. */
578}
579
Larry Hastings714e4932015-09-06 00:39:37 -0700580static int
581is_internal_frame(PyFrameObject *frame)
582{
583 static PyObject *importlib_string = NULL;
584 static PyObject *bootstrap_string = NULL;
585 PyObject *filename;
586 int contains;
587
588 if (importlib_string == NULL) {
589 importlib_string = PyUnicode_FromString("importlib");
590 if (importlib_string == NULL) {
591 return 0;
592 }
593
594 bootstrap_string = PyUnicode_FromString("_bootstrap");
595 if (bootstrap_string == NULL) {
596 Py_DECREF(importlib_string);
597 return 0;
598 }
599 Py_INCREF(importlib_string);
600 Py_INCREF(bootstrap_string);
601 }
602
603 if (frame == NULL || frame->f_code == NULL ||
604 frame->f_code->co_filename == NULL) {
605 return 0;
606 }
607 filename = frame->f_code->co_filename;
608 if (!PyUnicode_Check(filename)) {
609 return 0;
610 }
611 contains = PyUnicode_Contains(filename, importlib_string);
612 if (contains < 0) {
613 return 0;
614 }
615 else if (contains > 0) {
616 contains = PyUnicode_Contains(filename, bootstrap_string);
617 if (contains < 0) {
618 return 0;
619 }
620 else if (contains > 0) {
621 return 1;
622 }
623 }
624
625 return 0;
626}
627
628static PyFrameObject *
629next_external_frame(PyFrameObject *frame)
630{
631 do {
632 frame = frame->f_back;
633 } while (frame != NULL && is_internal_frame(frame));
634
635 return frame;
636}
637
Christian Heimes33fe8092008-04-13 13:53:33 +0000638/* filename, module, and registry are new refs, globals is borrowed */
639/* Returns 0 on error (no new refs), 1 on success */
640static int
641setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
642 PyObject **module, PyObject **registry)
643{
644 PyObject *globals;
645
646 /* Setup globals and lineno. */
647 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700648 // Stack level comparisons to Python code is off by one as there is no
649 // warnings-related stack level to avoid.
650 if (stack_level <= 0 || is_internal_frame(f)) {
651 while (--stack_level > 0 && f != NULL) {
652 f = f->f_back;
653 }
654 }
655 else {
656 while (--stack_level > 0 && f != NULL) {
657 f = next_external_frame(f);
658 }
659 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000660
661 if (f == NULL) {
662 globals = PyThreadState_Get()->interp->sysdict;
663 *lineno = 1;
664 }
665 else {
666 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000667 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000668 }
669
670 *module = NULL;
671
672 /* Setup registry. */
673 assert(globals != NULL);
674 assert(PyDict_Check(globals));
675 *registry = PyDict_GetItemString(globals, "__warningregistry__");
676 if (*registry == NULL) {
677 int rc;
678
679 *registry = PyDict_New();
680 if (*registry == NULL)
681 return 0;
682
683 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
684 if (rc < 0)
685 goto handle_error;
686 }
687 else
688 Py_INCREF(*registry);
689
690 /* Setup module. */
691 *module = PyDict_GetItemString(globals, "__name__");
692 if (*module == NULL) {
693 *module = PyUnicode_FromString("<string>");
694 if (*module == NULL)
695 goto handle_error;
696 }
697 else
698 Py_INCREF(*module);
699
700 /* Setup filename. */
701 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200702 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200703 Py_ssize_t len;
704 int kind;
705 void *data;
706
707 if (PyUnicode_READY(*filename))
708 goto handle_error;
709
Victor Stinner9e30aa52011-11-21 02:49:52 +0100710 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200711 kind = PyUnicode_KIND(*filename);
712 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000713
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500714#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400715 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000716 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200717 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500718 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
719 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400720 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000721 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200722 *filename = PyUnicode_Substring(*filename, 0,
723 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000724 if (*filename == NULL)
725 goto handle_error;
726 }
727 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000728 Py_INCREF(*filename);
729 }
730 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500731 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200732 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100733 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100734 /* PyList_Check() is needed because sys.argv is set to None during
735 Python finalization */
736 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000737 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000738 *filename = PyList_GetItem(argv, 0);
739 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000740 /* If sys.argv[0] is false, then use '__main__'. */
741 is_true = PyObject_IsTrue(*filename);
742 if (is_true < 0) {
743 Py_DECREF(*filename);
744 goto handle_error;
745 }
746 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300747 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000748 if (*filename == NULL)
749 goto handle_error;
750 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000751 }
752 else {
753 /* embedded interpreters don't have sys.argv, see bug #839151 */
754 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100755 if (*filename == NULL)
756 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000757 }
758 }
759 if (*filename == NULL) {
760 *filename = *module;
761 Py_INCREF(*filename);
762 }
763 }
764
765 return 1;
766
767 handle_error:
768 /* filename not XDECREF'ed here as there is no way to jump here with a
769 dangling reference. */
770 Py_XDECREF(*registry);
771 Py_XDECREF(*module);
772 return 0;
773}
774
775static PyObject *
776get_category(PyObject *message, PyObject *category)
777{
778 int rc;
779
780 /* Get category. */
781 rc = PyObject_IsInstance(message, PyExc_Warning);
782 if (rc == -1)
783 return NULL;
784
785 if (rc == 1)
786 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300787 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000788 category = PyExc_UserWarning;
789
790 /* Validate category. */
791 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300792 /* category is not a subclass of PyExc_Warning or
793 PyObject_IsSubclass raised an error */
794 if (rc == -1 || rc == 0) {
795 PyErr_Format(PyExc_TypeError,
796 "category must be a Warning subclass, not '%s'",
797 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000798 return NULL;
799 }
800
801 return category;
802}
803
804static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100805do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
806 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000807{
808 PyObject *filename, *module, *registry, *res;
809 int lineno;
810
811 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
812 return NULL;
813
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100814 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100815 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000816 Py_DECREF(filename);
817 Py_DECREF(registry);
818 Py_DECREF(module);
819 return res;
820}
821
Victor Stinner22f18752016-12-09 18:08:18 +0100822/*[clinic input]
823warn as warnings_warn
824
825 message: object
826 category: object = None
827 stacklevel: Py_ssize_t = 1
828 source: object = None
829
830Issue a warning, or maybe ignore it or raise an exception.
831[clinic start generated code]*/
832
Christian Heimes33fe8092008-04-13 13:53:33 +0000833static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100834warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
835 Py_ssize_t stacklevel, PyObject *source)
836/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000837{
Christian Heimes33fe8092008-04-13 13:53:33 +0000838 category = get_category(message, category);
839 if (category == NULL)
840 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100841 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000842}
843
844static PyObject *
845warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
846{
847 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100848 "module", "registry", "module_globals",
849 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000850 PyObject *message;
851 PyObject *category;
852 PyObject *filename;
853 int lineno;
854 PyObject *module = NULL;
855 PyObject *registry = NULL;
856 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100857 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000858
Victor Stinner914cde82016-03-19 01:03:51 +0100859 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000860 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100861 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000862 return NULL;
863
864 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200865 _Py_IDENTIFIER(get_source);
866 _Py_IDENTIFIER(splitlines);
867 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000868 PyObject *loader;
869 PyObject *module_name;
870 PyObject *source;
871 PyObject *source_list;
872 PyObject *source_line;
873 PyObject *returned;
874
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200875 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
876 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200877 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
878 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000879
880 /* Check/get the requisite pieces needed for the loader. */
881 loader = PyDict_GetItemString(module_globals, "__loader__");
882 module_name = PyDict_GetItemString(module_globals, "__name__");
883
884 if (loader == NULL || module_name == NULL)
885 goto standard_call;
886
887 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200888 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000889 goto standard_call;
890 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200891 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
892 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000893 if (!source)
894 return NULL;
895 else if (source == Py_None) {
896 Py_DECREF(Py_None);
897 goto standard_call;
898 }
899
900 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100901 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200902 PyId_splitlines.object,
903 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000904 Py_DECREF(source);
905 if (!source_list)
906 return NULL;
907
908 /* Get the source line. */
909 source_line = PyList_GetItem(source_list, lineno-1);
910 if (!source_line) {
911 Py_DECREF(source_list);
912 return NULL;
913 }
914
915 /* Handle the warning. */
916 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100917 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000918 Py_DECREF(source_list);
919 return returned;
920 }
921
922 standard_call:
923 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100924 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000925}
926
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200927static PyObject *
928warnings_filters_mutated(PyObject *self, PyObject *args)
929{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600930 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200931 Py_RETURN_NONE;
932}
933
Christian Heimes33fe8092008-04-13 13:53:33 +0000934
935/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000936
937static int
938warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100939 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000940{
941 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000942
943 if (category == NULL)
944 category = PyExc_RuntimeWarning;
945
Victor Stinner914cde82016-03-19 01:03:51 +0100946 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000947 if (res == NULL)
948 return -1;
949 Py_DECREF(res);
950
951 return 0;
952}
953
Victor Stinner914cde82016-03-19 01:03:51 +0100954static int
955_PyErr_WarnFormatV(PyObject *source,
956 PyObject *category, Py_ssize_t stack_level,
957 const char *format, va_list vargs)
958{
959 PyObject *message;
960 int res;
961
962 message = PyUnicode_FromFormatV(format, vargs);
963 if (message == NULL)
964 return -1;
965
966 res = warn_unicode(category, message, stack_level, source);
967 Py_DECREF(message);
968 return res;
969}
970
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000971int
972PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
973 const char *format, ...)
974{
Victor Stinner914cde82016-03-19 01:03:51 +0100975 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000976 va_list vargs;
977
978#ifdef HAVE_STDARG_PROTOTYPES
979 va_start(vargs, format);
980#else
981 va_start(vargs);
982#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100983 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000984 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100985 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000986}
987
988int
Victor Stinner914cde82016-03-19 01:03:51 +0100989PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
990 const char *format, ...)
991{
992 int res;
993 va_list vargs;
994
995#ifdef HAVE_STDARG_PROTOTYPES
996 va_start(vargs, format);
997#else
998 va_start(vargs);
999#endif
1000 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1001 stack_level, format, vargs);
1002 va_end(vargs);
1003 return res;
1004}
1005
1006
1007int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001008PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1009{
1010 int ret;
1011 PyObject *message = PyUnicode_FromString(text);
1012 if (message == NULL)
1013 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001014 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001015 Py_DECREF(message);
1016 return ret;
1017}
1018
Ezio Melotti42da6632011-03-15 05:18:48 +02001019/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001020 Use PyErr_WarnEx instead. */
1021
1022#undef PyErr_Warn
1023
1024PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001025PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001026{
1027 return PyErr_WarnEx(category, text, 1);
1028}
1029
1030/* Warning with explicit origin */
1031int
Victor Stinner14e461d2013-08-26 22:28:21 +02001032PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1033 PyObject *filename, int lineno,
1034 PyObject *module, PyObject *registry)
1035{
1036 PyObject *res;
1037 if (category == NULL)
1038 category = PyExc_RuntimeWarning;
1039 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001040 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001041 if (res == NULL)
1042 return -1;
1043 Py_DECREF(res);
1044 return 0;
1045}
1046
1047int
Christian Heimes33fe8092008-04-13 13:53:33 +00001048PyErr_WarnExplicit(PyObject *category, const char *text,
1049 const char *filename_str, int lineno,
1050 const char *module_str, PyObject *registry)
1051{
Christian Heimes33fe8092008-04-13 13:53:33 +00001052 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001053 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001054 PyObject *module = NULL;
1055 int ret = -1;
1056
1057 if (message == NULL || filename == NULL)
1058 goto exit;
1059 if (module_str != NULL) {
1060 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001061 if (module == NULL)
1062 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001063 }
1064
Victor Stinner14e461d2013-08-26 22:28:21 +02001065 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1066 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001067
1068 exit:
1069 Py_XDECREF(message);
1070 Py_XDECREF(module);
1071 Py_XDECREF(filename);
1072 return ret;
1073}
1074
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001075int
1076PyErr_WarnExplicitFormat(PyObject *category,
1077 const char *filename_str, int lineno,
1078 const char *module_str, PyObject *registry,
1079 const char *format, ...)
1080{
1081 PyObject *message;
1082 PyObject *module = NULL;
1083 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1084 int ret = -1;
1085 va_list vargs;
1086
1087 if (filename == NULL)
1088 goto exit;
1089 if (module_str != NULL) {
1090 module = PyUnicode_FromString(module_str);
1091 if (module == NULL)
1092 goto exit;
1093 }
1094
1095#ifdef HAVE_STDARG_PROTOTYPES
1096 va_start(vargs, format);
1097#else
1098 va_start(vargs);
1099#endif
1100 message = PyUnicode_FromFormatV(format, vargs);
1101 if (message != NULL) {
1102 PyObject *res;
1103 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001104 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001105 Py_DECREF(message);
1106 if (res != NULL) {
1107 Py_DECREF(res);
1108 ret = 0;
1109 }
1110 }
1111 va_end(vargs);
1112exit:
1113 Py_XDECREF(module);
1114 Py_XDECREF(filename);
1115 return ret;
1116}
1117
Christian Heimes33fe8092008-04-13 13:53:33 +00001118
Christian Heimes33fe8092008-04-13 13:53:33 +00001119PyDoc_STRVAR(warn_explicit_doc,
1120"Low-level inferface to warnings functionality.");
1121
1122static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001123 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001124 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1125 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001126 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1127 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001128 /* XXX(brett.cannon): add showwarning? */
1129 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001131};
1132
1133
1134static PyObject *
1135create_filter(PyObject *category, const char *action)
1136{
1137 static PyObject *ignore_str = NULL;
1138 static PyObject *error_str = NULL;
1139 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001140 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001141 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001142
1143 if (!strcmp(action, "ignore")) {
1144 if (ignore_str == NULL) {
1145 ignore_str = PyUnicode_InternFromString("ignore");
1146 if (ignore_str == NULL)
1147 return NULL;
1148 }
1149 action_obj = ignore_str;
1150 }
1151 else if (!strcmp(action, "error")) {
1152 if (error_str == NULL) {
1153 error_str = PyUnicode_InternFromString("error");
1154 if (error_str == NULL)
1155 return NULL;
1156 }
1157 action_obj = error_str;
1158 }
1159 else if (!strcmp(action, "default")) {
1160 if (default_str == NULL) {
1161 default_str = PyUnicode_InternFromString("default");
1162 if (default_str == NULL)
1163 return NULL;
1164 }
1165 action_obj = default_str;
1166 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001167 else if (!strcmp(action, "always")) {
1168 if (always_str == NULL) {
1169 always_str = PyUnicode_InternFromString("always");
1170 if (always_str == NULL)
1171 return NULL;
1172 }
1173 action_obj = always_str;
1174 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001175 else {
1176 Py_FatalError("unknown action");
1177 }
1178
1179 /* This assumes the line number is zero for now. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001180 return PyTuple_Pack(5, action_obj, Py_None,
1181 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001182}
1183
1184static PyObject *
1185init_filters(void)
1186{
Georg Brandl08be72d2010-10-24 15:11:22 +00001187 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001188 unsigned int pos = 0; /* Post-incremented in each use. */
1189 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001190 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001191
Christian Heimes33fe8092008-04-13 13:53:33 +00001192 if (filters == NULL)
1193 return NULL;
1194
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001195 PyList_SET_ITEM(filters, pos++,
1196 create_filter(PyExc_DeprecationWarning, "ignore"));
1197 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001198 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001199 PyList_SET_ITEM(filters, pos++,
1200 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001201 if (Py_BytesWarningFlag > 1)
1202 bytes_action = "error";
1203 else if (Py_BytesWarningFlag)
1204 bytes_action = "default";
1205 else
1206 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001207 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001208 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001209 /* resource usage warnings are enabled by default in pydebug mode */
1210#ifdef Py_DEBUG
1211 resource_action = "always";
1212#else
1213 resource_action = "ignore";
1214#endif
1215 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1216 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001217 for (x = 0; x < pos; x += 1) {
1218 if (PyList_GET_ITEM(filters, x) == NULL) {
1219 Py_DECREF(filters);
1220 return NULL;
1221 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001222 }
1223
1224 return filters;
1225}
1226
Martin v. Löwis1a214512008-06-11 05:26:20 +00001227static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 PyModuleDef_HEAD_INIT,
1229 MODULE_NAME,
1230 warnings__doc__,
1231 0,
1232 warnings_functions,
1233 NULL,
1234 NULL,
1235 NULL,
1236 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001237};
1238
Christian Heimes33fe8092008-04-13 13:53:33 +00001239
1240PyMODINIT_FUNC
1241_PyWarnings_Init(void)
1242{
Brett Cannon0759dd62009-04-01 18:13:07 +00001243 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001244
Martin v. Löwis1a214512008-06-11 05:26:20 +00001245 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001246 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001247 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001248
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001249 if (_PyRuntime.warnings.filters == NULL) {
1250 _PyRuntime.warnings.filters = init_filters();
1251 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001252 return NULL;
1253 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001254 Py_INCREF(_PyRuntime.warnings.filters);
1255 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001256 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001257
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001258 if (_PyRuntime.warnings.once_registry == NULL) {
1259 _PyRuntime.warnings.once_registry = PyDict_New();
1260 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001261 return NULL;
1262 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001263 Py_INCREF(_PyRuntime.warnings.once_registry);
1264 if (PyModule_AddObject(m, "_onceregistry",
1265 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001266 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001267
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001268 if (_PyRuntime.warnings.default_action == NULL) {
1269 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1270 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001271 return NULL;
1272 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001273 Py_INCREF(_PyRuntime.warnings.default_action);
1274 if (PyModule_AddObject(m, "_defaultaction",
1275 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001276 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001277
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001278 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001279 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001280}