blob: 086a70d7e68f0a508134c6dbe0b684cda506d1c3 [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 Stinnerb98f1712017-11-23 17:13:44 +010014_Py_IDENTIFIER(ignore);
15_Py_IDENTIFIER(error);
16_Py_IDENTIFIER(always);
17_Py_static_string(PyId_default, "default");
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
26 if (obj == Py_None)
27 return 1;
Victor Stinner55ba38a2016-12-09 16:09:30 +010028 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000029 if (result == NULL)
30 return -1;
31
32 rc = PyObject_IsTrue(result);
33 Py_DECREF(result);
34 return rc;
35}
36
37/*
38 Returns a new reference.
39 A NULL return value can mean false or an error.
40*/
41static PyObject *
Victor Stinner82656272017-11-22 23:51:42 +010042get_warnings_attr(_Py_Identifier *attr_id, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000043{
Victor Stinner82656272017-11-22 23:51:42 +010044 PyObject *warnings_str;
Victor Stinnere98445a2016-03-23 00:54:48 +010045 PyObject *warnings_module, *obj;
Victor Stinner82656272017-11-22 23:51:42 +010046 _Py_IDENTIFIER(warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000047
Victor Stinner82656272017-11-22 23:51:42 +010048 warnings_str = _PyUnicode_FromId(&PyId_warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000049 if (warnings_str == NULL) {
Victor Stinner82656272017-11-22 23:51:42 +010050 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +000051 }
52
Victor Stinnere98445a2016-03-23 00:54:48 +010053 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060054 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010055 warnings_module = PyImport_Import(warnings_str);
56 if (warnings_module == NULL) {
57 /* Fallback to the C implementation if we cannot get
58 the Python implementation */
Serhiy Storchakad4f84802017-11-11 15:19:47 +020059 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
60 PyErr_Clear();
61 }
Christian Heimes33fe8092008-04-13 13:53:33 +000062 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010063 }
64 }
65 else {
Eric Snow3f9eee62017-09-15 16:35:20 -060066 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010067 if (warnings_module == NULL)
68 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010069 }
70
Victor Stinner82656272017-11-22 23:51:42 +010071 obj = _PyObject_GetAttrId(warnings_module, attr_id);
Victor Stinnere98445a2016-03-23 00:54:48 +010072 Py_DECREF(warnings_module);
Serhiy Storchakad4f84802017-11-11 15:19:47 +020073 if (obj == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
74 PyErr_Clear();
75 }
Victor Stinnere98445a2016-03-23 00:54:48 +010076 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000077}
78
79
Neal Norwitz32dde222008-04-15 06:43:13 +000080static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000081get_once_registry(void)
82{
83 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +010084 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +000085
Victor Stinner82656272017-11-22 23:51:42 +010086 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000087 if (registry == NULL) {
88 if (PyErr_Occurred())
89 return NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +020090 assert(_PyRuntime.warnings.once_registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060091 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000092 }
Oren Milman252033d2017-09-11 09:28:39 +030093 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +020094 PyErr_Format(PyExc_TypeError,
95 MODULE_NAME ".onceregistry must be a dict, "
96 "not '%.200s'",
97 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +030098 Py_DECREF(registry);
99 return NULL;
100 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200101 Py_SETREF(_PyRuntime.warnings.once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000102 return registry;
103}
104
105
Brett Cannon0759dd62009-04-01 18:13:07 +0000106static PyObject *
107get_default_action(void)
108{
109 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100110 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000111
Victor Stinner82656272017-11-22 23:51:42 +0100112 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000113 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 if (PyErr_Occurred()) {
115 return NULL;
116 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200117 assert(_PyRuntime.warnings.default_action);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600118 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000119 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300120 if (!PyUnicode_Check(default_action)) {
121 PyErr_Format(PyExc_TypeError,
122 MODULE_NAME ".defaultaction must be a string, "
123 "not '%.200s'",
124 Py_TYPE(default_action)->tp_name);
125 Py_DECREF(default_action);
126 return NULL;
127 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200128 Py_SETREF(_PyRuntime.warnings.default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000129 return default_action;
130}
131
132
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400133/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100134static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000135get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
136 PyObject *module, PyObject **item)
137{
Brett Cannon0759dd62009-04-01 18:13:07 +0000138 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000139 Py_ssize_t i;
140 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100141 _Py_IDENTIFIER(filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000142
Victor Stinner82656272017-11-22 23:51:42 +0100143 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000144 if (warnings_filters == NULL) {
145 if (PyErr_Occurred())
146 return NULL;
147 }
148 else {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200149 Py_SETREF(_PyRuntime.warnings.filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000150 }
151
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600152 PyObject *filters = _PyRuntime.warnings.filters;
153 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000154 PyErr_SetString(PyExc_ValueError,
155 MODULE_NAME ".filters must be a list");
156 return NULL;
157 }
158
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600159 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
160 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000161 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
162 Py_ssize_t ln;
163 int is_subclass, good_msg, good_mod;
164
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600165 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400166 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000167 PyErr_Format(PyExc_ValueError,
168 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
169 return NULL;
170 }
171
172 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400173 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000174 action = PyTuple_GET_ITEM(tmp_item, 0);
175 msg = PyTuple_GET_ITEM(tmp_item, 1);
176 cat = PyTuple_GET_ITEM(tmp_item, 2);
177 mod = PyTuple_GET_ITEM(tmp_item, 3);
178 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
179
Oren Milman9d984fd2017-09-12 00:18:09 +0300180 if (!PyUnicode_Check(action)) {
181 PyErr_Format(PyExc_TypeError,
182 "action must be a string, not '%.200s'",
183 Py_TYPE(action)->tp_name);
184 Py_DECREF(tmp_item);
185 return NULL;
186 }
187
Christian Heimes33fe8092008-04-13 13:53:33 +0000188 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400189 if (good_msg == -1) {
190 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100191 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400192 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100193
Christian Heimes33fe8092008-04-13 13:53:33 +0000194 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400195 if (good_mod == -1) {
196 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100197 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400198 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100199
Christian Heimes33fe8092008-04-13 13:53:33 +0000200 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400201 if (is_subclass == -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 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400207 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400208 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000209 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400210 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000211
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400212 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
213 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100214 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400215 }
216
217 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000218 }
219
Brett Cannon0759dd62009-04-01 18:13:07 +0000220 action = get_default_action();
221 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400222 Py_INCREF(Py_None);
223 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100224 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000225 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000226
Christian Heimes33fe8092008-04-13 13:53:33 +0000227 return NULL;
228}
229
Brett Cannon0759dd62009-04-01 18:13:07 +0000230
Christian Heimes33fe8092008-04-13 13:53:33 +0000231static int
232already_warned(PyObject *registry, PyObject *key, int should_set)
233{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200234 PyObject *version_obj, *already_warned;
235 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000236
237 if (key == NULL)
238 return -1;
239
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200240 version_obj = _PyDict_GetItemId(registry, &PyId_version);
241 if (version_obj == NULL
242 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600243 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200244 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600245 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200246 if (version_obj == NULL)
247 return -1;
248 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
249 Py_DECREF(version_obj);
250 return -1;
251 }
252 Py_DECREF(version_obj);
253 }
254 else {
255 already_warned = PyDict_GetItem(registry, key);
256 if (already_warned != NULL) {
257 int rc = PyObject_IsTrue(already_warned);
258 if (rc != 0)
259 return rc;
260 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000261 }
262
263 /* This warning wasn't found in the registry, set it. */
264 if (should_set)
265 return PyDict_SetItem(registry, key, Py_True);
266 return 0;
267}
268
269/* New reference. */
270static PyObject *
271normalize_module(PyObject *filename)
272{
273 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100274 int kind;
275 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000276 Py_ssize_t len;
277
Victor Stinner9e30aa52011-11-21 02:49:52 +0100278 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000279 if (len < 0)
280 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100281
282 if (len == 0)
283 return PyUnicode_FromString("<unknown>");
284
285 kind = PyUnicode_KIND(filename);
286 data = PyUnicode_DATA(filename);
287
288 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000289 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100290 PyUnicode_READ(kind, data, len-3) == '.' &&
291 PyUnicode_READ(kind, data, len-2) == 'p' &&
292 PyUnicode_READ(kind, data, len-1) == 'y')
293 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100294 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000295 }
296 else {
297 module = filename;
298 Py_INCREF(module);
299 }
300 return module;
301}
302
303static int
304update_registry(PyObject *registry, PyObject *text, PyObject *category,
305 int add_zero)
306{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300307 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000308 int rc;
309
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300310 if (add_zero)
311 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000312 else
313 altkey = PyTuple_Pack(2, text, category);
314
315 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000316 Py_XDECREF(altkey);
317 return rc;
318}
319
320static void
Victor Stinner914cde82016-03-19 01:03:51 +0100321show_warning(PyObject *filename, int lineno, PyObject *text,
322 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyObject *f_stderr;
325 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000326 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200327 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000328
329 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
330
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200331 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000332 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100333 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000334
Victor Stinnerbd303c12013-11-07 23:07:29 +0100335 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000336 if (f_stderr == NULL) {
337 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100338 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000339 }
340
341 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100342 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
343 goto error;
344 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
345 goto error;
346 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
347 goto error;
348 if (PyFile_WriteString(": ", f_stderr) < 0)
349 goto error;
350 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
351 goto error;
352 if (PyFile_WriteString("\n", f_stderr) < 0)
353 goto error;
354 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000355
356 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000357 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100358 int kind;
359 void *data;
360 Py_ssize_t i, len;
361 Py_UCS4 ch;
362 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000363
Victor Stinnera4c704b2013-10-29 23:43:41 +0100364 if (PyUnicode_READY(sourceline) < 1)
365 goto error;
366
367 kind = PyUnicode_KIND(sourceline);
368 data = PyUnicode_DATA(sourceline);
369 len = PyUnicode_GET_LENGTH(sourceline);
370 for (i=0; i<len; i++) {
371 ch = PyUnicode_READ(kind, data, i);
372 if (ch != ' ' && ch != '\t' && ch != '\014')
373 break;
374 }
375
376 truncated = PyUnicode_Substring(sourceline, i, len);
377 if (truncated == NULL)
378 goto error;
379
380 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
381 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000382 PyFile_WriteString("\n", f_stderr);
383 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200384 else {
385 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
386 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100387
388error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100389 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000390 PyErr_Clear();
391}
392
Victor Stinner1231a462016-03-19 00:47:17 +0100393static int
394call_show_warning(PyObject *category, PyObject *text, PyObject *message,
395 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100396 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100397{
398 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100399 _Py_IDENTIFIER(_showwarnmsg);
400 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100401
Victor Stinnere98445a2016-03-23 00:54:48 +0100402 /* If the source parameter is set, try to get the Python implementation.
403 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600404 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100405 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100406 if (show_fn == NULL) {
407 if (PyErr_Occurred())
408 return -1;
409 show_warning(filename, lineno, text, category, sourceline);
410 return 0;
411 }
412
413 if (!PyCallable_Check(show_fn)) {
414 PyErr_SetString(PyExc_TypeError,
415 "warnings._showwarnmsg() must be set to a callable");
416 goto error;
417 }
418
Victor Stinner82656272017-11-22 23:51:42 +0100419 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100420 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200421 if (!PyErr_Occurred()) {
422 PyErr_SetString(PyExc_RuntimeError,
423 "unable to get warnings.WarningMessage");
424 }
Victor Stinner1231a462016-03-19 00:47:17 +0100425 goto error;
426 }
427
428 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100429 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100430 NULL);
431 Py_DECREF(warnmsg_cls);
432 if (msg == NULL)
433 goto error;
434
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100435 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100436 Py_DECREF(show_fn);
437 Py_DECREF(msg);
438
439 if (res == NULL)
440 return -1;
441
442 Py_DECREF(res);
443 return 0;
444
445error:
446 Py_XDECREF(show_fn);
447 return -1;
448}
449
Christian Heimes33fe8092008-04-13 13:53:33 +0000450static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000452 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100453 PyObject *module, PyObject *registry, PyObject *sourceline,
454 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000455{
456 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400457 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100458 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000459 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100461 /* module can be None if a warning is emitted late during Python shutdown.
462 In this case, the Python warnings module was probably unloaded, filters
463 are no more available to choose as action. It is safer to ignore the
464 warning and do nothing. */
465 if (module == Py_None)
466 Py_RETURN_NONE;
467
Brett Cannondb734912008-06-27 00:52:15 +0000468 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300469 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000470 return NULL;
471 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000472
473 /* Normalize module. */
474 if (module == NULL) {
475 module = normalize_module(filename);
476 if (module == NULL)
477 return NULL;
478 }
479 else
480 Py_INCREF(module);
481
482 /* Normalize message. */
483 Py_INCREF(message); /* DECREF'ed in cleanup. */
484 rc = PyObject_IsInstance(message, PyExc_Warning);
485 if (rc == -1) {
486 goto cleanup;
487 }
488 if (rc == 1) {
489 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000490 if (text == NULL)
491 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000492 category = (PyObject*)message->ob_type;
493 }
494 else {
495 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100496 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000497 if (message == NULL)
498 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000499 }
500
501 lineno_obj = PyLong_FromLong(lineno);
502 if (lineno_obj == NULL)
503 goto cleanup;
504
Victor Stinner22f18752016-12-09 18:08:18 +0100505 if (source == Py_None) {
506 source = NULL;
507 }
508
Christian Heimes33fe8092008-04-13 13:53:33 +0000509 /* Create key. */
510 key = PyTuple_Pack(3, text, category, lineno_obj);
511 if (key == NULL)
512 goto cleanup;
513
Brett Cannondb734912008-06-27 00:52:15 +0000514 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000515 rc = already_warned(registry, key, 0);
516 if (rc == -1)
517 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000519 goto return_none;
520 /* Else this warning hasn't been generated before. */
521 }
522
523 action = get_filter(category, text, lineno, module, &item);
524 if (action == NULL)
525 goto cleanup;
526
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200527 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000528 PyErr_SetObject(category, message);
529 goto cleanup;
530 }
531
532 /* Store in the registry that we've been here, *except* when the action
533 is "always". */
534 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200535 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000536 if (registry != NULL && registry != Py_None &&
537 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000538 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200539 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000540 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200541 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000542 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000543 registry = get_once_registry();
544 if (registry == NULL)
545 goto cleanup;
546 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600547 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000549 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200550 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000551 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000552 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000554 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200555 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000556 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100557 "Unrecognized action (%R) in warnings.filters:\n %R",
558 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000559 goto cleanup;
560 }
561 }
562
Christian Heimes1a8501c2008-10-02 19:56:01 +0000563 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000564 goto return_none;
565 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100566 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100567 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100568 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000569 }
570 else /* if (rc == -1) */
571 goto cleanup;
572
573 return_none:
574 result = Py_None;
575 Py_INCREF(result);
576
577 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400578 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000579 Py_XDECREF(key);
580 Py_XDECREF(text);
581 Py_XDECREF(lineno_obj);
582 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000583 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000584 return result; /* Py_None or NULL. */
585}
586
Larry Hastings714e4932015-09-06 00:39:37 -0700587static int
588is_internal_frame(PyFrameObject *frame)
589{
590 static PyObject *importlib_string = NULL;
591 static PyObject *bootstrap_string = NULL;
592 PyObject *filename;
593 int contains;
594
595 if (importlib_string == NULL) {
596 importlib_string = PyUnicode_FromString("importlib");
597 if (importlib_string == NULL) {
598 return 0;
599 }
600
601 bootstrap_string = PyUnicode_FromString("_bootstrap");
602 if (bootstrap_string == NULL) {
603 Py_DECREF(importlib_string);
604 return 0;
605 }
606 Py_INCREF(importlib_string);
607 Py_INCREF(bootstrap_string);
608 }
609
610 if (frame == NULL || frame->f_code == NULL ||
611 frame->f_code->co_filename == NULL) {
612 return 0;
613 }
614 filename = frame->f_code->co_filename;
615 if (!PyUnicode_Check(filename)) {
616 return 0;
617 }
618 contains = PyUnicode_Contains(filename, importlib_string);
619 if (contains < 0) {
620 return 0;
621 }
622 else if (contains > 0) {
623 contains = PyUnicode_Contains(filename, bootstrap_string);
624 if (contains < 0) {
625 return 0;
626 }
627 else if (contains > 0) {
628 return 1;
629 }
630 }
631
632 return 0;
633}
634
635static PyFrameObject *
636next_external_frame(PyFrameObject *frame)
637{
638 do {
639 frame = frame->f_back;
640 } while (frame != NULL && is_internal_frame(frame));
641
642 return frame;
643}
644
Christian Heimes33fe8092008-04-13 13:53:33 +0000645/* filename, module, and registry are new refs, globals is borrowed */
646/* Returns 0 on error (no new refs), 1 on success */
647static int
648setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
649 PyObject **module, PyObject **registry)
650{
651 PyObject *globals;
652
653 /* Setup globals and lineno. */
654 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700655 // Stack level comparisons to Python code is off by one as there is no
656 // warnings-related stack level to avoid.
657 if (stack_level <= 0 || is_internal_frame(f)) {
658 while (--stack_level > 0 && f != NULL) {
659 f = f->f_back;
660 }
661 }
662 else {
663 while (--stack_level > 0 && f != NULL) {
664 f = next_external_frame(f);
665 }
666 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000667
668 if (f == NULL) {
669 globals = PyThreadState_Get()->interp->sysdict;
670 *lineno = 1;
671 }
672 else {
673 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000674 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000675 }
676
677 *module = NULL;
678
679 /* Setup registry. */
680 assert(globals != NULL);
681 assert(PyDict_Check(globals));
682 *registry = PyDict_GetItemString(globals, "__warningregistry__");
683 if (*registry == NULL) {
684 int rc;
685
686 *registry = PyDict_New();
687 if (*registry == NULL)
688 return 0;
689
690 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
691 if (rc < 0)
692 goto handle_error;
693 }
694 else
695 Py_INCREF(*registry);
696
697 /* Setup module. */
698 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300699 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
700 Py_INCREF(*module);
701 }
702 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000703 *module = PyUnicode_FromString("<string>");
704 if (*module == NULL)
705 goto handle_error;
706 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000707
708 /* Setup filename. */
709 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200710 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200711 Py_ssize_t len;
712 int kind;
713 void *data;
714
715 if (PyUnicode_READY(*filename))
716 goto handle_error;
717
Victor Stinner9e30aa52011-11-21 02:49:52 +0100718 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200719 kind = PyUnicode_KIND(*filename);
720 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000721
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500722#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400723 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000724 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200725 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500726 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
727 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400728 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000729 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200730 *filename = PyUnicode_Substring(*filename, 0,
731 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000732 if (*filename == NULL)
733 goto handle_error;
734 }
735 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000736 Py_INCREF(*filename);
737 }
738 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500739 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200740 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100741 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100742 /* PyList_Check() is needed because sys.argv is set to None during
743 Python finalization */
744 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000745 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000746 *filename = PyList_GetItem(argv, 0);
747 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000748 /* If sys.argv[0] is false, then use '__main__'. */
749 is_true = PyObject_IsTrue(*filename);
750 if (is_true < 0) {
751 Py_DECREF(*filename);
752 goto handle_error;
753 }
754 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300755 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000756 if (*filename == NULL)
757 goto handle_error;
758 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000759 }
760 else {
761 /* embedded interpreters don't have sys.argv, see bug #839151 */
762 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100763 if (*filename == NULL)
764 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000765 }
766 }
767 if (*filename == NULL) {
768 *filename = *module;
769 Py_INCREF(*filename);
770 }
771 }
772
773 return 1;
774
775 handle_error:
776 /* filename not XDECREF'ed here as there is no way to jump here with a
777 dangling reference. */
778 Py_XDECREF(*registry);
779 Py_XDECREF(*module);
780 return 0;
781}
782
783static PyObject *
784get_category(PyObject *message, PyObject *category)
785{
786 int rc;
787
788 /* Get category. */
789 rc = PyObject_IsInstance(message, PyExc_Warning);
790 if (rc == -1)
791 return NULL;
792
793 if (rc == 1)
794 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300795 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000796 category = PyExc_UserWarning;
797
798 /* Validate category. */
799 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300800 /* category is not a subclass of PyExc_Warning or
801 PyObject_IsSubclass raised an error */
802 if (rc == -1 || rc == 0) {
803 PyErr_Format(PyExc_TypeError,
804 "category must be a Warning subclass, not '%s'",
805 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000806 return NULL;
807 }
808
809 return category;
810}
811
812static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100813do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
814 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000815{
816 PyObject *filename, *module, *registry, *res;
817 int lineno;
818
819 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
820 return NULL;
821
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100822 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100823 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000824 Py_DECREF(filename);
825 Py_DECREF(registry);
826 Py_DECREF(module);
827 return res;
828}
829
Victor Stinner22f18752016-12-09 18:08:18 +0100830/*[clinic input]
831warn as warnings_warn
832
833 message: object
834 category: object = None
835 stacklevel: Py_ssize_t = 1
836 source: object = None
837
838Issue a warning, or maybe ignore it or raise an exception.
839[clinic start generated code]*/
840
Christian Heimes33fe8092008-04-13 13:53:33 +0000841static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100842warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
843 Py_ssize_t stacklevel, PyObject *source)
844/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000845{
Christian Heimes33fe8092008-04-13 13:53:33 +0000846 category = get_category(message, category);
847 if (category == NULL)
848 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100849 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000850}
851
852static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200853get_source_line(PyObject *module_globals, int lineno)
854{
855 _Py_IDENTIFIER(get_source);
856 _Py_IDENTIFIER(__loader__);
857 _Py_IDENTIFIER(__name__);
858 PyObject *loader;
859 PyObject *module_name;
860 PyObject *get_source;
861 PyObject *source;
862 PyObject *source_list;
863 PyObject *source_line;
864
865 /* Check/get the requisite pieces needed for the loader. */
866 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
867 if (loader == NULL) {
868 return NULL;
869 }
870 Py_INCREF(loader);
871 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
872 if (!module_name) {
873 Py_DECREF(loader);
874 return NULL;
875 }
876 Py_INCREF(module_name);
877
878 /* Make sure the loader implements the optional get_source() method. */
879 get_source = _PyObject_GetAttrId(loader, &PyId_get_source);
880 Py_DECREF(loader);
881 if (!get_source) {
882 Py_DECREF(module_name);
883 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
884 PyErr_Clear();
885 }
886 return NULL;
887 }
888 /* Call get_source() to get the source code. */
889 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
890 Py_DECREF(get_source);
891 Py_DECREF(module_name);
892 if (!source) {
893 return NULL;
894 }
895 if (source == Py_None) {
896 Py_DECREF(source);
897 return NULL;
898 }
899
900 /* Split the source into lines. */
901 source_list = PyUnicode_Splitlines(source, 0);
902 Py_DECREF(source);
903 if (!source_list) {
904 return NULL;
905 }
906
907 /* Get the source line. */
908 source_line = PyList_GetItem(source_list, lineno-1);
909 Py_XINCREF(source_line);
910 Py_DECREF(source_list);
911 return source_line;
912}
913
914static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000915warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
916{
917 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100918 "module", "registry", "module_globals",
919 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000920 PyObject *message;
921 PyObject *category;
922 PyObject *filename;
923 int lineno;
924 PyObject *module = NULL;
925 PyObject *registry = NULL;
926 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100927 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200928 PyObject *source_line = NULL;
929 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000930
Victor Stinner914cde82016-03-19 01:03:51 +0100931 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000932 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100933 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000934 return NULL;
935
936 if (module_globals) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200937 source_line = get_source_line(module_globals, lineno);
938 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000939 return NULL;
940 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000941 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200942 returned = warn_explicit(category, message, filename, lineno, module,
943 registry, source_line, sourceobj);
944 Py_XDECREF(source_line);
945 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000946}
947
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200948static PyObject *
949warnings_filters_mutated(PyObject *self, PyObject *args)
950{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600951 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200952 Py_RETURN_NONE;
953}
954
Christian Heimes33fe8092008-04-13 13:53:33 +0000955
956/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000957
958static int
959warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100960 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000961{
962 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000963
964 if (category == NULL)
965 category = PyExc_RuntimeWarning;
966
Victor Stinner914cde82016-03-19 01:03:51 +0100967 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000968 if (res == NULL)
969 return -1;
970 Py_DECREF(res);
971
972 return 0;
973}
974
Victor Stinner914cde82016-03-19 01:03:51 +0100975static int
976_PyErr_WarnFormatV(PyObject *source,
977 PyObject *category, Py_ssize_t stack_level,
978 const char *format, va_list vargs)
979{
980 PyObject *message;
981 int res;
982
983 message = PyUnicode_FromFormatV(format, vargs);
984 if (message == NULL)
985 return -1;
986
987 res = warn_unicode(category, message, stack_level, source);
988 Py_DECREF(message);
989 return res;
990}
991
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000992int
993PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
994 const char *format, ...)
995{
Victor Stinner914cde82016-03-19 01:03:51 +0100996 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000997 va_list vargs;
998
999#ifdef HAVE_STDARG_PROTOTYPES
1000 va_start(vargs, format);
1001#else
1002 va_start(vargs);
1003#endif
Victor Stinner914cde82016-03-19 01:03:51 +01001004 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001005 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +01001006 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001007}
1008
1009int
Victor Stinner914cde82016-03-19 01:03:51 +01001010PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
1011 const char *format, ...)
1012{
1013 int res;
1014 va_list vargs;
1015
1016#ifdef HAVE_STDARG_PROTOTYPES
1017 va_start(vargs, format);
1018#else
1019 va_start(vargs);
1020#endif
1021 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1022 stack_level, format, vargs);
1023 va_end(vargs);
1024 return res;
1025}
1026
1027
1028int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001029PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1030{
1031 int ret;
1032 PyObject *message = PyUnicode_FromString(text);
1033 if (message == NULL)
1034 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001035 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001036 Py_DECREF(message);
1037 return ret;
1038}
1039
Ezio Melotti42da6632011-03-15 05:18:48 +02001040/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001041 Use PyErr_WarnEx instead. */
1042
1043#undef PyErr_Warn
1044
1045PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001046PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001047{
1048 return PyErr_WarnEx(category, text, 1);
1049}
1050
1051/* Warning with explicit origin */
1052int
Victor Stinner14e461d2013-08-26 22:28:21 +02001053PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1054 PyObject *filename, int lineno,
1055 PyObject *module, PyObject *registry)
1056{
1057 PyObject *res;
1058 if (category == NULL)
1059 category = PyExc_RuntimeWarning;
1060 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001061 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001062 if (res == NULL)
1063 return -1;
1064 Py_DECREF(res);
1065 return 0;
1066}
1067
1068int
Christian Heimes33fe8092008-04-13 13:53:33 +00001069PyErr_WarnExplicit(PyObject *category, const char *text,
1070 const char *filename_str, int lineno,
1071 const char *module_str, PyObject *registry)
1072{
Christian Heimes33fe8092008-04-13 13:53:33 +00001073 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001074 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001075 PyObject *module = NULL;
1076 int ret = -1;
1077
1078 if (message == NULL || filename == NULL)
1079 goto exit;
1080 if (module_str != NULL) {
1081 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001082 if (module == NULL)
1083 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001084 }
1085
Victor Stinner14e461d2013-08-26 22:28:21 +02001086 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1087 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001088
1089 exit:
1090 Py_XDECREF(message);
1091 Py_XDECREF(module);
1092 Py_XDECREF(filename);
1093 return ret;
1094}
1095
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001096int
1097PyErr_WarnExplicitFormat(PyObject *category,
1098 const char *filename_str, int lineno,
1099 const char *module_str, PyObject *registry,
1100 const char *format, ...)
1101{
1102 PyObject *message;
1103 PyObject *module = NULL;
1104 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1105 int ret = -1;
1106 va_list vargs;
1107
1108 if (filename == NULL)
1109 goto exit;
1110 if (module_str != NULL) {
1111 module = PyUnicode_FromString(module_str);
1112 if (module == NULL)
1113 goto exit;
1114 }
1115
1116#ifdef HAVE_STDARG_PROTOTYPES
1117 va_start(vargs, format);
1118#else
1119 va_start(vargs);
1120#endif
1121 message = PyUnicode_FromFormatV(format, vargs);
1122 if (message != NULL) {
1123 PyObject *res;
1124 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001125 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001126 Py_DECREF(message);
1127 if (res != NULL) {
1128 Py_DECREF(res);
1129 ret = 0;
1130 }
1131 }
1132 va_end(vargs);
1133exit:
1134 Py_XDECREF(module);
1135 Py_XDECREF(filename);
1136 return ret;
1137}
1138
Christian Heimes33fe8092008-04-13 13:53:33 +00001139
Christian Heimes33fe8092008-04-13 13:53:33 +00001140PyDoc_STRVAR(warn_explicit_doc,
1141"Low-level inferface to warnings functionality.");
1142
1143static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001144 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001145 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1146 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001147 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1148 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001149 /* XXX(brett.cannon): add showwarning? */
1150 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001152};
1153
1154
1155static PyObject *
Victor Stinnerb98f1712017-11-23 17:13:44 +01001156create_filter(PyObject *category, _Py_Identifier *id)
Christian Heimes33fe8092008-04-13 13:53:33 +00001157{
Victor Stinner82656272017-11-22 23:51:42 +01001158 PyObject *action_str = _PyUnicode_FromId(id);
1159 if (action_str == NULL) {
1160 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001161 }
1162
1163 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001164 return PyTuple_Pack(5, action_str, Py_None,
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001165 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001166}
1167
1168static PyObject *
Victor Stinner1f151112017-11-23 10:43:14 +01001169init_filters(const _PyCoreConfig *config)
Christian Heimes33fe8092008-04-13 13:53:33 +00001170{
Victor Stinner1f151112017-11-23 10:43:14 +01001171 int dev_mode = config->dev_mode;
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001172
1173 Py_ssize_t count = 2;
1174 if (dev_mode) {
1175 count++;
1176 }
Victor Stinner895862a2017-11-20 09:47:03 -08001177#ifndef Py_DEBUG
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001178 if (!dev_mode) {
1179 count += 3;
1180 }
Victor Stinner895862a2017-11-20 09:47:03 -08001181#endif
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001182 PyObject *filters = PyList_New(count);
Christian Heimes33fe8092008-04-13 13:53:33 +00001183 if (filters == NULL)
1184 return NULL;
1185
Victor Stinnerb98f1712017-11-23 17:13:44 +01001186 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner895862a2017-11-20 09:47:03 -08001187#ifndef Py_DEBUG
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001188 if (!dev_mode) {
1189 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001190 create_filter(PyExc_DeprecationWarning, &PyId_ignore));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001191 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001192 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001193 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001194 create_filter(PyExc_ImportWarning, &PyId_ignore));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001195 }
Victor Stinner895862a2017-11-20 09:47:03 -08001196#endif
1197
Victor Stinnerb98f1712017-11-23 17:13:44 +01001198 _Py_Identifier *bytes_action;
Christian Heimes33fe8092008-04-13 13:53:33 +00001199 if (Py_BytesWarningFlag > 1)
Victor Stinnerb98f1712017-11-23 17:13:44 +01001200 bytes_action = &PyId_error;
Christian Heimes33fe8092008-04-13 13:53:33 +00001201 else if (Py_BytesWarningFlag)
Victor Stinnerb98f1712017-11-23 17:13:44 +01001202 bytes_action = &PyId_default;
Christian Heimes33fe8092008-04-13 13:53:33 +00001203 else
Victor Stinnerb98f1712017-11-23 17:13:44 +01001204 bytes_action = &PyId_ignore;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001205 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001206 bytes_action));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001207
Victor Stinnerb98f1712017-11-23 17:13:44 +01001208 _Py_Identifier *resource_action;
Georg Brandl08be72d2010-10-24 15:11:22 +00001209 /* resource usage warnings are enabled by default in pydebug mode */
1210#ifdef Py_DEBUG
Victor Stinnerb98f1712017-11-23 17:13:44 +01001211 resource_action = &PyId_always;
Georg Brandl08be72d2010-10-24 15:11:22 +00001212#else
Victor Stinnerb98f1712017-11-23 17:13:44 +01001213 resource_action = (dev_mode ? &PyId_always : &PyId_ignore);
Georg Brandl08be72d2010-10-24 15:11:22 +00001214#endif
1215 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1216 resource_action));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001217
1218 if (dev_mode) {
1219 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001220 create_filter(PyExc_Warning, &PyId_default));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001221 }
1222
Victor Stinnerb98f1712017-11-23 17:13:44 +01001223 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001224 if (PyList_GET_ITEM(filters, x) == NULL) {
1225 Py_DECREF(filters);
1226 return NULL;
1227 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001228 }
1229
1230 return filters;
1231}
1232
Martin v. Löwis1a214512008-06-11 05:26:20 +00001233static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 PyModuleDef_HEAD_INIT,
1235 MODULE_NAME,
1236 warnings__doc__,
1237 0,
1238 warnings_functions,
1239 NULL,
1240 NULL,
1241 NULL,
1242 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001243};
1244
Christian Heimes33fe8092008-04-13 13:53:33 +00001245
Victor Stinner1f151112017-11-23 10:43:14 +01001246PyObject*
1247_PyWarnings_InitWithConfig(const _PyCoreConfig *config)
Christian Heimes33fe8092008-04-13 13:53:33 +00001248{
Brett Cannon0759dd62009-04-01 18:13:07 +00001249 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001250
Martin v. Löwis1a214512008-06-11 05:26:20 +00001251 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001252 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001253 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001254
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001255 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner1f151112017-11-23 10:43:14 +01001256 _PyRuntime.warnings.filters = init_filters(config);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001257 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001258 return NULL;
1259 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001260 Py_INCREF(_PyRuntime.warnings.filters);
1261 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001262 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001263
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001264 if (_PyRuntime.warnings.once_registry == NULL) {
1265 _PyRuntime.warnings.once_registry = PyDict_New();
1266 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001267 return NULL;
1268 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001269 Py_INCREF(_PyRuntime.warnings.once_registry);
1270 if (PyModule_AddObject(m, "_onceregistry",
1271 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001272 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001273
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001274 if (_PyRuntime.warnings.default_action == NULL) {
1275 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1276 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001277 return NULL;
1278 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001279 Py_INCREF(_PyRuntime.warnings.default_action);
1280 if (PyModule_AddObject(m, "_defaultaction",
1281 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001282 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001283
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001284 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001285 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001286}
Victor Stinner1f151112017-11-23 10:43:14 +01001287
1288
1289PyMODINIT_FUNC
1290_PyWarnings_Init(void)
1291{
1292 PyInterpreterState *interp = PyThreadState_GET()->interp;
1293 const _PyCoreConfig *config = &interp->core_config;
1294 return _PyWarnings_InitWithConfig(config);
1295}