blob: 16ae932450cc010604a2585dc4947d812a0f0374 [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
Serhiy Storchakaf320be72018-01-25 10:49:40 +020083 (void)_PyObject_LookupAttrId(warnings_module, attr_id, &obj);
Victor Stinnere98445a2016-03-23 00:54:48 +010084 Py_DECREF(warnings_module);
85 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000086}
87
88
Neal Norwitz32dde222008-04-15 06:43:13 +000089static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000090get_once_registry(void)
91{
92 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +010093 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +000094
Victor Stinner82656272017-11-22 23:51:42 +010095 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000096 if (registry == NULL) {
97 if (PyErr_Occurred())
98 return NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +020099 assert(_PyRuntime.warnings.once_registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600100 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +0000101 }
Oren Milman252033d2017-09-11 09:28:39 +0300102 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200103 PyErr_Format(PyExc_TypeError,
104 MODULE_NAME ".onceregistry must be a dict, "
105 "not '%.200s'",
106 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +0300107 Py_DECREF(registry);
108 return NULL;
109 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200110 Py_SETREF(_PyRuntime.warnings.once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000111 return registry;
112}
113
114
Brett Cannon0759dd62009-04-01 18:13:07 +0000115static PyObject *
116get_default_action(void)
117{
118 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100119 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000120
Victor Stinner82656272017-11-22 23:51:42 +0100121 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000122 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (PyErr_Occurred()) {
124 return NULL;
125 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200126 assert(_PyRuntime.warnings.default_action);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600127 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000128 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300129 if (!PyUnicode_Check(default_action)) {
130 PyErr_Format(PyExc_TypeError,
131 MODULE_NAME ".defaultaction must be a string, "
132 "not '%.200s'",
133 Py_TYPE(default_action)->tp_name);
134 Py_DECREF(default_action);
135 return NULL;
136 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200137 Py_SETREF(_PyRuntime.warnings.default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000138 return default_action;
139}
140
141
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400142/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100143static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000144get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
145 PyObject *module, PyObject **item)
146{
Brett Cannon0759dd62009-04-01 18:13:07 +0000147 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000148 Py_ssize_t i;
149 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100150 _Py_IDENTIFIER(filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000151
Victor Stinner82656272017-11-22 23:51:42 +0100152 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000153 if (warnings_filters == NULL) {
154 if (PyErr_Occurred())
155 return NULL;
156 }
157 else {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200158 Py_SETREF(_PyRuntime.warnings.filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000159 }
160
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600161 PyObject *filters = _PyRuntime.warnings.filters;
162 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000163 PyErr_SetString(PyExc_ValueError,
164 MODULE_NAME ".filters must be a list");
165 return NULL;
166 }
167
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600168 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
169 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000170 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
171 Py_ssize_t ln;
172 int is_subclass, good_msg, good_mod;
173
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600174 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400175 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000176 PyErr_Format(PyExc_ValueError,
177 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
178 return NULL;
179 }
180
181 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400182 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000183 action = PyTuple_GET_ITEM(tmp_item, 0);
184 msg = PyTuple_GET_ITEM(tmp_item, 1);
185 cat = PyTuple_GET_ITEM(tmp_item, 2);
186 mod = PyTuple_GET_ITEM(tmp_item, 3);
187 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
188
Oren Milman9d984fd2017-09-12 00:18:09 +0300189 if (!PyUnicode_Check(action)) {
190 PyErr_Format(PyExc_TypeError,
191 "action must be a string, not '%.200s'",
192 Py_TYPE(action)->tp_name);
193 Py_DECREF(tmp_item);
194 return NULL;
195 }
196
Christian Heimes33fe8092008-04-13 13:53:33 +0000197 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400198 if (good_msg == -1) {
199 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100200 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400201 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100202
Christian Heimes33fe8092008-04-13 13:53:33 +0000203 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400204 if (good_mod == -1) {
205 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100206 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400207 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100208
Christian Heimes33fe8092008-04-13 13:53:33 +0000209 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400210 if (is_subclass == -1) {
211 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100212 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400213 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100214
Christian Heimes33fe8092008-04-13 13:53:33 +0000215 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400216 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400217 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000218 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400219 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000220
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400221 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
222 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100223 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400224 }
225
226 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000227 }
228
Brett Cannon0759dd62009-04-01 18:13:07 +0000229 action = get_default_action();
230 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400231 Py_INCREF(Py_None);
232 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100233 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000234 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000235
Christian Heimes33fe8092008-04-13 13:53:33 +0000236 return NULL;
237}
238
Brett Cannon0759dd62009-04-01 18:13:07 +0000239
Christian Heimes33fe8092008-04-13 13:53:33 +0000240static int
241already_warned(PyObject *registry, PyObject *key, int should_set)
242{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200243 PyObject *version_obj, *already_warned;
244 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000245
246 if (key == NULL)
247 return -1;
248
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200249 version_obj = _PyDict_GetItemId(registry, &PyId_version);
250 if (version_obj == NULL
251 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600252 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200253 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600254 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200255 if (version_obj == NULL)
256 return -1;
257 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
258 Py_DECREF(version_obj);
259 return -1;
260 }
261 Py_DECREF(version_obj);
262 }
263 else {
264 already_warned = PyDict_GetItem(registry, key);
265 if (already_warned != NULL) {
266 int rc = PyObject_IsTrue(already_warned);
267 if (rc != 0)
268 return rc;
269 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000270 }
271
272 /* This warning wasn't found in the registry, set it. */
273 if (should_set)
274 return PyDict_SetItem(registry, key, Py_True);
275 return 0;
276}
277
278/* New reference. */
279static PyObject *
280normalize_module(PyObject *filename)
281{
282 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100283 int kind;
284 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000285 Py_ssize_t len;
286
Victor Stinner9e30aa52011-11-21 02:49:52 +0100287 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000288 if (len < 0)
289 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100290
291 if (len == 0)
292 return PyUnicode_FromString("<unknown>");
293
294 kind = PyUnicode_KIND(filename);
295 data = PyUnicode_DATA(filename);
296
297 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000298 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100299 PyUnicode_READ(kind, data, len-3) == '.' &&
300 PyUnicode_READ(kind, data, len-2) == 'p' &&
301 PyUnicode_READ(kind, data, len-1) == 'y')
302 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100303 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000304 }
305 else {
306 module = filename;
307 Py_INCREF(module);
308 }
309 return module;
310}
311
312static int
313update_registry(PyObject *registry, PyObject *text, PyObject *category,
314 int add_zero)
315{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300316 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000317 int rc;
318
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300319 if (add_zero)
320 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000321 else
322 altkey = PyTuple_Pack(2, text, category);
323
324 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000325 Py_XDECREF(altkey);
326 return rc;
327}
328
329static void
Victor Stinner914cde82016-03-19 01:03:51 +0100330show_warning(PyObject *filename, int lineno, PyObject *text,
331 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 PyObject *f_stderr;
334 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000335 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200336 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000337
338 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
339
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200340 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000341 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100342 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000343
Victor Stinnerbd303c12013-11-07 23:07:29 +0100344 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000345 if (f_stderr == NULL) {
346 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100347 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000348 }
349
350 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100351 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
352 goto error;
353 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
354 goto error;
355 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
356 goto error;
357 if (PyFile_WriteString(": ", f_stderr) < 0)
358 goto error;
359 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
360 goto error;
361 if (PyFile_WriteString("\n", f_stderr) < 0)
362 goto error;
363 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000364
365 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000366 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100367 int kind;
368 void *data;
369 Py_ssize_t i, len;
370 Py_UCS4 ch;
371 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000372
Victor Stinnera4c704b2013-10-29 23:43:41 +0100373 if (PyUnicode_READY(sourceline) < 1)
374 goto error;
375
376 kind = PyUnicode_KIND(sourceline);
377 data = PyUnicode_DATA(sourceline);
378 len = PyUnicode_GET_LENGTH(sourceline);
379 for (i=0; i<len; i++) {
380 ch = PyUnicode_READ(kind, data, i);
381 if (ch != ' ' && ch != '\t' && ch != '\014')
382 break;
383 }
384
385 truncated = PyUnicode_Substring(sourceline, i, len);
386 if (truncated == NULL)
387 goto error;
388
389 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
390 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000391 PyFile_WriteString("\n", f_stderr);
392 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200393 else {
394 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
395 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100396
397error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100398 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000399 PyErr_Clear();
400}
401
Victor Stinner1231a462016-03-19 00:47:17 +0100402static int
403call_show_warning(PyObject *category, PyObject *text, PyObject *message,
404 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100405 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100406{
407 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100408 _Py_IDENTIFIER(_showwarnmsg);
409 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100410
Victor Stinnere98445a2016-03-23 00:54:48 +0100411 /* If the source parameter is set, try to get the Python implementation.
412 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600413 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100414 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100415 if (show_fn == NULL) {
416 if (PyErr_Occurred())
417 return -1;
418 show_warning(filename, lineno, text, category, sourceline);
419 return 0;
420 }
421
422 if (!PyCallable_Check(show_fn)) {
423 PyErr_SetString(PyExc_TypeError,
424 "warnings._showwarnmsg() must be set to a callable");
425 goto error;
426 }
427
Victor Stinner82656272017-11-22 23:51:42 +0100428 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100429 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200430 if (!PyErr_Occurred()) {
431 PyErr_SetString(PyExc_RuntimeError,
432 "unable to get warnings.WarningMessage");
433 }
Victor Stinner1231a462016-03-19 00:47:17 +0100434 goto error;
435 }
436
437 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100438 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100439 NULL);
440 Py_DECREF(warnmsg_cls);
441 if (msg == NULL)
442 goto error;
443
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100444 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100445 Py_DECREF(show_fn);
446 Py_DECREF(msg);
447
448 if (res == NULL)
449 return -1;
450
451 Py_DECREF(res);
452 return 0;
453
454error:
455 Py_XDECREF(show_fn);
456 return -1;
457}
458
Christian Heimes33fe8092008-04-13 13:53:33 +0000459static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000461 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100462 PyObject *module, PyObject *registry, PyObject *sourceline,
463 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000464{
465 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400466 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100467 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000468 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100470 /* module can be None if a warning is emitted late during Python shutdown.
471 In this case, the Python warnings module was probably unloaded, filters
472 are no more available to choose as action. It is safer to ignore the
473 warning and do nothing. */
474 if (module == Py_None)
475 Py_RETURN_NONE;
476
Brett Cannondb734912008-06-27 00:52:15 +0000477 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300478 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000479 return NULL;
480 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000481
482 /* Normalize module. */
483 if (module == NULL) {
484 module = normalize_module(filename);
485 if (module == NULL)
486 return NULL;
487 }
488 else
489 Py_INCREF(module);
490
491 /* Normalize message. */
492 Py_INCREF(message); /* DECREF'ed in cleanup. */
493 rc = PyObject_IsInstance(message, PyExc_Warning);
494 if (rc == -1) {
495 goto cleanup;
496 }
497 if (rc == 1) {
498 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000499 if (text == NULL)
500 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000501 category = (PyObject*)message->ob_type;
502 }
503 else {
504 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100505 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000506 if (message == NULL)
507 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000508 }
509
510 lineno_obj = PyLong_FromLong(lineno);
511 if (lineno_obj == NULL)
512 goto cleanup;
513
Victor Stinner22f18752016-12-09 18:08:18 +0100514 if (source == Py_None) {
515 source = NULL;
516 }
517
Christian Heimes33fe8092008-04-13 13:53:33 +0000518 /* Create key. */
519 key = PyTuple_Pack(3, text, category, lineno_obj);
520 if (key == NULL)
521 goto cleanup;
522
Brett Cannondb734912008-06-27 00:52:15 +0000523 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000524 rc = already_warned(registry, key, 0);
525 if (rc == -1)
526 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000528 goto return_none;
529 /* Else this warning hasn't been generated before. */
530 }
531
532 action = get_filter(category, text, lineno, module, &item);
533 if (action == NULL)
534 goto cleanup;
535
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200536 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000537 PyErr_SetObject(category, message);
538 goto cleanup;
539 }
540
Victor Stinnerc9758782017-11-27 16:57:07 +0100541 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
542 goto return_none;
543 }
544
Christian Heimes33fe8092008-04-13 13:53:33 +0000545 /* Store in the registry that we've been here, *except* when the action
546 is "always". */
547 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200548 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000549 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100550 PyDict_SetItem(registry, key, Py_True) < 0)
551 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000552 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100553 }
554
555 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000556 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000557 registry = get_once_registry();
558 if (registry == NULL)
559 goto cleanup;
560 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600561 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000563 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200564 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000565 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000566 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000568 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200569 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000570 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100571 "Unrecognized action (%R) in warnings.filters:\n %R",
572 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000573 goto cleanup;
574 }
575 }
576
Christian Heimes1a8501c2008-10-02 19:56:01 +0000577 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000578 goto return_none;
579 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100580 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100581 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100582 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000583 }
584 else /* if (rc == -1) */
585 goto cleanup;
586
587 return_none:
588 result = Py_None;
589 Py_INCREF(result);
590
591 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400592 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000593 Py_XDECREF(key);
594 Py_XDECREF(text);
595 Py_XDECREF(lineno_obj);
596 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000597 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000598 return result; /* Py_None or NULL. */
599}
600
Larry Hastings714e4932015-09-06 00:39:37 -0700601static int
602is_internal_frame(PyFrameObject *frame)
603{
604 static PyObject *importlib_string = NULL;
605 static PyObject *bootstrap_string = NULL;
606 PyObject *filename;
607 int contains;
608
609 if (importlib_string == NULL) {
610 importlib_string = PyUnicode_FromString("importlib");
611 if (importlib_string == NULL) {
612 return 0;
613 }
614
615 bootstrap_string = PyUnicode_FromString("_bootstrap");
616 if (bootstrap_string == NULL) {
617 Py_DECREF(importlib_string);
618 return 0;
619 }
620 Py_INCREF(importlib_string);
621 Py_INCREF(bootstrap_string);
622 }
623
624 if (frame == NULL || frame->f_code == NULL ||
625 frame->f_code->co_filename == NULL) {
626 return 0;
627 }
628 filename = frame->f_code->co_filename;
629 if (!PyUnicode_Check(filename)) {
630 return 0;
631 }
632 contains = PyUnicode_Contains(filename, importlib_string);
633 if (contains < 0) {
634 return 0;
635 }
636 else if (contains > 0) {
637 contains = PyUnicode_Contains(filename, bootstrap_string);
638 if (contains < 0) {
639 return 0;
640 }
641 else if (contains > 0) {
642 return 1;
643 }
644 }
645
646 return 0;
647}
648
649static PyFrameObject *
650next_external_frame(PyFrameObject *frame)
651{
652 do {
653 frame = frame->f_back;
654 } while (frame != NULL && is_internal_frame(frame));
655
656 return frame;
657}
658
Christian Heimes33fe8092008-04-13 13:53:33 +0000659/* filename, module, and registry are new refs, globals is borrowed */
660/* Returns 0 on error (no new refs), 1 on success */
661static int
662setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
663 PyObject **module, PyObject **registry)
664{
665 PyObject *globals;
666
667 /* Setup globals and lineno. */
668 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700669 // Stack level comparisons to Python code is off by one as there is no
670 // warnings-related stack level to avoid.
671 if (stack_level <= 0 || is_internal_frame(f)) {
672 while (--stack_level > 0 && f != NULL) {
673 f = f->f_back;
674 }
675 }
676 else {
677 while (--stack_level > 0 && f != NULL) {
678 f = next_external_frame(f);
679 }
680 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000681
682 if (f == NULL) {
683 globals = PyThreadState_Get()->interp->sysdict;
684 *lineno = 1;
685 }
686 else {
687 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000688 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000689 }
690
691 *module = NULL;
692
693 /* Setup registry. */
694 assert(globals != NULL);
695 assert(PyDict_Check(globals));
696 *registry = PyDict_GetItemString(globals, "__warningregistry__");
697 if (*registry == NULL) {
698 int rc;
699
700 *registry = PyDict_New();
701 if (*registry == NULL)
702 return 0;
703
704 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
705 if (rc < 0)
706 goto handle_error;
707 }
708 else
709 Py_INCREF(*registry);
710
711 /* Setup module. */
712 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300713 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
714 Py_INCREF(*module);
715 }
716 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000717 *module = PyUnicode_FromString("<string>");
718 if (*module == NULL)
719 goto handle_error;
720 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000721
722 /* Setup filename. */
723 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200724 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200725 Py_ssize_t len;
726 int kind;
727 void *data;
728
729 if (PyUnicode_READY(*filename))
730 goto handle_error;
731
Victor Stinner9e30aa52011-11-21 02:49:52 +0100732 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200733 kind = PyUnicode_KIND(*filename);
734 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000735
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500736#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400737 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000738 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200739 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500740 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
741 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400742 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000743 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200744 *filename = PyUnicode_Substring(*filename, 0,
745 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000746 if (*filename == NULL)
747 goto handle_error;
748 }
749 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000750 Py_INCREF(*filename);
751 }
752 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500753 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200754 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100755 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100756 /* PyList_Check() is needed because sys.argv is set to None during
757 Python finalization */
758 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000759 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000760 *filename = PyList_GetItem(argv, 0);
761 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000762 /* If sys.argv[0] is false, then use '__main__'. */
763 is_true = PyObject_IsTrue(*filename);
764 if (is_true < 0) {
765 Py_DECREF(*filename);
766 goto handle_error;
767 }
768 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300769 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000770 if (*filename == NULL)
771 goto handle_error;
772 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000773 }
774 else {
775 /* embedded interpreters don't have sys.argv, see bug #839151 */
776 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100777 if (*filename == NULL)
778 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000779 }
780 }
781 if (*filename == NULL) {
782 *filename = *module;
783 Py_INCREF(*filename);
784 }
785 }
786
787 return 1;
788
789 handle_error:
790 /* filename not XDECREF'ed here as there is no way to jump here with a
791 dangling reference. */
792 Py_XDECREF(*registry);
793 Py_XDECREF(*module);
794 return 0;
795}
796
797static PyObject *
798get_category(PyObject *message, PyObject *category)
799{
800 int rc;
801
802 /* Get category. */
803 rc = PyObject_IsInstance(message, PyExc_Warning);
804 if (rc == -1)
805 return NULL;
806
807 if (rc == 1)
808 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300809 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000810 category = PyExc_UserWarning;
811
812 /* Validate category. */
813 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300814 /* category is not a subclass of PyExc_Warning or
815 PyObject_IsSubclass raised an error */
816 if (rc == -1 || rc == 0) {
817 PyErr_Format(PyExc_TypeError,
818 "category must be a Warning subclass, not '%s'",
819 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000820 return NULL;
821 }
822
823 return category;
824}
825
826static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100827do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
828 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000829{
830 PyObject *filename, *module, *registry, *res;
831 int lineno;
832
833 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
834 return NULL;
835
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100836 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100837 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000838 Py_DECREF(filename);
839 Py_DECREF(registry);
840 Py_DECREF(module);
841 return res;
842}
843
Victor Stinner22f18752016-12-09 18:08:18 +0100844/*[clinic input]
845warn as warnings_warn
846
847 message: object
848 category: object = None
849 stacklevel: Py_ssize_t = 1
850 source: object = None
851
852Issue a warning, or maybe ignore it or raise an exception.
853[clinic start generated code]*/
854
Christian Heimes33fe8092008-04-13 13:53:33 +0000855static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100856warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
857 Py_ssize_t stacklevel, PyObject *source)
858/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000859{
Christian Heimes33fe8092008-04-13 13:53:33 +0000860 category = get_category(message, category);
861 if (category == NULL)
862 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100863 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000864}
865
866static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200867get_source_line(PyObject *module_globals, int lineno)
868{
869 _Py_IDENTIFIER(get_source);
870 _Py_IDENTIFIER(__loader__);
871 _Py_IDENTIFIER(__name__);
872 PyObject *loader;
873 PyObject *module_name;
874 PyObject *get_source;
875 PyObject *source;
876 PyObject *source_list;
877 PyObject *source_line;
878
879 /* Check/get the requisite pieces needed for the loader. */
880 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
881 if (loader == NULL) {
882 return NULL;
883 }
884 Py_INCREF(loader);
885 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
886 if (!module_name) {
887 Py_DECREF(loader);
888 return NULL;
889 }
890 Py_INCREF(module_name);
891
892 /* Make sure the loader implements the optional get_source() method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200893 (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200894 Py_DECREF(loader);
895 if (!get_source) {
896 Py_DECREF(module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200897 return NULL;
898 }
899 /* Call get_source() to get the source code. */
900 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
901 Py_DECREF(get_source);
902 Py_DECREF(module_name);
903 if (!source) {
904 return NULL;
905 }
906 if (source == Py_None) {
907 Py_DECREF(source);
908 return NULL;
909 }
910
911 /* Split the source into lines. */
912 source_list = PyUnicode_Splitlines(source, 0);
913 Py_DECREF(source);
914 if (!source_list) {
915 return NULL;
916 }
917
918 /* Get the source line. */
919 source_line = PyList_GetItem(source_list, lineno-1);
920 Py_XINCREF(source_line);
921 Py_DECREF(source_list);
922 return source_line;
923}
924
925static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000926warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
927{
928 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100929 "module", "registry", "module_globals",
930 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000931 PyObject *message;
932 PyObject *category;
933 PyObject *filename;
934 int lineno;
935 PyObject *module = NULL;
936 PyObject *registry = NULL;
937 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100938 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200939 PyObject *source_line = NULL;
940 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000941
Victor Stinner914cde82016-03-19 01:03:51 +0100942 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000943 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100944 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000945 return NULL;
946
947 if (module_globals) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200948 source_line = get_source_line(module_globals, lineno);
949 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000950 return NULL;
951 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000952 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200953 returned = warn_explicit(category, message, filename, lineno, module,
954 registry, source_line, sourceobj);
955 Py_XDECREF(source_line);
956 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000957}
958
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200959static PyObject *
960warnings_filters_mutated(PyObject *self, PyObject *args)
961{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600962 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200963 Py_RETURN_NONE;
964}
965
Christian Heimes33fe8092008-04-13 13:53:33 +0000966
967/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000968
969static int
970warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100971 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000972{
973 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000974
975 if (category == NULL)
976 category = PyExc_RuntimeWarning;
977
Victor Stinner914cde82016-03-19 01:03:51 +0100978 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000979 if (res == NULL)
980 return -1;
981 Py_DECREF(res);
982
983 return 0;
984}
985
Victor Stinner914cde82016-03-19 01:03:51 +0100986static int
987_PyErr_WarnFormatV(PyObject *source,
988 PyObject *category, Py_ssize_t stack_level,
989 const char *format, va_list vargs)
990{
991 PyObject *message;
992 int res;
993
994 message = PyUnicode_FromFormatV(format, vargs);
995 if (message == NULL)
996 return -1;
997
998 res = warn_unicode(category, message, stack_level, source);
999 Py_DECREF(message);
1000 return res;
1001}
1002
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001003int
1004PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
1005 const char *format, ...)
1006{
Victor Stinner914cde82016-03-19 01:03:51 +01001007 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001008 va_list vargs;
1009
1010#ifdef HAVE_STDARG_PROTOTYPES
1011 va_start(vargs, format);
1012#else
1013 va_start(vargs);
1014#endif
Victor Stinner914cde82016-03-19 01:03:51 +01001015 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001016 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +01001017 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001018}
1019
1020int
Victor Stinner914cde82016-03-19 01:03:51 +01001021PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
1022 const char *format, ...)
1023{
1024 int res;
1025 va_list vargs;
1026
1027#ifdef HAVE_STDARG_PROTOTYPES
1028 va_start(vargs, format);
1029#else
1030 va_start(vargs);
1031#endif
1032 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1033 stack_level, format, vargs);
1034 va_end(vargs);
1035 return res;
1036}
1037
1038
1039int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001040PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1041{
1042 int ret;
1043 PyObject *message = PyUnicode_FromString(text);
1044 if (message == NULL)
1045 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001046 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001047 Py_DECREF(message);
1048 return ret;
1049}
1050
Ezio Melotti42da6632011-03-15 05:18:48 +02001051/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001052 Use PyErr_WarnEx instead. */
1053
1054#undef PyErr_Warn
1055
1056PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001057PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001058{
1059 return PyErr_WarnEx(category, text, 1);
1060}
1061
1062/* Warning with explicit origin */
1063int
Victor Stinner14e461d2013-08-26 22:28:21 +02001064PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1065 PyObject *filename, int lineno,
1066 PyObject *module, PyObject *registry)
1067{
1068 PyObject *res;
1069 if (category == NULL)
1070 category = PyExc_RuntimeWarning;
1071 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001072 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001073 if (res == NULL)
1074 return -1;
1075 Py_DECREF(res);
1076 return 0;
1077}
1078
1079int
Christian Heimes33fe8092008-04-13 13:53:33 +00001080PyErr_WarnExplicit(PyObject *category, const char *text,
1081 const char *filename_str, int lineno,
1082 const char *module_str, PyObject *registry)
1083{
Christian Heimes33fe8092008-04-13 13:53:33 +00001084 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001085 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001086 PyObject *module = NULL;
1087 int ret = -1;
1088
1089 if (message == NULL || filename == NULL)
1090 goto exit;
1091 if (module_str != NULL) {
1092 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001093 if (module == NULL)
1094 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001095 }
1096
Victor Stinner14e461d2013-08-26 22:28:21 +02001097 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1098 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001099
1100 exit:
1101 Py_XDECREF(message);
1102 Py_XDECREF(module);
1103 Py_XDECREF(filename);
1104 return ret;
1105}
1106
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001107int
1108PyErr_WarnExplicitFormat(PyObject *category,
1109 const char *filename_str, int lineno,
1110 const char *module_str, PyObject *registry,
1111 const char *format, ...)
1112{
1113 PyObject *message;
1114 PyObject *module = NULL;
1115 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1116 int ret = -1;
1117 va_list vargs;
1118
1119 if (filename == NULL)
1120 goto exit;
1121 if (module_str != NULL) {
1122 module = PyUnicode_FromString(module_str);
1123 if (module == NULL)
1124 goto exit;
1125 }
1126
1127#ifdef HAVE_STDARG_PROTOTYPES
1128 va_start(vargs, format);
1129#else
1130 va_start(vargs);
1131#endif
1132 message = PyUnicode_FromFormatV(format, vargs);
1133 if (message != NULL) {
1134 PyObject *res;
1135 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001136 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001137 Py_DECREF(message);
1138 if (res != NULL) {
1139 Py_DECREF(res);
1140 ret = 0;
1141 }
1142 }
1143 va_end(vargs);
1144exit:
1145 Py_XDECREF(module);
1146 Py_XDECREF(filename);
1147 return ret;
1148}
1149
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001150void
1151_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
1152{
1153 /* First, we attempt to funnel the warning through
1154 warnings._warn_unawaited_coroutine.
1155
1156 This could raise an exception, due to:
1157 - a bug
1158 - some kind of shutdown-related brokenness
1159 - succeeding, but with an "error" warning filter installed, so the
1160 warning is converted into a RuntimeWarning exception
1161
1162 In the first two cases, we want to print the error (so we know what it
1163 is!), and then print a warning directly as a fallback. In the last
1164 case, we want to print the error (since it's the warning!), but *not*
1165 do a fallback. And after we print the error we can't check for what
1166 type of error it was (because PyErr_WriteUnraisable clears it), so we
1167 need a flag to keep track.
1168
1169 Since this is called from __del__ context, it's careful to never raise
1170 an exception.
1171 */
1172 _Py_IDENTIFIER(_warn_unawaited_coroutine);
1173 int warned = 0;
1174 PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1);
1175 if (fn) {
1176 PyObject *res = PyObject_CallFunctionObjArgs(fn, coro, NULL);
1177 Py_DECREF(fn);
1178 if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) {
1179 warned = 1;
1180 }
1181 Py_XDECREF(res);
1182 }
1183
1184 if (PyErr_Occurred()) {
1185 PyErr_WriteUnraisable(coro);
1186 }
1187 if (!warned) {
Yury Selivanov35103342018-01-21 20:47:04 -05001188 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
1189 "coroutine '%.50S' was never awaited",
1190 ((PyCoroObject *)coro)->cr_qualname) < 0)
1191 {
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001192 PyErr_WriteUnraisable(coro);
1193 }
1194 }
1195}
Christian Heimes33fe8092008-04-13 13:53:33 +00001196
Christian Heimes33fe8092008-04-13 13:53:33 +00001197PyDoc_STRVAR(warn_explicit_doc,
1198"Low-level inferface to warnings functionality.");
1199
1200static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001201 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001202 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1203 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001204 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1205 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001206 /* XXX(brett.cannon): add showwarning? */
1207 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001209};
1210
1211
Victor Stinner747f48e2017-12-12 22:59:48 +01001212#ifndef Py_DEBUG
Christian Heimes33fe8092008-04-13 13:53:33 +00001213static PyObject *
Nick Coghlan9b997472018-01-08 12:45:02 +10001214create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
Christian Heimes33fe8092008-04-13 13:53:33 +00001215{
Nick Coghlan9b997472018-01-08 12:45:02 +10001216 PyObject *modname_obj = NULL;
Victor Stinner82656272017-11-22 23:51:42 +01001217 PyObject *action_str = _PyUnicode_FromId(id);
1218 if (action_str == NULL) {
1219 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001220 }
1221
Nick Coghlan9b997472018-01-08 12:45:02 +10001222 /* Default to "no module name" for initial filter set */
1223 if (modname != NULL) {
1224 modname_obj = PyUnicode_InternFromString(modname);
1225 if (modname_obj == NULL) {
1226 return NULL;
1227 }
1228 } else {
1229 modname_obj = Py_None;
1230 }
1231
Christian Heimes33fe8092008-04-13 13:53:33 +00001232 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001233 return PyTuple_Pack(5, action_str, Py_None,
Nick Coghlan9b997472018-01-08 12:45:02 +10001234 category, modname_obj, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001235}
Victor Stinner747f48e2017-12-12 22:59:48 +01001236#endif
1237
Christian Heimes33fe8092008-04-13 13:53:33 +00001238
1239static PyObject *
Victor Stinner5d862462017-12-19 11:35:58 +01001240init_filters(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001241{
Victor Stinner747f48e2017-12-12 22:59:48 +01001242#ifdef Py_DEBUG
1243 /* Py_DEBUG builds show all warnings by default */
1244 return PyList_New(0);
1245#else
1246 /* Other builds ignore a number of warning categories by default */
Nick Coghlan9b997472018-01-08 12:45:02 +10001247 PyObject *filters = PyList_New(5);
Victor Stinner747f48e2017-12-12 22:59:48 +01001248 if (filters == NULL) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001249 return NULL;
Victor Stinner747f48e2017-12-12 22:59:48 +01001250 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001251
Victor Stinnerb98f1712017-11-23 17:13:44 +01001252 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner747f48e2017-12-12 22:59:48 +01001253 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001254 create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__"));
Victor Stinner747f48e2017-12-12 22:59:48 +01001255 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001256 create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001257 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001258 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL));
Victor Stinner747f48e2017-12-12 22:59:48 +01001259 PyList_SET_ITEM(filters, pos++,
Nick Coghlan9b997472018-01-08 12:45:02 +10001260 create_filter(PyExc_ImportWarning, &PyId_ignore, NULL));
1261 PyList_SET_ITEM(filters, pos++,
1262 create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001263
Victor Stinnerb98f1712017-11-23 17:13:44 +01001264 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001265 if (PyList_GET_ITEM(filters, x) == NULL) {
1266 Py_DECREF(filters);
1267 return NULL;
1268 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001269 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001270 return filters;
Victor Stinner747f48e2017-12-12 22:59:48 +01001271#endif
Christian Heimes33fe8092008-04-13 13:53:33 +00001272}
1273
Martin v. Löwis1a214512008-06-11 05:26:20 +00001274static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 PyModuleDef_HEAD_INIT,
1276 MODULE_NAME,
1277 warnings__doc__,
1278 0,
1279 warnings_functions,
1280 NULL,
1281 NULL,
1282 NULL,
1283 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001284};
1285
Christian Heimes33fe8092008-04-13 13:53:33 +00001286
Victor Stinner5d862462017-12-19 11:35:58 +01001287PyMODINIT_FUNC
1288_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001289{
Brett Cannon0759dd62009-04-01 18:13:07 +00001290 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001291
Martin v. Löwis1a214512008-06-11 05:26:20 +00001292 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001293 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001294 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001295
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001296 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner5d862462017-12-19 11:35:58 +01001297 _PyRuntime.warnings.filters = init_filters();
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001298 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001299 return NULL;
1300 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001301 Py_INCREF(_PyRuntime.warnings.filters);
1302 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001303 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001304
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001305 if (_PyRuntime.warnings.once_registry == NULL) {
1306 _PyRuntime.warnings.once_registry = PyDict_New();
1307 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001308 return NULL;
1309 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001310 Py_INCREF(_PyRuntime.warnings.once_registry);
1311 if (PyModule_AddObject(m, "_onceregistry",
1312 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001313 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001314
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001315 if (_PyRuntime.warnings.default_action == NULL) {
1316 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1317 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001318 return NULL;
1319 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001320 Py_INCREF(_PyRuntime.warnings.default_action);
1321 if (PyModule_AddObject(m, "_defaultaction",
1322 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001323 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001324
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001325 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001326 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001327}