blob: 8616195c4e322a0a5d6ff09386e1bc45efcfc8cb [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
Eric Snow05351c12017-09-05 21:43:08 -070011/* 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 */
15static PyObject *_default_action; /* String */
16static long _filters_version;
17
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;
Victor Stinnere98445a2016-03-23 00:54:48 +010047 PyObject *warnings_module, *obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000048
49 if (warnings_str == NULL) {
50 warnings_str = PyUnicode_InternFromString("warnings");
51 if (warnings_str == NULL)
52 return NULL;
53 }
54
Victor Stinnere98445a2016-03-23 00:54:48 +010055 /* don't try to import after the start of the Python finallization */
Eric Snow05351c12017-09-05 21:43:08 -070056 if (try_import && _Py_Finalizing == NULL) {
Victor Stinnere98445a2016-03-23 00:54:48 +010057 warnings_module = PyImport_Import(warnings_str);
58 if (warnings_module == NULL) {
59 /* Fallback to the C implementation if we cannot get
60 the Python implementation */
61 PyErr_Clear();
Christian Heimes33fe8092008-04-13 13:53:33 +000062 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010063 }
64 }
65 else {
Eric Snow86b7afd2017-09-04 17:54:09 -060066 warnings_module = _PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010067 if (warnings_module == NULL)
68 return NULL;
69
Victor Stinnere98445a2016-03-23 00:54:48 +010070 Py_INCREF(warnings_module);
71 }
72
73 if (!PyObject_HasAttrString(warnings_module, attr)) {
74 Py_DECREF(warnings_module);
75 return NULL;
76 }
77
78 obj = PyObject_GetAttrString(warnings_module, attr);
79 Py_DECREF(warnings_module);
80 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000081}
82
83
Neal Norwitz32dde222008-04-15 06:43:13 +000084static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000085get_once_registry(void)
86{
87 PyObject *registry;
88
Victor Stinnere98445a2016-03-23 00:54:48 +010089 registry = get_warnings_attr("onceregistry", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000090 if (registry == NULL) {
91 if (PyErr_Occurred())
92 return NULL;
Eric Snow05351c12017-09-05 21:43:08 -070093 return _once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000094 }
Eric Snow05351c12017-09-05 21:43:08 -070095 Py_DECREF(_once_registry);
96 _once_registry = registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000097 return registry;
98}
99
100
Brett Cannon0759dd62009-04-01 18:13:07 +0000101static PyObject *
102get_default_action(void)
103{
104 PyObject *default_action;
105
Victor Stinnere98445a2016-03-23 00:54:48 +0100106 default_action = get_warnings_attr("defaultaction", 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000107 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (PyErr_Occurred()) {
109 return NULL;
110 }
Eric Snow05351c12017-09-05 21:43:08 -0700111 return _default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000112 }
113
Eric Snow05351c12017-09-05 21:43:08 -0700114 Py_DECREF(_default_action);
115 _default_action = default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000116 return default_action;
117}
118
119
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400120/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100121static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000122get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
123 PyObject *module, PyObject **item)
124{
Brett Cannon0759dd62009-04-01 18:13:07 +0000125 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000126 Py_ssize_t i;
127 PyObject *warnings_filters;
128
Victor Stinnere98445a2016-03-23 00:54:48 +0100129 warnings_filters = get_warnings_attr("filters", 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000130 if (warnings_filters == NULL) {
131 if (PyErr_Occurred())
132 return NULL;
133 }
134 else {
Eric Snow05351c12017-09-05 21:43:08 -0700135 Py_DECREF(_filters);
136 _filters = warnings_filters;
Christian Heimes33fe8092008-04-13 13:53:33 +0000137 }
138
Eric Snow05351c12017-09-05 21:43:08 -0700139 if (_filters == NULL || !PyList_Check(_filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000140 PyErr_SetString(PyExc_ValueError,
141 MODULE_NAME ".filters must be a list");
142 return NULL;
143 }
144
Eric Snow05351c12017-09-05 21:43:08 -0700145 /* _filters could change while we are iterating over it. */
146 for (i = 0; i < PyList_GET_SIZE(_filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000147 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
148 Py_ssize_t ln;
149 int is_subclass, good_msg, good_mod;
150
Eric Snow05351c12017-09-05 21:43:08 -0700151 tmp_item = PyList_GET_ITEM(_filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400152 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000153 PyErr_Format(PyExc_ValueError,
154 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
155 return NULL;
156 }
157
158 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400159 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000160 action = PyTuple_GET_ITEM(tmp_item, 0);
161 msg = PyTuple_GET_ITEM(tmp_item, 1);
162 cat = PyTuple_GET_ITEM(tmp_item, 2);
163 mod = PyTuple_GET_ITEM(tmp_item, 3);
164 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
165
166 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400167 if (good_msg == -1) {
168 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100169 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400170 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100171
Christian Heimes33fe8092008-04-13 13:53:33 +0000172 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400173 if (good_mod == -1) {
174 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100175 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400176 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100177
Christian Heimes33fe8092008-04-13 13:53:33 +0000178 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400179 if (is_subclass == -1) {
180 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100181 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400182 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100183
Christian Heimes33fe8092008-04-13 13:53:33 +0000184 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400185 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400186 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000187 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400188 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000189
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400190 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
191 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100192 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400193 }
194
195 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000196 }
197
Brett Cannon0759dd62009-04-01 18:13:07 +0000198 action = get_default_action();
199 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400200 Py_INCREF(Py_None);
201 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100202 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000203 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000204
205 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000206 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000207 return NULL;
208}
209
Brett Cannon0759dd62009-04-01 18:13:07 +0000210
Christian Heimes33fe8092008-04-13 13:53:33 +0000211static int
212already_warned(PyObject *registry, PyObject *key, int should_set)
213{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200214 PyObject *version_obj, *already_warned;
215 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000216
217 if (key == NULL)
218 return -1;
219
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200220 version_obj = _PyDict_GetItemId(registry, &PyId_version);
221 if (version_obj == NULL
222 || !PyLong_CheckExact(version_obj)
Eric Snow05351c12017-09-05 21:43:08 -0700223 || PyLong_AsLong(version_obj) != _filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200224 PyDict_Clear(registry);
Eric Snow05351c12017-09-05 21:43:08 -0700225 version_obj = PyLong_FromLong(_filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200226 if (version_obj == NULL)
227 return -1;
228 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
229 Py_DECREF(version_obj);
230 return -1;
231 }
232 Py_DECREF(version_obj);
233 }
234 else {
235 already_warned = PyDict_GetItem(registry, key);
236 if (already_warned != NULL) {
237 int rc = PyObject_IsTrue(already_warned);
238 if (rc != 0)
239 return rc;
240 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000241 }
242
243 /* This warning wasn't found in the registry, set it. */
244 if (should_set)
245 return PyDict_SetItem(registry, key, Py_True);
246 return 0;
247}
248
249/* New reference. */
250static PyObject *
251normalize_module(PyObject *filename)
252{
253 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100254 int kind;
255 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000256 Py_ssize_t len;
257
Victor Stinner9e30aa52011-11-21 02:49:52 +0100258 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000259 if (len < 0)
260 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100261
262 if (len == 0)
263 return PyUnicode_FromString("<unknown>");
264
265 kind = PyUnicode_KIND(filename);
266 data = PyUnicode_DATA(filename);
267
268 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000269 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100270 PyUnicode_READ(kind, data, len-3) == '.' &&
271 PyUnicode_READ(kind, data, len-2) == 'p' &&
272 PyUnicode_READ(kind, data, len-1) == 'y')
273 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100274 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000275 }
276 else {
277 module = filename;
278 Py_INCREF(module);
279 }
280 return module;
281}
282
283static int
284update_registry(PyObject *registry, PyObject *text, PyObject *category,
285 int add_zero)
286{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300287 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000288 int rc;
289
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300290 if (add_zero)
291 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000292 else
293 altkey = PyTuple_Pack(2, text, category);
294
295 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000296 Py_XDECREF(altkey);
297 return rc;
298}
299
300static void
Victor Stinner914cde82016-03-19 01:03:51 +0100301show_warning(PyObject *filename, int lineno, PyObject *text,
302 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 PyObject *f_stderr;
305 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000306 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200307 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000308
309 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
310
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200311 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000312 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100313 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000314
Victor Stinnerbd303c12013-11-07 23:07:29 +0100315 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000316 if (f_stderr == NULL) {
317 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100318 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000319 }
320
321 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100322 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
323 goto error;
324 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
325 goto error;
326 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
327 goto error;
328 if (PyFile_WriteString(": ", f_stderr) < 0)
329 goto error;
330 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
331 goto error;
332 if (PyFile_WriteString("\n", f_stderr) < 0)
333 goto error;
334 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000335
336 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000337 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100338 int kind;
339 void *data;
340 Py_ssize_t i, len;
341 Py_UCS4 ch;
342 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000343
Victor Stinnera4c704b2013-10-29 23:43:41 +0100344 if (PyUnicode_READY(sourceline) < 1)
345 goto error;
346
347 kind = PyUnicode_KIND(sourceline);
348 data = PyUnicode_DATA(sourceline);
349 len = PyUnicode_GET_LENGTH(sourceline);
350 for (i=0; i<len; i++) {
351 ch = PyUnicode_READ(kind, data, i);
352 if (ch != ' ' && ch != '\t' && ch != '\014')
353 break;
354 }
355
356 truncated = PyUnicode_Substring(sourceline, i, len);
357 if (truncated == NULL)
358 goto error;
359
360 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
361 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000362 PyFile_WriteString("\n", f_stderr);
363 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200364 else {
365 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
366 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100367
368error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100369 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000370 PyErr_Clear();
371}
372
Victor Stinner1231a462016-03-19 00:47:17 +0100373static int
374call_show_warning(PyObject *category, PyObject *text, PyObject *message,
375 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100376 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100377{
378 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
379
Victor Stinnere98445a2016-03-23 00:54:48 +0100380 /* If the source parameter is set, try to get the Python implementation.
381 The Python implementation is able to log the traceback where the source
382 was allocated, whereas the C implementation doesnt. */
383 show_fn = get_warnings_attr("_showwarnmsg", source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100384 if (show_fn == NULL) {
385 if (PyErr_Occurred())
386 return -1;
387 show_warning(filename, lineno, text, category, sourceline);
388 return 0;
389 }
390
391 if (!PyCallable_Check(show_fn)) {
392 PyErr_SetString(PyExc_TypeError,
393 "warnings._showwarnmsg() must be set to a callable");
394 goto error;
395 }
396
Victor Stinnere98445a2016-03-23 00:54:48 +0100397 warnmsg_cls = get_warnings_attr("WarningMessage", 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100398 if (warnmsg_cls == NULL) {
399 PyErr_SetString(PyExc_RuntimeError,
400 "unable to get warnings.WarningMessage");
401 goto error;
402 }
403
404 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100405 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100406 NULL);
407 Py_DECREF(warnmsg_cls);
408 if (msg == NULL)
409 goto error;
410
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100411 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100412 Py_DECREF(show_fn);
413 Py_DECREF(msg);
414
415 if (res == NULL)
416 return -1;
417
418 Py_DECREF(res);
419 return 0;
420
421error:
422 Py_XDECREF(show_fn);
423 return -1;
424}
425
Christian Heimes33fe8092008-04-13 13:53:33 +0000426static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000428 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100429 PyObject *module, PyObject *registry, PyObject *sourceline,
430 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000431{
432 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400433 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100434 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000435 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100437 /* module can be None if a warning is emitted late during Python shutdown.
438 In this case, the Python warnings module was probably unloaded, filters
439 are no more available to choose as action. It is safer to ignore the
440 warning and do nothing. */
441 if (module == Py_None)
442 Py_RETURN_NONE;
443
Brett Cannondb734912008-06-27 00:52:15 +0000444 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
445 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
446 return NULL;
447 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000448
449 /* Normalize module. */
450 if (module == NULL) {
451 module = normalize_module(filename);
452 if (module == NULL)
453 return NULL;
454 }
455 else
456 Py_INCREF(module);
457
458 /* Normalize message. */
459 Py_INCREF(message); /* DECREF'ed in cleanup. */
460 rc = PyObject_IsInstance(message, PyExc_Warning);
461 if (rc == -1) {
462 goto cleanup;
463 }
464 if (rc == 1) {
465 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000466 if (text == NULL)
467 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000468 category = (PyObject*)message->ob_type;
469 }
470 else {
471 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100472 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000473 if (message == NULL)
474 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000475 }
476
477 lineno_obj = PyLong_FromLong(lineno);
478 if (lineno_obj == NULL)
479 goto cleanup;
480
Victor Stinner22f18752016-12-09 18:08:18 +0100481 if (source == Py_None) {
482 source = NULL;
483 }
484
Christian Heimes33fe8092008-04-13 13:53:33 +0000485 /* Create key. */
486 key = PyTuple_Pack(3, text, category, lineno_obj);
487 if (key == NULL)
488 goto cleanup;
489
Brett Cannondb734912008-06-27 00:52:15 +0000490 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000491 rc = already_warned(registry, key, 0);
492 if (rc == -1)
493 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000495 goto return_none;
496 /* Else this warning hasn't been generated before. */
497 }
498
499 action = get_filter(category, text, lineno, module, &item);
500 if (action == NULL)
501 goto cleanup;
502
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200503 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000504 PyErr_SetObject(category, message);
505 goto cleanup;
506 }
507
508 /* Store in the registry that we've been here, *except* when the action
509 is "always". */
510 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200511 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000512 if (registry != NULL && registry != Py_None &&
513 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000514 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200515 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000516 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200517 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000518 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000519 registry = get_once_registry();
520 if (registry == NULL)
521 goto cleanup;
522 }
Eric Snow05351c12017-09-05 21:43:08 -0700523 /* _once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000525 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200526 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000527 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000528 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000530 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200531 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000532 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100533 "Unrecognized action (%R) in warnings.filters:\n %R",
534 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000535 goto cleanup;
536 }
537 }
538
Christian Heimes1a8501c2008-10-02 19:56:01 +0000539 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000540 goto return_none;
541 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100542 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100543 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100544 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000545 }
546 else /* if (rc == -1) */
547 goto cleanup;
548
549 return_none:
550 result = Py_None;
551 Py_INCREF(result);
552
553 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400554 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000555 Py_XDECREF(key);
556 Py_XDECREF(text);
557 Py_XDECREF(lineno_obj);
558 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000559 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000560 return result; /* Py_None or NULL. */
561}
562
Larry Hastings714e4932015-09-06 00:39:37 -0700563static int
564is_internal_frame(PyFrameObject *frame)
565{
566 static PyObject *importlib_string = NULL;
567 static PyObject *bootstrap_string = NULL;
568 PyObject *filename;
569 int contains;
570
571 if (importlib_string == NULL) {
572 importlib_string = PyUnicode_FromString("importlib");
573 if (importlib_string == NULL) {
574 return 0;
575 }
576
577 bootstrap_string = PyUnicode_FromString("_bootstrap");
578 if (bootstrap_string == NULL) {
579 Py_DECREF(importlib_string);
580 return 0;
581 }
582 Py_INCREF(importlib_string);
583 Py_INCREF(bootstrap_string);
584 }
585
586 if (frame == NULL || frame->f_code == NULL ||
587 frame->f_code->co_filename == NULL) {
588 return 0;
589 }
590 filename = frame->f_code->co_filename;
591 if (!PyUnicode_Check(filename)) {
592 return 0;
593 }
594 contains = PyUnicode_Contains(filename, importlib_string);
595 if (contains < 0) {
596 return 0;
597 }
598 else if (contains > 0) {
599 contains = PyUnicode_Contains(filename, bootstrap_string);
600 if (contains < 0) {
601 return 0;
602 }
603 else if (contains > 0) {
604 return 1;
605 }
606 }
607
608 return 0;
609}
610
611static PyFrameObject *
612next_external_frame(PyFrameObject *frame)
613{
614 do {
615 frame = frame->f_back;
616 } while (frame != NULL && is_internal_frame(frame));
617
618 return frame;
619}
620
Christian Heimes33fe8092008-04-13 13:53:33 +0000621/* filename, module, and registry are new refs, globals is borrowed */
622/* Returns 0 on error (no new refs), 1 on success */
623static int
624setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
625 PyObject **module, PyObject **registry)
626{
627 PyObject *globals;
628
629 /* Setup globals and lineno. */
630 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700631 // Stack level comparisons to Python code is off by one as there is no
632 // warnings-related stack level to avoid.
633 if (stack_level <= 0 || is_internal_frame(f)) {
634 while (--stack_level > 0 && f != NULL) {
635 f = f->f_back;
636 }
637 }
638 else {
639 while (--stack_level > 0 && f != NULL) {
640 f = next_external_frame(f);
641 }
642 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000643
644 if (f == NULL) {
645 globals = PyThreadState_Get()->interp->sysdict;
646 *lineno = 1;
647 }
648 else {
649 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000650 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000651 }
652
653 *module = NULL;
654
655 /* Setup registry. */
656 assert(globals != NULL);
657 assert(PyDict_Check(globals));
658 *registry = PyDict_GetItemString(globals, "__warningregistry__");
659 if (*registry == NULL) {
660 int rc;
661
662 *registry = PyDict_New();
663 if (*registry == NULL)
664 return 0;
665
666 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
667 if (rc < 0)
668 goto handle_error;
669 }
670 else
671 Py_INCREF(*registry);
672
673 /* Setup module. */
674 *module = PyDict_GetItemString(globals, "__name__");
675 if (*module == NULL) {
676 *module = PyUnicode_FromString("<string>");
677 if (*module == NULL)
678 goto handle_error;
679 }
680 else
681 Py_INCREF(*module);
682
683 /* Setup filename. */
684 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200685 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200686 Py_ssize_t len;
687 int kind;
688 void *data;
689
690 if (PyUnicode_READY(*filename))
691 goto handle_error;
692
Victor Stinner9e30aa52011-11-21 02:49:52 +0100693 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200694 kind = PyUnicode_KIND(*filename);
695 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000696
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500697#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400698 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000699 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200700 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500701 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
702 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400703 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000704 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200705 *filename = PyUnicode_Substring(*filename, 0,
706 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000707 if (*filename == NULL)
708 goto handle_error;
709 }
710 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000711 Py_INCREF(*filename);
712 }
713 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500714 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200715 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100716 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100717 /* PyList_Check() is needed because sys.argv is set to None during
718 Python finalization */
719 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000720 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000721 *filename = PyList_GetItem(argv, 0);
722 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000723 /* If sys.argv[0] is false, then use '__main__'. */
724 is_true = PyObject_IsTrue(*filename);
725 if (is_true < 0) {
726 Py_DECREF(*filename);
727 goto handle_error;
728 }
729 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300730 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000731 if (*filename == NULL)
732 goto handle_error;
733 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000734 }
735 else {
736 /* embedded interpreters don't have sys.argv, see bug #839151 */
737 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100738 if (*filename == NULL)
739 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000740 }
741 }
742 if (*filename == NULL) {
743 *filename = *module;
744 Py_INCREF(*filename);
745 }
746 }
747
748 return 1;
749
750 handle_error:
751 /* filename not XDECREF'ed here as there is no way to jump here with a
752 dangling reference. */
753 Py_XDECREF(*registry);
754 Py_XDECREF(*module);
755 return 0;
756}
757
758static PyObject *
759get_category(PyObject *message, PyObject *category)
760{
761 int rc;
762
763 /* Get category. */
764 rc = PyObject_IsInstance(message, PyExc_Warning);
765 if (rc == -1)
766 return NULL;
767
768 if (rc == 1)
769 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300770 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000771 category = PyExc_UserWarning;
772
773 /* Validate category. */
774 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300775 /* category is not a subclass of PyExc_Warning or
776 PyObject_IsSubclass raised an error */
777 if (rc == -1 || rc == 0) {
778 PyErr_Format(PyExc_TypeError,
779 "category must be a Warning subclass, not '%s'",
780 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000781 return NULL;
782 }
783
784 return category;
785}
786
787static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100788do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
789 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000790{
791 PyObject *filename, *module, *registry, *res;
792 int lineno;
793
794 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
795 return NULL;
796
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100797 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100798 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000799 Py_DECREF(filename);
800 Py_DECREF(registry);
801 Py_DECREF(module);
802 return res;
803}
804
Victor Stinner22f18752016-12-09 18:08:18 +0100805/*[clinic input]
806warn as warnings_warn
807
808 message: object
809 category: object = None
810 stacklevel: Py_ssize_t = 1
811 source: object = None
812
813Issue a warning, or maybe ignore it or raise an exception.
814[clinic start generated code]*/
815
Christian Heimes33fe8092008-04-13 13:53:33 +0000816static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100817warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
818 Py_ssize_t stacklevel, PyObject *source)
819/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000820{
Christian Heimes33fe8092008-04-13 13:53:33 +0000821 category = get_category(message, category);
822 if (category == NULL)
823 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100824 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000825}
826
827static PyObject *
828warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
829{
830 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100831 "module", "registry", "module_globals",
832 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000833 PyObject *message;
834 PyObject *category;
835 PyObject *filename;
836 int lineno;
837 PyObject *module = NULL;
838 PyObject *registry = NULL;
839 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100840 PyObject *sourceobj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000841
Victor Stinner914cde82016-03-19 01:03:51 +0100842 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000843 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100844 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000845 return NULL;
846
847 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200848 _Py_IDENTIFIER(get_source);
849 _Py_IDENTIFIER(splitlines);
850 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000851 PyObject *loader;
852 PyObject *module_name;
853 PyObject *source;
854 PyObject *source_list;
855 PyObject *source_line;
856 PyObject *returned;
857
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200858 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
859 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200860 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
861 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000862
863 /* Check/get the requisite pieces needed for the loader. */
864 loader = PyDict_GetItemString(module_globals, "__loader__");
865 module_name = PyDict_GetItemString(module_globals, "__name__");
866
867 if (loader == NULL || module_name == NULL)
868 goto standard_call;
869
870 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200871 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000872 goto standard_call;
873 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200874 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
875 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000876 if (!source)
877 return NULL;
878 else if (source == Py_None) {
879 Py_DECREF(Py_None);
880 goto standard_call;
881 }
882
883 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100884 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200885 PyId_splitlines.object,
886 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000887 Py_DECREF(source);
888 if (!source_list)
889 return NULL;
890
891 /* Get the source line. */
892 source_line = PyList_GetItem(source_list, lineno-1);
893 if (!source_line) {
894 Py_DECREF(source_list);
895 return NULL;
896 }
897
898 /* Handle the warning. */
899 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100900 registry, source_line, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000901 Py_DECREF(source_list);
902 return returned;
903 }
904
905 standard_call:
906 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner914cde82016-03-19 01:03:51 +0100907 registry, NULL, sourceobj);
Christian Heimes33fe8092008-04-13 13:53:33 +0000908}
909
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200910static PyObject *
911warnings_filters_mutated(PyObject *self, PyObject *args)
912{
Eric Snow05351c12017-09-05 21:43:08 -0700913 _filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200914 Py_RETURN_NONE;
915}
916
Christian Heimes33fe8092008-04-13 13:53:33 +0000917
918/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000919
920static int
921warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100922 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000923{
924 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000925
926 if (category == NULL)
927 category = PyExc_RuntimeWarning;
928
Victor Stinner914cde82016-03-19 01:03:51 +0100929 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000930 if (res == NULL)
931 return -1;
932 Py_DECREF(res);
933
934 return 0;
935}
936
Victor Stinner914cde82016-03-19 01:03:51 +0100937static int
938_PyErr_WarnFormatV(PyObject *source,
939 PyObject *category, Py_ssize_t stack_level,
940 const char *format, va_list vargs)
941{
942 PyObject *message;
943 int res;
944
945 message = PyUnicode_FromFormatV(format, vargs);
946 if (message == NULL)
947 return -1;
948
949 res = warn_unicode(category, message, stack_level, source);
950 Py_DECREF(message);
951 return res;
952}
953
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000954int
955PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
956 const char *format, ...)
957{
Victor Stinner914cde82016-03-19 01:03:51 +0100958 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000959 va_list vargs;
960
961#ifdef HAVE_STDARG_PROTOTYPES
962 va_start(vargs, format);
963#else
964 va_start(vargs);
965#endif
Victor Stinner914cde82016-03-19 01:03:51 +0100966 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000967 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +0100968 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000969}
970
971int
Victor Stinner914cde82016-03-19 01:03:51 +0100972PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
973 const char *format, ...)
974{
975 int res;
976 va_list vargs;
977
978#ifdef HAVE_STDARG_PROTOTYPES
979 va_start(vargs, format);
980#else
981 va_start(vargs);
982#endif
983 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
984 stack_level, format, vargs);
985 va_end(vargs);
986 return res;
987}
988
989
990int
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000991PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
992{
993 int ret;
994 PyObject *message = PyUnicode_FromString(text);
995 if (message == NULL)
996 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +0100997 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000998 Py_DECREF(message);
999 return ret;
1000}
1001
Ezio Melotti42da6632011-03-15 05:18:48 +02001002/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001003 Use PyErr_WarnEx instead. */
1004
1005#undef PyErr_Warn
1006
1007PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001008PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001009{
1010 return PyErr_WarnEx(category, text, 1);
1011}
1012
1013/* Warning with explicit origin */
1014int
Victor Stinner14e461d2013-08-26 22:28:21 +02001015PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1016 PyObject *filename, int lineno,
1017 PyObject *module, PyObject *registry)
1018{
1019 PyObject *res;
1020 if (category == NULL)
1021 category = PyExc_RuntimeWarning;
1022 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001023 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001024 if (res == NULL)
1025 return -1;
1026 Py_DECREF(res);
1027 return 0;
1028}
1029
1030int
Christian Heimes33fe8092008-04-13 13:53:33 +00001031PyErr_WarnExplicit(PyObject *category, const char *text,
1032 const char *filename_str, int lineno,
1033 const char *module_str, PyObject *registry)
1034{
Christian Heimes33fe8092008-04-13 13:53:33 +00001035 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001036 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001037 PyObject *module = NULL;
1038 int ret = -1;
1039
1040 if (message == NULL || filename == NULL)
1041 goto exit;
1042 if (module_str != NULL) {
1043 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001044 if (module == NULL)
1045 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001046 }
1047
Victor Stinner14e461d2013-08-26 22:28:21 +02001048 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1049 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001050
1051 exit:
1052 Py_XDECREF(message);
1053 Py_XDECREF(module);
1054 Py_XDECREF(filename);
1055 return ret;
1056}
1057
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001058int
1059PyErr_WarnExplicitFormat(PyObject *category,
1060 const char *filename_str, int lineno,
1061 const char *module_str, PyObject *registry,
1062 const char *format, ...)
1063{
1064 PyObject *message;
1065 PyObject *module = NULL;
1066 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1067 int ret = -1;
1068 va_list vargs;
1069
1070 if (filename == NULL)
1071 goto exit;
1072 if (module_str != NULL) {
1073 module = PyUnicode_FromString(module_str);
1074 if (module == NULL)
1075 goto exit;
1076 }
1077
1078#ifdef HAVE_STDARG_PROTOTYPES
1079 va_start(vargs, format);
1080#else
1081 va_start(vargs);
1082#endif
1083 message = PyUnicode_FromFormatV(format, vargs);
1084 if (message != NULL) {
1085 PyObject *res;
1086 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001087 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001088 Py_DECREF(message);
1089 if (res != NULL) {
1090 Py_DECREF(res);
1091 ret = 0;
1092 }
1093 }
1094 va_end(vargs);
1095exit:
1096 Py_XDECREF(module);
1097 Py_XDECREF(filename);
1098 return ret;
1099}
1100
Christian Heimes33fe8092008-04-13 13:53:33 +00001101
Christian Heimes33fe8092008-04-13 13:53:33 +00001102PyDoc_STRVAR(warn_explicit_doc,
1103"Low-level inferface to warnings functionality.");
1104
1105static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001106 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001107 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1108 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001109 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1110 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001111 /* XXX(brett.cannon): add showwarning? */
1112 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001114};
1115
1116
1117static PyObject *
1118create_filter(PyObject *category, const char *action)
1119{
1120 static PyObject *ignore_str = NULL;
1121 static PyObject *error_str = NULL;
1122 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +00001123 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001124 PyObject *action_obj = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001125
1126 if (!strcmp(action, "ignore")) {
1127 if (ignore_str == NULL) {
1128 ignore_str = PyUnicode_InternFromString("ignore");
1129 if (ignore_str == NULL)
1130 return NULL;
1131 }
1132 action_obj = ignore_str;
1133 }
1134 else if (!strcmp(action, "error")) {
1135 if (error_str == NULL) {
1136 error_str = PyUnicode_InternFromString("error");
1137 if (error_str == NULL)
1138 return NULL;
1139 }
1140 action_obj = error_str;
1141 }
1142 else if (!strcmp(action, "default")) {
1143 if (default_str == NULL) {
1144 default_str = PyUnicode_InternFromString("default");
1145 if (default_str == NULL)
1146 return NULL;
1147 }
1148 action_obj = default_str;
1149 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001150 else if (!strcmp(action, "always")) {
1151 if (always_str == NULL) {
1152 always_str = PyUnicode_InternFromString("always");
1153 if (always_str == NULL)
1154 return NULL;
1155 }
1156 action_obj = always_str;
1157 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001158 else {
1159 Py_FatalError("unknown action");
1160 }
1161
1162 /* This assumes the line number is zero for now. */
Eric Snow05351c12017-09-05 21:43:08 -07001163 return PyTuple_Pack(5, action_obj, Py_None, category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001164}
1165
1166static PyObject *
1167init_filters(void)
1168{
Georg Brandl08be72d2010-10-24 15:11:22 +00001169 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001170 unsigned int pos = 0; /* Post-incremented in each use. */
1171 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001172 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001173
Christian Heimes33fe8092008-04-13 13:53:33 +00001174 if (filters == NULL)
1175 return NULL;
1176
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001177 PyList_SET_ITEM(filters, pos++,
1178 create_filter(PyExc_DeprecationWarning, "ignore"));
1179 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001180 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001181 PyList_SET_ITEM(filters, pos++,
1182 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001183 if (Py_BytesWarningFlag > 1)
1184 bytes_action = "error";
1185 else if (Py_BytesWarningFlag)
1186 bytes_action = "default";
1187 else
1188 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001189 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001190 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001191 /* resource usage warnings are enabled by default in pydebug mode */
1192#ifdef Py_DEBUG
1193 resource_action = "always";
1194#else
1195 resource_action = "ignore";
1196#endif
1197 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1198 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001199 for (x = 0; x < pos; x += 1) {
1200 if (PyList_GET_ITEM(filters, x) == NULL) {
1201 Py_DECREF(filters);
1202 return NULL;
1203 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001204 }
1205
1206 return filters;
1207}
1208
Martin v. Löwis1a214512008-06-11 05:26:20 +00001209static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 PyModuleDef_HEAD_INIT,
1211 MODULE_NAME,
1212 warnings__doc__,
1213 0,
1214 warnings_functions,
1215 NULL,
1216 NULL,
1217 NULL,
1218 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001219};
1220
Christian Heimes33fe8092008-04-13 13:53:33 +00001221
1222PyMODINIT_FUNC
1223_PyWarnings_Init(void)
1224{
Brett Cannon0759dd62009-04-01 18:13:07 +00001225 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001226
Martin v. Löwis1a214512008-06-11 05:26:20 +00001227 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001228 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001229 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001230
Eric Snow05351c12017-09-05 21:43:08 -07001231 if (_filters == NULL) {
1232 _filters = init_filters();
1233 if (_filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001234 return NULL;
1235 }
Eric Snow05351c12017-09-05 21:43:08 -07001236 Py_INCREF(_filters);
1237 if (PyModule_AddObject(m, "filters", _filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001238 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001239
Eric Snow05351c12017-09-05 21:43:08 -07001240 if (_once_registry == NULL) {
1241 _once_registry = PyDict_New();
1242 if (_once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001243 return NULL;
1244 }
Eric Snow05351c12017-09-05 21:43:08 -07001245 Py_INCREF(_once_registry);
1246 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001247 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001248
Eric Snow05351c12017-09-05 21:43:08 -07001249 if (_default_action == NULL) {
1250 _default_action = PyUnicode_FromString("default");
1251 if (_default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001252 return NULL;
1253 }
Eric Snow05351c12017-09-05 21:43:08 -07001254 Py_INCREF(_default_action);
1255 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001256 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001257
Eric Snow05351c12017-09-05 21:43:08 -07001258 _filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001259 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001260}