blob: c286364dda03b2c7054e0a07de3be74cd4c45496 [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);
Victor Stinner747f48e2017-12-12 22:59:48 +010014#ifndef Py_DEBUG
Nick Coghlan9b997472018-01-08 12:45:02 +100015_Py_IDENTIFIER(default);
Victor Stinnerb98f1712017-11-23 17:13:44 +010016_Py_IDENTIFIER(ignore);
Victor Stinner747f48e2017-12-12 22:59:48 +010017#endif
Christian Heimes33fe8092008-04-13 13:53:33 +000018
19static int
20check_matched(PyObject *obj, PyObject *arg)
21{
22 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020023 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000024 int rc;
25
Nick Coghlan9b997472018-01-08 12:45:02 +100026 /* A 'None' filter always matches */
Christian Heimes33fe8092008-04-13 13:53:33 +000027 if (obj == Py_None)
28 return 1;
Nick Coghlan9b997472018-01-08 12:45:02 +100029
30 /* An internal plain text default filter must match exactly */
31 if (PyUnicode_CheckExact(obj)) {
32 int cmp_result = PyUnicode_Compare(obj, arg);
33 if (cmp_result == -1 && PyErr_Occurred()) {
34 return -1;
35 }
36 return !cmp_result;
37 }
38
39 /* Otherwise assume a regex filter and call its match() method */
Victor Stinner55ba38a2016-12-09 16:09:30 +010040 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000041 if (result == NULL)
42 return -1;
43
44 rc = PyObject_IsTrue(result);
45 Py_DECREF(result);
46 return rc;
47}
48
49/*
50 Returns a new reference.
51 A NULL return value can mean false or an error.
52*/
53static PyObject *
Victor Stinner82656272017-11-22 23:51:42 +010054get_warnings_attr(_Py_Identifier *attr_id, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000055{
Victor Stinner82656272017-11-22 23:51:42 +010056 PyObject *warnings_str;
Victor Stinnere98445a2016-03-23 00:54:48 +010057 PyObject *warnings_module, *obj;
Victor Stinner82656272017-11-22 23:51:42 +010058 _Py_IDENTIFIER(warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000059
Victor Stinner82656272017-11-22 23:51:42 +010060 warnings_str = _PyUnicode_FromId(&PyId_warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000061 if (warnings_str == NULL) {
Victor Stinner82656272017-11-22 23:51:42 +010062 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +000063 }
64
Victor Stinnere98445a2016-03-23 00:54:48 +010065 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060066 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010067 warnings_module = PyImport_Import(warnings_str);
68 if (warnings_module == NULL) {
69 /* Fallback to the C implementation if we cannot get
70 the Python implementation */
Serhiy Storchakad4f84802017-11-11 15:19:47 +020071 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
72 PyErr_Clear();
73 }
Christian Heimes33fe8092008-04-13 13:53:33 +000074 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010075 }
76 }
77 else {
Eric Snow3f9eee62017-09-15 16:35:20 -060078 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010079 if (warnings_module == NULL)
80 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010081 }
82
Victor Stinner82656272017-11-22 23:51:42 +010083 obj = _PyObject_GetAttrId(warnings_module, attr_id);
Victor Stinnere98445a2016-03-23 00:54:48 +010084 Py_DECREF(warnings_module);
Serhiy Storchakad4f84802017-11-11 15:19:47 +020085 if (obj == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
86 PyErr_Clear();
87 }
Victor Stinnere98445a2016-03-23 00:54:48 +010088 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000089}
90
91
Neal Norwitz32dde222008-04-15 06:43:13 +000092static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000093get_once_registry(void)
94{
95 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +010096 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +000097
Victor Stinner82656272017-11-22 23:51:42 +010098 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000099 if (registry == NULL) {
100 if (PyErr_Occurred())
101 return NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200102 assert(_PyRuntime.warnings.once_registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600103 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +0000104 }
Oren Milman252033d2017-09-11 09:28:39 +0300105 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200106 PyErr_Format(PyExc_TypeError,
107 MODULE_NAME ".onceregistry must be a dict, "
108 "not '%.200s'",
109 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +0300110 Py_DECREF(registry);
111 return NULL;
112 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200113 Py_SETREF(_PyRuntime.warnings.once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000114 return registry;
115}
116
117
Brett Cannon0759dd62009-04-01 18:13:07 +0000118static PyObject *
119get_default_action(void)
120{
121 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100122 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000123
Victor Stinner82656272017-11-22 23:51:42 +0100124 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000125 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 if (PyErr_Occurred()) {
127 return NULL;
128 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200129 assert(_PyRuntime.warnings.default_action);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600130 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000131 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300132 if (!PyUnicode_Check(default_action)) {
133 PyErr_Format(PyExc_TypeError,
134 MODULE_NAME ".defaultaction must be a string, "
135 "not '%.200s'",
136 Py_TYPE(default_action)->tp_name);
137 Py_DECREF(default_action);
138 return NULL;
139 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200140 Py_SETREF(_PyRuntime.warnings.default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000141 return default_action;
142}
143
144
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400145/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100146static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000147get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
148 PyObject *module, PyObject **item)
149{
Brett Cannon0759dd62009-04-01 18:13:07 +0000150 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000151 Py_ssize_t i;
152 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100153 _Py_IDENTIFIER(filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000154
Victor Stinner82656272017-11-22 23:51:42 +0100155 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000156 if (warnings_filters == NULL) {
157 if (PyErr_Occurred())
158 return NULL;
159 }
160 else {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200161 Py_SETREF(_PyRuntime.warnings.filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000162 }
163
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600164 PyObject *filters = _PyRuntime.warnings.filters;
165 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000166 PyErr_SetString(PyExc_ValueError,
167 MODULE_NAME ".filters must be a list");
168 return NULL;
169 }
170
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600171 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
172 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000173 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
174 Py_ssize_t ln;
175 int is_subclass, good_msg, good_mod;
176
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600177 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400178 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000179 PyErr_Format(PyExc_ValueError,
180 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
181 return NULL;
182 }
183
184 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400185 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000186 action = PyTuple_GET_ITEM(tmp_item, 0);
187 msg = PyTuple_GET_ITEM(tmp_item, 1);
188 cat = PyTuple_GET_ITEM(tmp_item, 2);
189 mod = PyTuple_GET_ITEM(tmp_item, 3);
190 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
191
Oren Milman9d984fd2017-09-12 00:18:09 +0300192 if (!PyUnicode_Check(action)) {
193 PyErr_Format(PyExc_TypeError,
194 "action must be a string, not '%.200s'",
195 Py_TYPE(action)->tp_name);
196 Py_DECREF(tmp_item);
197 return NULL;
198 }
199
Christian Heimes33fe8092008-04-13 13:53:33 +0000200 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400201 if (good_msg == -1) {
202 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100203 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400204 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100205
Christian Heimes33fe8092008-04-13 13:53:33 +0000206 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400207 if (good_mod == -1) {
208 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100209 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400210 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100211
Christian Heimes33fe8092008-04-13 13:53:33 +0000212 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400213 if (is_subclass == -1) {
214 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100215 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400216 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100217
Christian Heimes33fe8092008-04-13 13:53:33 +0000218 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400219 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400220 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000221 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400222 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000223
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400224 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
225 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100226 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400227 }
228
229 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000230 }
231
Brett Cannon0759dd62009-04-01 18:13:07 +0000232 action = get_default_action();
233 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400234 Py_INCREF(Py_None);
235 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100236 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000237 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000238
Christian Heimes33fe8092008-04-13 13:53:33 +0000239 return NULL;
240}
241
Brett Cannon0759dd62009-04-01 18:13:07 +0000242
Christian Heimes33fe8092008-04-13 13:53:33 +0000243static int
244already_warned(PyObject *registry, PyObject *key, int should_set)
245{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200246 PyObject *version_obj, *already_warned;
247 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000248
249 if (key == NULL)
250 return -1;
251
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200252 version_obj = _PyDict_GetItemId(registry, &PyId_version);
253 if (version_obj == NULL
254 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600255 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200256 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600257 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200258 if (version_obj == NULL)
259 return -1;
260 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
261 Py_DECREF(version_obj);
262 return -1;
263 }
264 Py_DECREF(version_obj);
265 }
266 else {
267 already_warned = PyDict_GetItem(registry, key);
268 if (already_warned != NULL) {
269 int rc = PyObject_IsTrue(already_warned);
270 if (rc != 0)
271 return rc;
272 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000273 }
274
275 /* This warning wasn't found in the registry, set it. */
276 if (should_set)
277 return PyDict_SetItem(registry, key, Py_True);
278 return 0;
279}
280
281/* New reference. */
282static PyObject *
283normalize_module(PyObject *filename)
284{
285 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100286 int kind;
287 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000288 Py_ssize_t len;
289
Victor Stinner9e30aa52011-11-21 02:49:52 +0100290 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000291 if (len < 0)
292 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100293
294 if (len == 0)
295 return PyUnicode_FromString("<unknown>");
296
297 kind = PyUnicode_KIND(filename);
298 data = PyUnicode_DATA(filename);
299
300 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000301 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100302 PyUnicode_READ(kind, data, len-3) == '.' &&
303 PyUnicode_READ(kind, data, len-2) == 'p' &&
304 PyUnicode_READ(kind, data, len-1) == 'y')
305 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100306 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000307 }
308 else {
309 module = filename;
310 Py_INCREF(module);
311 }
312 return module;
313}
314
315static int
316update_registry(PyObject *registry, PyObject *text, PyObject *category,
317 int add_zero)
318{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300319 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000320 int rc;
321
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300322 if (add_zero)
323 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000324 else
325 altkey = PyTuple_Pack(2, text, category);
326
327 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000328 Py_XDECREF(altkey);
329 return rc;
330}
331
332static void
Victor Stinner914cde82016-03-19 01:03:51 +0100333show_warning(PyObject *filename, int lineno, PyObject *text,
334 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 PyObject *f_stderr;
337 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000338 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200339 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000340
341 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
342
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200343 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000344 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100345 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000346
Victor Stinnerbd303c12013-11-07 23:07:29 +0100347 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000348 if (f_stderr == NULL) {
349 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100350 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000351 }
352
353 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100354 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
355 goto error;
356 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
357 goto error;
358 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
359 goto error;
360 if (PyFile_WriteString(": ", f_stderr) < 0)
361 goto error;
362 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
363 goto error;
364 if (PyFile_WriteString("\n", f_stderr) < 0)
365 goto error;
366 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000367
368 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000369 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100370 int kind;
371 void *data;
372 Py_ssize_t i, len;
373 Py_UCS4 ch;
374 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000375
Victor Stinnera4c704b2013-10-29 23:43:41 +0100376 if (PyUnicode_READY(sourceline) < 1)
377 goto error;
378
379 kind = PyUnicode_KIND(sourceline);
380 data = PyUnicode_DATA(sourceline);
381 len = PyUnicode_GET_LENGTH(sourceline);
382 for (i=0; i<len; i++) {
383 ch = PyUnicode_READ(kind, data, i);
384 if (ch != ' ' && ch != '\t' && ch != '\014')
385 break;
386 }
387
388 truncated = PyUnicode_Substring(sourceline, i, len);
389 if (truncated == NULL)
390 goto error;
391
392 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
393 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000394 PyFile_WriteString("\n", f_stderr);
395 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200396 else {
397 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
398 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100399
400error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100401 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000402 PyErr_Clear();
403}
404
Victor Stinner1231a462016-03-19 00:47:17 +0100405static int
406call_show_warning(PyObject *category, PyObject *text, PyObject *message,
407 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100408 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100409{
410 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100411 _Py_IDENTIFIER(_showwarnmsg);
412 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100413
Victor Stinnere98445a2016-03-23 00:54:48 +0100414 /* If the source parameter is set, try to get the Python implementation.
415 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600416 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100417 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100418 if (show_fn == NULL) {
419 if (PyErr_Occurred())
420 return -1;
421 show_warning(filename, lineno, text, category, sourceline);
422 return 0;
423 }
424
425 if (!PyCallable_Check(show_fn)) {
426 PyErr_SetString(PyExc_TypeError,
427 "warnings._showwarnmsg() must be set to a callable");
428 goto error;
429 }
430
Victor Stinner82656272017-11-22 23:51:42 +0100431 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100432 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200433 if (!PyErr_Occurred()) {
434 PyErr_SetString(PyExc_RuntimeError,
435 "unable to get warnings.WarningMessage");
436 }
Victor Stinner1231a462016-03-19 00:47:17 +0100437 goto error;
438 }
439
440 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100441 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100442 NULL);
443 Py_DECREF(warnmsg_cls);
444 if (msg == NULL)
445 goto error;
446
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100447 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100448 Py_DECREF(show_fn);
449 Py_DECREF(msg);
450
451 if (res == NULL)
452 return -1;
453
454 Py_DECREF(res);
455 return 0;
456
457error:
458 Py_XDECREF(show_fn);
459 return -1;
460}
461
Christian Heimes33fe8092008-04-13 13:53:33 +0000462static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000464 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100465 PyObject *module, PyObject *registry, PyObject *sourceline,
466 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000467{
468 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400469 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100470 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000471 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100473 /* module can be None if a warning is emitted late during Python shutdown.
474 In this case, the Python warnings module was probably unloaded, filters
475 are no more available to choose as action. It is safer to ignore the
476 warning and do nothing. */
477 if (module == Py_None)
478 Py_RETURN_NONE;
479
Brett Cannondb734912008-06-27 00:52:15 +0000480 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300481 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000482 return NULL;
483 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000484
485 /* Normalize module. */
486 if (module == NULL) {
487 module = normalize_module(filename);
488 if (module == NULL)
489 return NULL;
490 }
491 else
492 Py_INCREF(module);
493
494 /* Normalize message. */
495 Py_INCREF(message); /* DECREF'ed in cleanup. */
496 rc = PyObject_IsInstance(message, PyExc_Warning);
497 if (rc == -1) {
498 goto cleanup;
499 }
500 if (rc == 1) {
501 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000502 if (text == NULL)
503 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000504 category = (PyObject*)message->ob_type;
505 }
506 else {
507 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100508 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000509 if (message == NULL)
510 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000511 }
512
513 lineno_obj = PyLong_FromLong(lineno);
514 if (lineno_obj == NULL)
515 goto cleanup;
516
Victor Stinner22f18752016-12-09 18:08:18 +0100517 if (source == Py_None) {
518 source = NULL;
519 }
520
Christian Heimes33fe8092008-04-13 13:53:33 +0000521 /* Create key. */
522 key = PyTuple_Pack(3, text, category, lineno_obj);
523 if (key == NULL)
524 goto cleanup;
525
Brett Cannondb734912008-06-27 00:52:15 +0000526 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000527 rc = already_warned(registry, key, 0);
528 if (rc == -1)
529 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000531 goto return_none;
532 /* Else this warning hasn't been generated before. */
533 }
534
535 action = get_filter(category, text, lineno, module, &item);
536 if (action == NULL)
537 goto cleanup;
538
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200539 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000540 PyErr_SetObject(category, message);
541 goto cleanup;
542 }
543
Victor Stinnerc9758782017-11-27 16:57:07 +0100544 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
545 goto return_none;
546 }
547
Christian Heimes33fe8092008-04-13 13:53:33 +0000548 /* Store in the registry that we've been here, *except* when the action
549 is "always". */
550 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200551 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000552 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100553 PyDict_SetItem(registry, key, Py_True) < 0)
554 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000555 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100556 }
557
558 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000559 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000560 registry = get_once_registry();
561 if (registry == NULL)
562 goto cleanup;
563 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600564 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000566 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200567 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000568 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000569 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000571 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200572 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000573 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100574 "Unrecognized action (%R) in warnings.filters:\n %R",
575 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000576 goto cleanup;
577 }
578 }
579
Christian Heimes1a8501c2008-10-02 19:56:01 +0000580 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000581 goto return_none;
582 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100583 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100584 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100585 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000586 }
587 else /* if (rc == -1) */
588 goto cleanup;
589
590 return_none:
591 result = Py_None;
592 Py_INCREF(result);
593
594 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400595 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000596 Py_XDECREF(key);
597 Py_XDECREF(text);
598 Py_XDECREF(lineno_obj);
599 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000600 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000601 return result; /* Py_None or NULL. */
602}
603
Larry Hastings714e4932015-09-06 00:39:37 -0700604static int
605is_internal_frame(PyFrameObject *frame)
606{
607 static PyObject *importlib_string = NULL;
608 static PyObject *bootstrap_string = NULL;
609 PyObject *filename;
610 int contains;
611
612 if (importlib_string == NULL) {
613 importlib_string = PyUnicode_FromString("importlib");
614 if (importlib_string == NULL) {
615 return 0;
616 }
617
618 bootstrap_string = PyUnicode_FromString("_bootstrap");
619 if (bootstrap_string == NULL) {
620 Py_DECREF(importlib_string);
621 return 0;
622 }
623 Py_INCREF(importlib_string);
624 Py_INCREF(bootstrap_string);
625 }
626
627 if (frame == NULL || frame->f_code == NULL ||
628 frame->f_code->co_filename == NULL) {
629 return 0;
630 }
631 filename = frame->f_code->co_filename;
632 if (!PyUnicode_Check(filename)) {
633 return 0;
634 }
635 contains = PyUnicode_Contains(filename, importlib_string);
636 if (contains < 0) {
637 return 0;
638 }
639 else if (contains > 0) {
640 contains = PyUnicode_Contains(filename, bootstrap_string);
641 if (contains < 0) {
642 return 0;
643 }
644 else if (contains > 0) {
645 return 1;
646 }
647 }
648
649 return 0;
650}
651
652static PyFrameObject *
653next_external_frame(PyFrameObject *frame)
654{
655 do {
656 frame = frame->f_back;
657 } while (frame != NULL && is_internal_frame(frame));
658
659 return frame;
660}
661
Christian Heimes33fe8092008-04-13 13:53:33 +0000662/* filename, module, and registry are new refs, globals is borrowed */
663/* Returns 0 on error (no new refs), 1 on success */
664static int
665setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
666 PyObject **module, PyObject **registry)
667{
668 PyObject *globals;
669
670 /* Setup globals and lineno. */
671 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700672 // Stack level comparisons to Python code is off by one as there is no
673 // warnings-related stack level to avoid.
674 if (stack_level <= 0 || is_internal_frame(f)) {
675 while (--stack_level > 0 && f != NULL) {
676 f = f->f_back;
677 }
678 }
679 else {
680 while (--stack_level > 0 && f != NULL) {
681 f = next_external_frame(f);
682 }
683 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000684
685 if (f == NULL) {
686 globals = PyThreadState_Get()->interp->sysdict;
687 *lineno = 1;
688 }
689 else {
690 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000691 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000692 }
693
694 *module = NULL;
695
696 /* Setup registry. */
697 assert(globals != NULL);
698 assert(PyDict_Check(globals));
699 *registry = PyDict_GetItemString(globals, "__warningregistry__");
700 if (*registry == NULL) {
701 int rc;
702
703 *registry = PyDict_New();
704 if (*registry == NULL)
705 return 0;
706
707 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
708 if (rc < 0)
709 goto handle_error;
710 }
711 else
712 Py_INCREF(*registry);
713
714 /* Setup module. */
715 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300716 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
717 Py_INCREF(*module);
718 }
719 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000720 *module = PyUnicode_FromString("<string>");
721 if (*module == NULL)
722 goto handle_error;
723 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000724
725 /* Setup filename. */
726 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200727 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200728 Py_ssize_t len;
729 int kind;
730 void *data;
731
732 if (PyUnicode_READY(*filename))
733 goto handle_error;
734
Victor Stinner9e30aa52011-11-21 02:49:52 +0100735 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200736 kind = PyUnicode_KIND(*filename);
737 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000738
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500739#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400740 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000741 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200742 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500743 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
744 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400745 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000746 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200747 *filename = PyUnicode_Substring(*filename, 0,
748 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000749 if (*filename == NULL)
750 goto handle_error;
751 }
752 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000753 Py_INCREF(*filename);
754 }
755 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500756 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200757 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100758 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100759 /* PyList_Check() is needed because sys.argv is set to None during
760 Python finalization */
761 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000762 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000763 *filename = PyList_GetItem(argv, 0);
764 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000765 /* If sys.argv[0] is false, then use '__main__'. */
766 is_true = PyObject_IsTrue(*filename);
767 if (is_true < 0) {
768 Py_DECREF(*filename);
769 goto handle_error;
770 }
771 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300772 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000773 if (*filename == NULL)
774 goto handle_error;
775 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000776 }
777 else {
778 /* embedded interpreters don't have sys.argv, see bug #839151 */
779 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100780 if (*filename == NULL)
781 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000782 }
783 }
784 if (*filename == NULL) {
785 *filename = *module;
786 Py_INCREF(*filename);
787 }
788 }
789
790 return 1;
791
792 handle_error:
793 /* filename not XDECREF'ed here as there is no way to jump here with a
794 dangling reference. */
795 Py_XDECREF(*registry);
796 Py_XDECREF(*module);
797 return 0;
798}
799
800static PyObject *
801get_category(PyObject *message, PyObject *category)
802{
803 int rc;
804
805 /* Get category. */
806 rc = PyObject_IsInstance(message, PyExc_Warning);
807 if (rc == -1)
808 return NULL;
809
810 if (rc == 1)
811 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300812 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000813 category = PyExc_UserWarning;
814
815 /* Validate category. */
816 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300817 /* category is not a subclass of PyExc_Warning or
818 PyObject_IsSubclass raised an error */
819 if (rc == -1 || rc == 0) {
820 PyErr_Format(PyExc_TypeError,
821 "category must be a Warning subclass, not '%s'",
822 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000823 return NULL;
824 }
825
826 return category;
827}
828
829static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100830do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
831 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000832{
833 PyObject *filename, *module, *registry, *res;
834 int lineno;
835
836 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
837 return NULL;
838
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100839 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100840 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000841 Py_DECREF(filename);
842 Py_DECREF(registry);
843 Py_DECREF(module);
844 return res;
845}
846
Victor Stinner22f18752016-12-09 18:08:18 +0100847/*[clinic input]
848warn as warnings_warn
849
850 message: object
851 category: object = None
852 stacklevel: Py_ssize_t = 1
853 source: object = None
854
855Issue a warning, or maybe ignore it or raise an exception.
856[clinic start generated code]*/
857
Christian Heimes33fe8092008-04-13 13:53:33 +0000858static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100859warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
860 Py_ssize_t stacklevel, PyObject *source)
861/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000862{
Christian Heimes33fe8092008-04-13 13:53:33 +0000863 category = get_category(message, category);
864 if (category == NULL)
865 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100866 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000867}
868
869static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200870get_source_line(PyObject *module_globals, int lineno)
871{
872 _Py_IDENTIFIER(get_source);
873 _Py_IDENTIFIER(__loader__);
874 _Py_IDENTIFIER(__name__);
875 PyObject *loader;
876 PyObject *module_name;
877 PyObject *get_source;
878 PyObject *source;
879 PyObject *source_list;
880 PyObject *source_line;
881
882 /* Check/get the requisite pieces needed for the loader. */
883 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
884 if (loader == NULL) {
885 return NULL;
886 }
887 Py_INCREF(loader);
888 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
889 if (!module_name) {
890 Py_DECREF(loader);
891 return NULL;
892 }
893 Py_INCREF(module_name);
894
895 /* Make sure the loader implements the optional get_source() method. */
896 get_source = _PyObject_GetAttrId(loader, &PyId_get_source);
897 Py_DECREF(loader);
898 if (!get_source) {
899 Py_DECREF(module_name);
900 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
901 PyErr_Clear();
902 }
903 return NULL;
904 }
905 /* Call get_source() to get the source code. */
906 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
907 Py_DECREF(get_source);
908 Py_DECREF(module_name);
909 if (!source) {
910 return NULL;
911 }
912 if (source == Py_None) {
913 Py_DECREF(source);
914 return NULL;
915 }
916
917 /* Split the source into lines. */
918 source_list = PyUnicode_Splitlines(source, 0);
919 Py_DECREF(source);
920 if (!source_list) {
921 return NULL;
922 }
923
924 /* Get the source line. */
925 source_line = PyList_GetItem(source_list, lineno-1);
926 Py_XINCREF(source_line);
927 Py_DECREF(source_list);
928 return source_line;
929}
930
931static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000932warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
933{
934 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100935 "module", "registry", "module_globals",
936 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000937 PyObject *message;
938 PyObject *category;
939 PyObject *filename;
940 int lineno;
941 PyObject *module = NULL;
942 PyObject *registry = NULL;
943 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100944 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200945 PyObject *source_line = NULL;
946 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000947
Victor Stinner914cde82016-03-19 01:03:51 +0100948 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000949 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100950 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000951 return NULL;
952
953 if (module_globals) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200954 source_line = get_source_line(module_globals, lineno);
955 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000956 return NULL;
957 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000958 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200959 returned = warn_explicit(category, message, filename, lineno, module,
960 registry, source_line, sourceobj);
961 Py_XDECREF(source_line);
962 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000963}
964
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200965static PyObject *
966warnings_filters_mutated(PyObject *self, PyObject *args)
967{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600968 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200969 Py_RETURN_NONE;
970}
971
Christian Heimes33fe8092008-04-13 13:53:33 +0000972
973/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000974
975static int
976warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100977 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000978{
979 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000980
981 if (category == NULL)
982 category = PyExc_RuntimeWarning;
983
Victor Stinner914cde82016-03-19 01:03:51 +0100984 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000985 if (res == NULL)
986 return -1;
987 Py_DECREF(res);
988
989 return 0;
990}
991
Victor Stinner914cde82016-03-19 01:03:51 +0100992static int
993_PyErr_WarnFormatV(PyObject *source,
994 PyObject *category, Py_ssize_t stack_level,
995 const char *format, va_list vargs)
996{
997 PyObject *message;
998 int res;
999
1000 message = PyUnicode_FromFormatV(format, vargs);
1001 if (message == NULL)
1002 return -1;
1003
1004 res = warn_unicode(category, message, stack_level, source);
1005 Py_DECREF(message);
1006 return res;
1007}
1008
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001009int
1010PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
1011 const char *format, ...)
1012{
Victor Stinner914cde82016-03-19 01:03:51 +01001013 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001014 va_list vargs;
1015
1016#ifdef HAVE_STDARG_PROTOTYPES
1017 va_start(vargs, format);
1018#else
1019 va_start(vargs);
1020#endif
Victor Stinner914cde82016-03-19 01:03:51 +01001021 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001022 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +01001023 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001024}
1025
1026int
Victor Stinner914cde82016-03-19 01:03:51 +01001027PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
1028 const char *format, ...)
1029{
1030 int res;
1031 va_list vargs;
1032
1033#ifdef HAVE_STDARG_PROTOTYPES
1034 va_start(vargs, format);
1035#else
1036 va_start(vargs);
1037#endif
1038 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1039 stack_level, format, vargs);
1040 va_end(vargs);
1041 return res;
1042}
1043
1044
1045int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001046PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1047{
1048 int ret;
1049 PyObject *message = PyUnicode_FromString(text);
1050 if (message == NULL)
1051 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001052 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001053 Py_DECREF(message);
1054 return ret;
1055}
1056
Ezio Melotti42da6632011-03-15 05:18:48 +02001057/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001058 Use PyErr_WarnEx instead. */
1059
1060#undef PyErr_Warn
1061
1062PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001063PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001064{
1065 return PyErr_WarnEx(category, text, 1);
1066}
1067
1068/* Warning with explicit origin */
1069int
Victor Stinner14e461d2013-08-26 22:28:21 +02001070PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1071 PyObject *filename, int lineno,
1072 PyObject *module, PyObject *registry)
1073{
1074 PyObject *res;
1075 if (category == NULL)
1076 category = PyExc_RuntimeWarning;
1077 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001078 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001079 if (res == NULL)
1080 return -1;
1081 Py_DECREF(res);
1082 return 0;
1083}
1084
1085int
Christian Heimes33fe8092008-04-13 13:53:33 +00001086PyErr_WarnExplicit(PyObject *category, const char *text,
1087 const char *filename_str, int lineno,
1088 const char *module_str, PyObject *registry)
1089{
Christian Heimes33fe8092008-04-13 13:53:33 +00001090 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001091 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001092 PyObject *module = NULL;
1093 int ret = -1;
1094
1095 if (message == NULL || filename == NULL)
1096 goto exit;
1097 if (module_str != NULL) {
1098 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001099 if (module == NULL)
1100 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001101 }
1102
Victor Stinner14e461d2013-08-26 22:28:21 +02001103 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1104 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001105
1106 exit:
1107 Py_XDECREF(message);
1108 Py_XDECREF(module);
1109 Py_XDECREF(filename);
1110 return ret;
1111}
1112
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001113int
1114PyErr_WarnExplicitFormat(PyObject *category,
1115 const char *filename_str, int lineno,
1116 const char *module_str, PyObject *registry,
1117 const char *format, ...)
1118{
1119 PyObject *message;
1120 PyObject *module = NULL;
1121 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1122 int ret = -1;
1123 va_list vargs;
1124
1125 if (filename == NULL)
1126 goto exit;
1127 if (module_str != NULL) {
1128 module = PyUnicode_FromString(module_str);
1129 if (module == NULL)
1130 goto exit;
1131 }
1132
1133#ifdef HAVE_STDARG_PROTOTYPES
1134 va_start(vargs, format);
1135#else
1136 va_start(vargs);
1137#endif
1138 message = PyUnicode_FromFormatV(format, vargs);
1139 if (message != NULL) {
1140 PyObject *res;
1141 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001142 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001143 Py_DECREF(message);
1144 if (res != NULL) {
1145 Py_DECREF(res);
1146 ret = 0;
1147 }
1148 }
1149 va_end(vargs);
1150exit:
1151 Py_XDECREF(module);
1152 Py_XDECREF(filename);
1153 return ret;
1154}
1155
Christian Heimes33fe8092008-04-13 13:53:33 +00001156
Christian Heimes33fe8092008-04-13 13:53:33 +00001157PyDoc_STRVAR(warn_explicit_doc,
1158"Low-level inferface to warnings functionality.");
1159
1160static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001161 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001162 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1163 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001164 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1165 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001166 /* XXX(brett.cannon): add showwarning? */
1167 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001169};
1170
1171
Victor Stinner747f48e2017-12-12 22:59:48 +01001172#ifndef Py_DEBUG
Christian Heimes33fe8092008-04-13 13:53:33 +00001173static PyObject *
Nick Coghlan9b997472018-01-08 12:45:02 +10001174create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
Christian Heimes33fe8092008-04-13 13:53:33 +00001175{
Nick Coghlan9b997472018-01-08 12:45:02 +10001176 PyObject *modname_obj = NULL;
Victor Stinner82656272017-11-22 23:51:42 +01001177 PyObject *action_str = _PyUnicode_FromId(id);
1178 if (action_str == NULL) {
1179 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001180 }
1181
Nick Coghlan9b997472018-01-08 12:45:02 +10001182 /* Default to "no module name" for initial filter set */
1183 if (modname != NULL) {
1184 modname_obj = PyUnicode_InternFromString(modname);
1185 if (modname_obj == NULL) {
1186 return NULL;
1187 }
1188 } else {
1189 modname_obj = Py_None;
1190 }
1191
Christian Heimes33fe8092008-04-13 13:53:33 +00001192 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001193 return PyTuple_Pack(5, action_str, Py_None,
Nick Coghlan9b997472018-01-08 12:45:02 +10001194 category, modname_obj, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001195}
Victor Stinner747f48e2017-12-12 22:59:48 +01001196#endif
1197
Christian Heimes33fe8092008-04-13 13:53:33 +00001198
1199static PyObject *
Victor Stinner5d862462017-12-19 11:35:58 +01001200init_filters(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001201{
Victor Stinner747f48e2017-12-12 22:59:48 +01001202#ifdef Py_DEBUG
1203 /* Py_DEBUG builds show all warnings by default */
1204 return PyList_New(0);
1205#else
1206 /* Other builds ignore a number of warning categories by default */
Nick Coghlan9b997472018-01-08 12:45:02 +10001207 PyObject *filters = PyList_New(5);
Victor Stinner747f48e2017-12-12 22:59:48 +01001208 if (filters == NULL) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001209 return NULL;
Victor Stinner747f48e2017-12-12 22:59:48 +01001210 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001211
Victor Stinnerb98f1712017-11-23 17:13:44 +01001212 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner747f48e2017-12-12 22:59:48 +01001213 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001214 create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__"));
Victor Stinner747f48e2017-12-12 22:59:48 +01001215 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001216 create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001217 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001218 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001219 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001220 create_filter(PyExc_ImportWarning, &PyId_ignore, NULL));
1221 PyList_SET_ITEM(filters, pos++,
1222 create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001223
Victor Stinnerb98f1712017-11-23 17:13:44 +01001224 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001225 if (PyList_GET_ITEM(filters, x) == NULL) {
1226 Py_DECREF(filters);
1227 return NULL;
1228 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001229 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001230 return filters;
Victor Stinner747f48e2017-12-12 22:59:48 +01001231#endif
Christian Heimes33fe8092008-04-13 13:53:33 +00001232}
1233
Martin v. Löwis1a214512008-06-11 05:26:20 +00001234static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 PyModuleDef_HEAD_INIT,
1236 MODULE_NAME,
1237 warnings__doc__,
1238 0,
1239 warnings_functions,
1240 NULL,
1241 NULL,
1242 NULL,
1243 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001244};
1245
Christian Heimes33fe8092008-04-13 13:53:33 +00001246
Victor Stinner5d862462017-12-19 11:35:58 +01001247PyMODINIT_FUNC
1248_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001249{
Brett Cannon0759dd62009-04-01 18:13:07 +00001250 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001251
Martin v. Löwis1a214512008-06-11 05:26:20 +00001252 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001253 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001254 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001255
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001256 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner5d862462017-12-19 11:35:58 +01001257 _PyRuntime.warnings.filters = init_filters();
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001258 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001259 return NULL;
1260 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001261 Py_INCREF(_PyRuntime.warnings.filters);
1262 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001263 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001264
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001265 if (_PyRuntime.warnings.once_registry == NULL) {
1266 _PyRuntime.warnings.once_registry = PyDict_New();
1267 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001268 return NULL;
1269 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001270 Py_INCREF(_PyRuntime.warnings.once_registry);
1271 if (PyModule_AddObject(m, "_onceregistry",
1272 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001273 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001274
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001275 if (_PyRuntime.warnings.default_action == NULL) {
1276 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1277 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001278 return NULL;
1279 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001280 Py_INCREF(_PyRuntime.warnings.default_action);
1281 if (PyModule_AddObject(m, "_defaultaction",
1282 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001283 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001284
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001285 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001286 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001287}