blob: 27f5b813a7ef99aa52dc5fe6188f2ed2fe88111a [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);
Victor Stinnerb98f1712017-11-23 17:13:44 +010016_Py_static_string(PyId_default, "default");
Christian Heimes33fe8092008-04-13 13:53:33 +000017
18static int
19check_matched(PyObject *obj, PyObject *arg)
20{
21 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020022 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000023 int rc;
24
25 if (obj == Py_None)
26 return 1;
Victor Stinner55ba38a2016-12-09 16:09:30 +010027 result = _PyObject_CallMethodIdObjArgs(obj, &PyId_match, arg, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +000028 if (result == NULL)
29 return -1;
30
31 rc = PyObject_IsTrue(result);
32 Py_DECREF(result);
33 return rc;
34}
35
36/*
37 Returns a new reference.
38 A NULL return value can mean false or an error.
39*/
40static PyObject *
Victor Stinner82656272017-11-22 23:51:42 +010041get_warnings_attr(_Py_Identifier *attr_id, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +000042{
Victor Stinner82656272017-11-22 23:51:42 +010043 PyObject *warnings_str;
Victor Stinnere98445a2016-03-23 00:54:48 +010044 PyObject *warnings_module, *obj;
Victor Stinner82656272017-11-22 23:51:42 +010045 _Py_IDENTIFIER(warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000046
Victor Stinner82656272017-11-22 23:51:42 +010047 warnings_str = _PyUnicode_FromId(&PyId_warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +000048 if (warnings_str == NULL) {
Victor Stinner82656272017-11-22 23:51:42 +010049 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +000050 }
51
Victor Stinnere98445a2016-03-23 00:54:48 +010052 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060053 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +010054 warnings_module = PyImport_Import(warnings_str);
55 if (warnings_module == NULL) {
56 /* Fallback to the C implementation if we cannot get
57 the Python implementation */
Serhiy Storchakad4f84802017-11-11 15:19:47 +020058 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
59 PyErr_Clear();
60 }
Christian Heimes33fe8092008-04-13 13:53:33 +000061 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010062 }
63 }
64 else {
Eric Snow3f9eee62017-09-15 16:35:20 -060065 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +010066 if (warnings_module == NULL)
67 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +010068 }
69
Victor Stinner82656272017-11-22 23:51:42 +010070 obj = _PyObject_GetAttrId(warnings_module, attr_id);
Victor Stinnere98445a2016-03-23 00:54:48 +010071 Py_DECREF(warnings_module);
Serhiy Storchakad4f84802017-11-11 15:19:47 +020072 if (obj == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
73 PyErr_Clear();
74 }
Victor Stinnere98445a2016-03-23 00:54:48 +010075 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +000076}
77
78
Neal Norwitz32dde222008-04-15 06:43:13 +000079static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000080get_once_registry(void)
81{
82 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +010083 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +000084
Victor Stinner82656272017-11-22 23:51:42 +010085 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +000086 if (registry == NULL) {
87 if (PyErr_Occurred())
88 return NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +020089 assert(_PyRuntime.warnings.once_registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060090 return _PyRuntime.warnings.once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +000091 }
Oren Milman252033d2017-09-11 09:28:39 +030092 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +020093 PyErr_Format(PyExc_TypeError,
94 MODULE_NAME ".onceregistry must be a dict, "
95 "not '%.200s'",
96 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +030097 Py_DECREF(registry);
98 return NULL;
99 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200100 Py_SETREF(_PyRuntime.warnings.once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000101 return registry;
102}
103
104
Brett Cannon0759dd62009-04-01 18:13:07 +0000105static PyObject *
106get_default_action(void)
107{
108 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100109 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000110
Victor Stinner82656272017-11-22 23:51:42 +0100111 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000112 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 if (PyErr_Occurred()) {
114 return NULL;
115 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200116 assert(_PyRuntime.warnings.default_action);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600117 return _PyRuntime.warnings.default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000118 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300119 if (!PyUnicode_Check(default_action)) {
120 PyErr_Format(PyExc_TypeError,
121 MODULE_NAME ".defaultaction must be a string, "
122 "not '%.200s'",
123 Py_TYPE(default_action)->tp_name);
124 Py_DECREF(default_action);
125 return NULL;
126 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200127 Py_SETREF(_PyRuntime.warnings.default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000128 return default_action;
129}
130
131
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400132/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100133static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000134get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
135 PyObject *module, PyObject **item)
136{
Brett Cannon0759dd62009-04-01 18:13:07 +0000137 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000138 Py_ssize_t i;
139 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100140 _Py_IDENTIFIER(filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000141
Victor Stinner82656272017-11-22 23:51:42 +0100142 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000143 if (warnings_filters == NULL) {
144 if (PyErr_Occurred())
145 return NULL;
146 }
147 else {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200148 Py_SETREF(_PyRuntime.warnings.filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000149 }
150
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600151 PyObject *filters = _PyRuntime.warnings.filters;
152 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000153 PyErr_SetString(PyExc_ValueError,
154 MODULE_NAME ".filters must be a list");
155 return NULL;
156 }
157
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600158 /* _PyRuntime.warnings.filters could change while we are iterating over it. */
159 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000160 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
161 Py_ssize_t ln;
162 int is_subclass, good_msg, good_mod;
163
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600164 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400165 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000166 PyErr_Format(PyExc_ValueError,
167 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
168 return NULL;
169 }
170
171 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400172 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000173 action = PyTuple_GET_ITEM(tmp_item, 0);
174 msg = PyTuple_GET_ITEM(tmp_item, 1);
175 cat = PyTuple_GET_ITEM(tmp_item, 2);
176 mod = PyTuple_GET_ITEM(tmp_item, 3);
177 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
178
Oren Milman9d984fd2017-09-12 00:18:09 +0300179 if (!PyUnicode_Check(action)) {
180 PyErr_Format(PyExc_TypeError,
181 "action must be a string, not '%.200s'",
182 Py_TYPE(action)->tp_name);
183 Py_DECREF(tmp_item);
184 return NULL;
185 }
186
Christian Heimes33fe8092008-04-13 13:53:33 +0000187 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400188 if (good_msg == -1) {
189 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100190 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400191 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100192
Christian Heimes33fe8092008-04-13 13:53:33 +0000193 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400194 if (good_mod == -1) {
195 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100196 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400197 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100198
Christian Heimes33fe8092008-04-13 13:53:33 +0000199 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400200 if (is_subclass == -1) {
201 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100202 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400203 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100204
Christian Heimes33fe8092008-04-13 13:53:33 +0000205 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400206 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400207 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000208 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400209 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000210
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400211 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
212 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100213 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400214 }
215
216 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000217 }
218
Brett Cannon0759dd62009-04-01 18:13:07 +0000219 action = get_default_action();
220 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400221 Py_INCREF(Py_None);
222 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100223 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000224 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000225
Christian Heimes33fe8092008-04-13 13:53:33 +0000226 return NULL;
227}
228
Brett Cannon0759dd62009-04-01 18:13:07 +0000229
Christian Heimes33fe8092008-04-13 13:53:33 +0000230static int
231already_warned(PyObject *registry, PyObject *key, int should_set)
232{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200233 PyObject *version_obj, *already_warned;
234 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000235
236 if (key == NULL)
237 return -1;
238
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200239 version_obj = _PyDict_GetItemId(registry, &PyId_version);
240 if (version_obj == NULL
241 || !PyLong_CheckExact(version_obj)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600242 || PyLong_AsLong(version_obj) != _PyRuntime.warnings.filters_version) {
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200243 PyDict_Clear(registry);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600244 version_obj = PyLong_FromLong(_PyRuntime.warnings.filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200245 if (version_obj == NULL)
246 return -1;
247 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
248 Py_DECREF(version_obj);
249 return -1;
250 }
251 Py_DECREF(version_obj);
252 }
253 else {
254 already_warned = PyDict_GetItem(registry, key);
255 if (already_warned != NULL) {
256 int rc = PyObject_IsTrue(already_warned);
257 if (rc != 0)
258 return rc;
259 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000260 }
261
262 /* This warning wasn't found in the registry, set it. */
263 if (should_set)
264 return PyDict_SetItem(registry, key, Py_True);
265 return 0;
266}
267
268/* New reference. */
269static PyObject *
270normalize_module(PyObject *filename)
271{
272 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100273 int kind;
274 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000275 Py_ssize_t len;
276
Victor Stinner9e30aa52011-11-21 02:49:52 +0100277 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000278 if (len < 0)
279 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100280
281 if (len == 0)
282 return PyUnicode_FromString("<unknown>");
283
284 kind = PyUnicode_KIND(filename);
285 data = PyUnicode_DATA(filename);
286
287 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000288 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100289 PyUnicode_READ(kind, data, len-3) == '.' &&
290 PyUnicode_READ(kind, data, len-2) == 'p' &&
291 PyUnicode_READ(kind, data, len-1) == 'y')
292 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100293 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000294 }
295 else {
296 module = filename;
297 Py_INCREF(module);
298 }
299 return module;
300}
301
302static int
303update_registry(PyObject *registry, PyObject *text, PyObject *category,
304 int add_zero)
305{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300306 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000307 int rc;
308
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300309 if (add_zero)
310 altkey = PyTuple_Pack(3, text, category, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +0000311 else
312 altkey = PyTuple_Pack(2, text, category);
313
314 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000315 Py_XDECREF(altkey);
316 return rc;
317}
318
319static void
Victor Stinner914cde82016-03-19 01:03:51 +0100320show_warning(PyObject *filename, int lineno, PyObject *text,
321 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 PyObject *f_stderr;
324 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000325 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200326 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000327
328 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
329
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200330 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000331 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100332 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000333
Victor Stinnerbd303c12013-11-07 23:07:29 +0100334 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000335 if (f_stderr == NULL) {
336 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100337 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000338 }
339
340 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100341 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
342 goto error;
343 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
344 goto error;
345 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
346 goto error;
347 if (PyFile_WriteString(": ", f_stderr) < 0)
348 goto error;
349 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
350 goto error;
351 if (PyFile_WriteString("\n", f_stderr) < 0)
352 goto error;
353 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000354
355 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000356 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100357 int kind;
358 void *data;
359 Py_ssize_t i, len;
360 Py_UCS4 ch;
361 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000362
Victor Stinnera4c704b2013-10-29 23:43:41 +0100363 if (PyUnicode_READY(sourceline) < 1)
364 goto error;
365
366 kind = PyUnicode_KIND(sourceline);
367 data = PyUnicode_DATA(sourceline);
368 len = PyUnicode_GET_LENGTH(sourceline);
369 for (i=0; i<len; i++) {
370 ch = PyUnicode_READ(kind, data, i);
371 if (ch != ' ' && ch != '\t' && ch != '\014')
372 break;
373 }
374
375 truncated = PyUnicode_Substring(sourceline, i, len);
376 if (truncated == NULL)
377 goto error;
378
379 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
380 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000381 PyFile_WriteString("\n", f_stderr);
382 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200383 else {
384 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
385 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100386
387error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100388 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000389 PyErr_Clear();
390}
391
Victor Stinner1231a462016-03-19 00:47:17 +0100392static int
393call_show_warning(PyObject *category, PyObject *text, PyObject *message,
394 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100395 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100396{
397 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100398 _Py_IDENTIFIER(_showwarnmsg);
399 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100400
Victor Stinnere98445a2016-03-23 00:54:48 +0100401 /* If the source parameter is set, try to get the Python implementation.
402 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600403 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100404 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100405 if (show_fn == NULL) {
406 if (PyErr_Occurred())
407 return -1;
408 show_warning(filename, lineno, text, category, sourceline);
409 return 0;
410 }
411
412 if (!PyCallable_Check(show_fn)) {
413 PyErr_SetString(PyExc_TypeError,
414 "warnings._showwarnmsg() must be set to a callable");
415 goto error;
416 }
417
Victor Stinner82656272017-11-22 23:51:42 +0100418 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100419 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200420 if (!PyErr_Occurred()) {
421 PyErr_SetString(PyExc_RuntimeError,
422 "unable to get warnings.WarningMessage");
423 }
Victor Stinner1231a462016-03-19 00:47:17 +0100424 goto error;
425 }
426
427 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100428 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100429 NULL);
430 Py_DECREF(warnmsg_cls);
431 if (msg == NULL)
432 goto error;
433
Victor Stinnerde4ae3d2016-12-04 22:59:09 +0100434 res = PyObject_CallFunctionObjArgs(show_fn, msg, NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100435 Py_DECREF(show_fn);
436 Py_DECREF(msg);
437
438 if (res == NULL)
439 return -1;
440
441 Py_DECREF(res);
442 return 0;
443
444error:
445 Py_XDECREF(show_fn);
446 return -1;
447}
448
Christian Heimes33fe8092008-04-13 13:53:33 +0000449static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000451 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100452 PyObject *module, PyObject *registry, PyObject *sourceline,
453 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000454{
455 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400456 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100457 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000458 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100460 /* module can be None if a warning is emitted late during Python shutdown.
461 In this case, the Python warnings module was probably unloaded, filters
462 are no more available to choose as action. It is safer to ignore the
463 warning and do nothing. */
464 if (module == Py_None)
465 Py_RETURN_NONE;
466
Brett Cannondb734912008-06-27 00:52:15 +0000467 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300468 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000469 return NULL;
470 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000471
472 /* Normalize module. */
473 if (module == NULL) {
474 module = normalize_module(filename);
475 if (module == NULL)
476 return NULL;
477 }
478 else
479 Py_INCREF(module);
480
481 /* Normalize message. */
482 Py_INCREF(message); /* DECREF'ed in cleanup. */
483 rc = PyObject_IsInstance(message, PyExc_Warning);
484 if (rc == -1) {
485 goto cleanup;
486 }
487 if (rc == 1) {
488 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000489 if (text == NULL)
490 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000491 category = (PyObject*)message->ob_type;
492 }
493 else {
494 text = message;
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100495 message = PyObject_CallFunctionObjArgs(category, message, NULL);
Brett Cannondb734912008-06-27 00:52:15 +0000496 if (message == NULL)
497 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000498 }
499
500 lineno_obj = PyLong_FromLong(lineno);
501 if (lineno_obj == NULL)
502 goto cleanup;
503
Victor Stinner22f18752016-12-09 18:08:18 +0100504 if (source == Py_None) {
505 source = NULL;
506 }
507
Christian Heimes33fe8092008-04-13 13:53:33 +0000508 /* Create key. */
509 key = PyTuple_Pack(3, text, category, lineno_obj);
510 if (key == NULL)
511 goto cleanup;
512
Brett Cannondb734912008-06-27 00:52:15 +0000513 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000514 rc = already_warned(registry, key, 0);
515 if (rc == -1)
516 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000518 goto return_none;
519 /* Else this warning hasn't been generated before. */
520 }
521
522 action = get_filter(category, text, lineno, module, &item);
523 if (action == NULL)
524 goto cleanup;
525
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200526 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000527 PyErr_SetObject(category, message);
528 goto cleanup;
529 }
530
531 /* Store in the registry that we've been here, *except* when the action
532 is "always". */
533 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200534 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000535 if (registry != NULL && registry != Py_None &&
536 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000537 goto cleanup;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200538 else if (_PyUnicode_EqualToASCIIString(action, "ignore"))
Christian Heimes33fe8092008-04-13 13:53:33 +0000539 goto return_none;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200540 else if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000541 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000542 registry = get_once_registry();
543 if (registry == NULL)
544 goto cleanup;
545 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600546 /* _PyRuntime.warnings.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000548 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200549 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000550 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000551 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000553 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200554 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000555 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100556 "Unrecognized action (%R) in warnings.filters:\n %R",
557 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000558 goto cleanup;
559 }
560 }
561
Christian Heimes1a8501c2008-10-02 19:56:01 +0000562 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000563 goto return_none;
564 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100565 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100566 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100567 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000568 }
569 else /* if (rc == -1) */
570 goto cleanup;
571
572 return_none:
573 result = Py_None;
574 Py_INCREF(result);
575
576 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400577 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000578 Py_XDECREF(key);
579 Py_XDECREF(text);
580 Py_XDECREF(lineno_obj);
581 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000582 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000583 return result; /* Py_None or NULL. */
584}
585
Larry Hastings714e4932015-09-06 00:39:37 -0700586static int
587is_internal_frame(PyFrameObject *frame)
588{
589 static PyObject *importlib_string = NULL;
590 static PyObject *bootstrap_string = NULL;
591 PyObject *filename;
592 int contains;
593
594 if (importlib_string == NULL) {
595 importlib_string = PyUnicode_FromString("importlib");
596 if (importlib_string == NULL) {
597 return 0;
598 }
599
600 bootstrap_string = PyUnicode_FromString("_bootstrap");
601 if (bootstrap_string == NULL) {
602 Py_DECREF(importlib_string);
603 return 0;
604 }
605 Py_INCREF(importlib_string);
606 Py_INCREF(bootstrap_string);
607 }
608
609 if (frame == NULL || frame->f_code == NULL ||
610 frame->f_code->co_filename == NULL) {
611 return 0;
612 }
613 filename = frame->f_code->co_filename;
614 if (!PyUnicode_Check(filename)) {
615 return 0;
616 }
617 contains = PyUnicode_Contains(filename, importlib_string);
618 if (contains < 0) {
619 return 0;
620 }
621 else if (contains > 0) {
622 contains = PyUnicode_Contains(filename, bootstrap_string);
623 if (contains < 0) {
624 return 0;
625 }
626 else if (contains > 0) {
627 return 1;
628 }
629 }
630
631 return 0;
632}
633
634static PyFrameObject *
635next_external_frame(PyFrameObject *frame)
636{
637 do {
638 frame = frame->f_back;
639 } while (frame != NULL && is_internal_frame(frame));
640
641 return frame;
642}
643
Christian Heimes33fe8092008-04-13 13:53:33 +0000644/* filename, module, and registry are new refs, globals is borrowed */
645/* Returns 0 on error (no new refs), 1 on success */
646static int
647setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
648 PyObject **module, PyObject **registry)
649{
650 PyObject *globals;
651
652 /* Setup globals and lineno. */
653 PyFrameObject *f = PyThreadState_GET()->frame;
Larry Hastings714e4932015-09-06 00:39:37 -0700654 // Stack level comparisons to Python code is off by one as there is no
655 // warnings-related stack level to avoid.
656 if (stack_level <= 0 || is_internal_frame(f)) {
657 while (--stack_level > 0 && f != NULL) {
658 f = f->f_back;
659 }
660 }
661 else {
662 while (--stack_level > 0 && f != NULL) {
663 f = next_external_frame(f);
664 }
665 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000666
667 if (f == NULL) {
668 globals = PyThreadState_Get()->interp->sysdict;
669 *lineno = 1;
670 }
671 else {
672 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000673 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000674 }
675
676 *module = NULL;
677
678 /* Setup registry. */
679 assert(globals != NULL);
680 assert(PyDict_Check(globals));
681 *registry = PyDict_GetItemString(globals, "__warningregistry__");
682 if (*registry == NULL) {
683 int rc;
684
685 *registry = PyDict_New();
686 if (*registry == NULL)
687 return 0;
688
689 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
690 if (rc < 0)
691 goto handle_error;
692 }
693 else
694 Py_INCREF(*registry);
695
696 /* Setup module. */
697 *module = PyDict_GetItemString(globals, "__name__");
Oren Milman5d3e8002017-09-24 21:28:42 +0300698 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
699 Py_INCREF(*module);
700 }
701 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000702 *module = PyUnicode_FromString("<string>");
703 if (*module == NULL)
704 goto handle_error;
705 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000706
707 /* Setup filename. */
708 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200709 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200710 Py_ssize_t len;
711 int kind;
712 void *data;
713
714 if (PyUnicode_READY(*filename))
715 goto handle_error;
716
Victor Stinner9e30aa52011-11-21 02:49:52 +0100717 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200718 kind = PyUnicode_KIND(*filename);
719 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000720
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500721#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Brett Cannonf299abd2015-04-13 14:21:02 -0400722 /* if filename.lower().endswith(".pyc"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000723 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200724 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500725 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
726 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
Brett Cannonf299abd2015-04-13 14:21:02 -0400727 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c')
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000728 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200729 *filename = PyUnicode_Substring(*filename, 0,
730 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000731 if (*filename == NULL)
732 goto handle_error;
733 }
734 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000735 Py_INCREF(*filename);
736 }
737 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500738 *filename = NULL;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200739 if (*module != Py_None && _PyUnicode_EqualToASCIIString(*module, "__main__")) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100740 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100741 /* PyList_Check() is needed because sys.argv is set to None during
742 Python finalization */
743 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000744 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000745 *filename = PyList_GetItem(argv, 0);
746 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000747 /* If sys.argv[0] is false, then use '__main__'. */
748 is_true = PyObject_IsTrue(*filename);
749 if (is_true < 0) {
750 Py_DECREF(*filename);
751 goto handle_error;
752 }
753 else if (!is_true) {
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300754 Py_SETREF(*filename, PyUnicode_FromString("__main__"));
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000755 if (*filename == NULL)
756 goto handle_error;
757 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000758 }
759 else {
760 /* embedded interpreters don't have sys.argv, see bug #839151 */
761 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100762 if (*filename == NULL)
763 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000764 }
765 }
766 if (*filename == NULL) {
767 *filename = *module;
768 Py_INCREF(*filename);
769 }
770 }
771
772 return 1;
773
774 handle_error:
775 /* filename not XDECREF'ed here as there is no way to jump here with a
776 dangling reference. */
777 Py_XDECREF(*registry);
778 Py_XDECREF(*module);
779 return 0;
780}
781
782static PyObject *
783get_category(PyObject *message, PyObject *category)
784{
785 int rc;
786
787 /* Get category. */
788 rc = PyObject_IsInstance(message, PyExc_Warning);
789 if (rc == -1)
790 return NULL;
791
792 if (rc == 1)
793 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300794 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000795 category = PyExc_UserWarning;
796
797 /* Validate category. */
798 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300799 /* category is not a subclass of PyExc_Warning or
800 PyObject_IsSubclass raised an error */
801 if (rc == -1 || rc == 0) {
802 PyErr_Format(PyExc_TypeError,
803 "category must be a Warning subclass, not '%s'",
804 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000805 return NULL;
806 }
807
808 return category;
809}
810
811static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100812do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
813 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000814{
815 PyObject *filename, *module, *registry, *res;
816 int lineno;
817
818 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
819 return NULL;
820
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100821 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100822 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000823 Py_DECREF(filename);
824 Py_DECREF(registry);
825 Py_DECREF(module);
826 return res;
827}
828
Victor Stinner22f18752016-12-09 18:08:18 +0100829/*[clinic input]
830warn as warnings_warn
831
832 message: object
833 category: object = None
834 stacklevel: Py_ssize_t = 1
835 source: object = None
836
837Issue a warning, or maybe ignore it or raise an exception.
838[clinic start generated code]*/
839
Christian Heimes33fe8092008-04-13 13:53:33 +0000840static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100841warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
842 Py_ssize_t stacklevel, PyObject *source)
843/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000844{
Christian Heimes33fe8092008-04-13 13:53:33 +0000845 category = get_category(message, category);
846 if (category == NULL)
847 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100848 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000849}
850
851static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200852get_source_line(PyObject *module_globals, int lineno)
853{
854 _Py_IDENTIFIER(get_source);
855 _Py_IDENTIFIER(__loader__);
856 _Py_IDENTIFIER(__name__);
857 PyObject *loader;
858 PyObject *module_name;
859 PyObject *get_source;
860 PyObject *source;
861 PyObject *source_list;
862 PyObject *source_line;
863
864 /* Check/get the requisite pieces needed for the loader. */
865 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
866 if (loader == NULL) {
867 return NULL;
868 }
869 Py_INCREF(loader);
870 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
871 if (!module_name) {
872 Py_DECREF(loader);
873 return NULL;
874 }
875 Py_INCREF(module_name);
876
877 /* Make sure the loader implements the optional get_source() method. */
878 get_source = _PyObject_GetAttrId(loader, &PyId_get_source);
879 Py_DECREF(loader);
880 if (!get_source) {
881 Py_DECREF(module_name);
882 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
883 PyErr_Clear();
884 }
885 return NULL;
886 }
887 /* Call get_source() to get the source code. */
888 source = PyObject_CallFunctionObjArgs(get_source, module_name, NULL);
889 Py_DECREF(get_source);
890 Py_DECREF(module_name);
891 if (!source) {
892 return NULL;
893 }
894 if (source == Py_None) {
895 Py_DECREF(source);
896 return NULL;
897 }
898
899 /* Split the source into lines. */
900 source_list = PyUnicode_Splitlines(source, 0);
901 Py_DECREF(source);
902 if (!source_list) {
903 return NULL;
904 }
905
906 /* Get the source line. */
907 source_line = PyList_GetItem(source_list, lineno-1);
908 Py_XINCREF(source_line);
909 Py_DECREF(source_list);
910 return source_line;
911}
912
913static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +0000914warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
915{
916 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +0100917 "module", "registry", "module_globals",
918 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +0000919 PyObject *message;
920 PyObject *category;
921 PyObject *filename;
922 int lineno;
923 PyObject *module = NULL;
924 PyObject *registry = NULL;
925 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +0100926 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200927 PyObject *source_line = NULL;
928 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000929
Victor Stinner914cde82016-03-19 01:03:51 +0100930 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000931 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +0100932 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +0000933 return NULL;
934
935 if (module_globals) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200936 source_line = get_source_line(module_globals, lineno);
937 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000938 return NULL;
939 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000940 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200941 returned = warn_explicit(category, message, filename, lineno, module,
942 registry, source_line, sourceobj);
943 Py_XDECREF(source_line);
944 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +0000945}
946
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200947static PyObject *
948warnings_filters_mutated(PyObject *self, PyObject *args)
949{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600950 _PyRuntime.warnings.filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200951 Py_RETURN_NONE;
952}
953
Christian Heimes33fe8092008-04-13 13:53:33 +0000954
955/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000956
957static int
958warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +0100959 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000960{
961 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000962
963 if (category == NULL)
964 category = PyExc_RuntimeWarning;
965
Victor Stinner914cde82016-03-19 01:03:51 +0100966 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000967 if (res == NULL)
968 return -1;
969 Py_DECREF(res);
970
971 return 0;
972}
973
Victor Stinner914cde82016-03-19 01:03:51 +0100974static int
975_PyErr_WarnFormatV(PyObject *source,
976 PyObject *category, Py_ssize_t stack_level,
977 const char *format, va_list vargs)
978{
979 PyObject *message;
980 int res;
981
982 message = PyUnicode_FromFormatV(format, vargs);
983 if (message == NULL)
984 return -1;
985
986 res = warn_unicode(category, message, stack_level, source);
987 Py_DECREF(message);
988 return res;
989}
990
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000991int
992PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
993 const char *format, ...)
994{
Victor Stinner914cde82016-03-19 01:03:51 +0100995 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000996 va_list vargs;
997
998#ifdef HAVE_STDARG_PROTOTYPES
999 va_start(vargs, format);
1000#else
1001 va_start(vargs);
1002#endif
Victor Stinner914cde82016-03-19 01:03:51 +01001003 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001004 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +01001005 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001006}
1007
1008int
Victor Stinner914cde82016-03-19 01:03:51 +01001009PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
1010 const char *format, ...)
1011{
1012 int res;
1013 va_list vargs;
1014
1015#ifdef HAVE_STDARG_PROTOTYPES
1016 va_start(vargs, format);
1017#else
1018 va_start(vargs);
1019#endif
1020 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1021 stack_level, format, vargs);
1022 va_end(vargs);
1023 return res;
1024}
1025
1026
1027int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001028PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1029{
1030 int ret;
1031 PyObject *message = PyUnicode_FromString(text);
1032 if (message == NULL)
1033 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001034 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001035 Py_DECREF(message);
1036 return ret;
1037}
1038
Ezio Melotti42da6632011-03-15 05:18:48 +02001039/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001040 Use PyErr_WarnEx instead. */
1041
1042#undef PyErr_Warn
1043
1044PyAPI_FUNC(int)
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001045PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001046{
1047 return PyErr_WarnEx(category, text, 1);
1048}
1049
1050/* Warning with explicit origin */
1051int
Victor Stinner14e461d2013-08-26 22:28:21 +02001052PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1053 PyObject *filename, int lineno,
1054 PyObject *module, PyObject *registry)
1055{
1056 PyObject *res;
1057 if (category == NULL)
1058 category = PyExc_RuntimeWarning;
1059 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001060 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001061 if (res == NULL)
1062 return -1;
1063 Py_DECREF(res);
1064 return 0;
1065}
1066
1067int
Christian Heimes33fe8092008-04-13 13:53:33 +00001068PyErr_WarnExplicit(PyObject *category, const char *text,
1069 const char *filename_str, int lineno,
1070 const char *module_str, PyObject *registry)
1071{
Christian Heimes33fe8092008-04-13 13:53:33 +00001072 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001073 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001074 PyObject *module = NULL;
1075 int ret = -1;
1076
1077 if (message == NULL || filename == NULL)
1078 goto exit;
1079 if (module_str != NULL) {
1080 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001081 if (module == NULL)
1082 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001083 }
1084
Victor Stinner14e461d2013-08-26 22:28:21 +02001085 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1086 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001087
1088 exit:
1089 Py_XDECREF(message);
1090 Py_XDECREF(module);
1091 Py_XDECREF(filename);
1092 return ret;
1093}
1094
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001095int
1096PyErr_WarnExplicitFormat(PyObject *category,
1097 const char *filename_str, int lineno,
1098 const char *module_str, PyObject *registry,
1099 const char *format, ...)
1100{
1101 PyObject *message;
1102 PyObject *module = NULL;
1103 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1104 int ret = -1;
1105 va_list vargs;
1106
1107 if (filename == NULL)
1108 goto exit;
1109 if (module_str != NULL) {
1110 module = PyUnicode_FromString(module_str);
1111 if (module == NULL)
1112 goto exit;
1113 }
1114
1115#ifdef HAVE_STDARG_PROTOTYPES
1116 va_start(vargs, format);
1117#else
1118 va_start(vargs);
1119#endif
1120 message = PyUnicode_FromFormatV(format, vargs);
1121 if (message != NULL) {
1122 PyObject *res;
1123 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001124 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001125 Py_DECREF(message);
1126 if (res != NULL) {
1127 Py_DECREF(res);
1128 ret = 0;
1129 }
1130 }
1131 va_end(vargs);
1132exit:
1133 Py_XDECREF(module);
1134 Py_XDECREF(filename);
1135 return ret;
1136}
1137
Christian Heimes33fe8092008-04-13 13:53:33 +00001138
Christian Heimes33fe8092008-04-13 13:53:33 +00001139PyDoc_STRVAR(warn_explicit_doc,
1140"Low-level inferface to warnings functionality.");
1141
1142static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001143 WARNINGS_WARN_METHODDEF
Christian Heimes33fe8092008-04-13 13:53:33 +00001144 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
1145 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001146 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1147 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001148 /* XXX(brett.cannon): add showwarning? */
1149 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001151};
1152
1153
1154static PyObject *
Victor Stinnerb98f1712017-11-23 17:13:44 +01001155create_filter(PyObject *category, _Py_Identifier *id)
Christian Heimes33fe8092008-04-13 13:53:33 +00001156{
Victor Stinner82656272017-11-22 23:51:42 +01001157 PyObject *action_str = _PyUnicode_FromId(id);
1158 if (action_str == NULL) {
1159 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001160 }
1161
1162 /* This assumes the line number is zero for now. */
Victor Stinner82656272017-11-22 23:51:42 +01001163 return PyTuple_Pack(5, action_str, Py_None,
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001164 category, Py_None, _PyLong_Zero);
Christian Heimes33fe8092008-04-13 13:53:33 +00001165}
1166
1167static PyObject *
Victor Stinner1f151112017-11-23 10:43:14 +01001168init_filters(const _PyCoreConfig *config)
Christian Heimes33fe8092008-04-13 13:53:33 +00001169{
Victor Stinner1f151112017-11-23 10:43:14 +01001170 int dev_mode = config->dev_mode;
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001171
1172 Py_ssize_t count = 2;
1173 if (dev_mode) {
1174 count++;
1175 }
Victor Stinner895862a2017-11-20 09:47:03 -08001176#ifndef Py_DEBUG
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001177 if (!dev_mode) {
1178 count += 3;
1179 }
Victor Stinner895862a2017-11-20 09:47:03 -08001180#endif
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001181 PyObject *filters = PyList_New(count);
Christian Heimes33fe8092008-04-13 13:53:33 +00001182 if (filters == NULL)
1183 return NULL;
1184
Victor Stinnerb98f1712017-11-23 17:13:44 +01001185 size_t pos = 0; /* Post-incremented in each use. */
Victor Stinner895862a2017-11-20 09:47:03 -08001186#ifndef Py_DEBUG
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001187 if (!dev_mode) {
1188 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001189 create_filter(PyExc_DeprecationWarning, &PyId_ignore));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001190 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001191 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001192 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001193 create_filter(PyExc_ImportWarning, &PyId_ignore));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001194 }
Victor Stinner895862a2017-11-20 09:47:03 -08001195#endif
1196
Victor Stinnerb98f1712017-11-23 17:13:44 +01001197 _Py_Identifier *bytes_action;
Christian Heimes33fe8092008-04-13 13:53:33 +00001198 if (Py_BytesWarningFlag > 1)
Victor Stinnerb98f1712017-11-23 17:13:44 +01001199 bytes_action = &PyId_error;
Christian Heimes33fe8092008-04-13 13:53:33 +00001200 else if (Py_BytesWarningFlag)
Victor Stinnerb98f1712017-11-23 17:13:44 +01001201 bytes_action = &PyId_default;
Christian Heimes33fe8092008-04-13 13:53:33 +00001202 else
Victor Stinnerb98f1712017-11-23 17:13:44 +01001203 bytes_action = &PyId_ignore;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001204 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001205 bytes_action));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001206
Victor Stinnerb98f1712017-11-23 17:13:44 +01001207 _Py_Identifier *resource_action;
Georg Brandl08be72d2010-10-24 15:11:22 +00001208 /* resource usage warnings are enabled by default in pydebug mode */
1209#ifdef Py_DEBUG
Victor Stinner21c77302017-11-27 12:11:55 +01001210 resource_action = &PyId_default;
Georg Brandl08be72d2010-10-24 15:11:22 +00001211#else
Victor Stinner21c77302017-11-27 12:11:55 +01001212 resource_action = (dev_mode ? &PyId_default: &PyId_ignore);
Georg Brandl08be72d2010-10-24 15:11:22 +00001213#endif
1214 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1215 resource_action));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001216
1217 if (dev_mode) {
1218 PyList_SET_ITEM(filters, pos++,
Victor Stinnerb98f1712017-11-23 17:13:44 +01001219 create_filter(PyExc_Warning, &PyId_default));
Victor Stinner09f3a8a2017-11-20 17:32:40 -08001220 }
1221
Victor Stinnerb98f1712017-11-23 17:13:44 +01001222 for (size_t x = 0; x < pos; x++) {
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001223 if (PyList_GET_ITEM(filters, x) == NULL) {
1224 Py_DECREF(filters);
1225 return NULL;
1226 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001227 }
1228
1229 return filters;
1230}
1231
Martin v. Löwis1a214512008-06-11 05:26:20 +00001232static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 PyModuleDef_HEAD_INIT,
1234 MODULE_NAME,
1235 warnings__doc__,
1236 0,
1237 warnings_functions,
1238 NULL,
1239 NULL,
1240 NULL,
1241 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001242};
1243
Christian Heimes33fe8092008-04-13 13:53:33 +00001244
Victor Stinner1f151112017-11-23 10:43:14 +01001245PyObject*
1246_PyWarnings_InitWithConfig(const _PyCoreConfig *config)
Christian Heimes33fe8092008-04-13 13:53:33 +00001247{
Brett Cannon0759dd62009-04-01 18:13:07 +00001248 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001249
Martin v. Löwis1a214512008-06-11 05:26:20 +00001250 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001251 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001252 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001253
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001254 if (_PyRuntime.warnings.filters == NULL) {
Victor Stinner1f151112017-11-23 10:43:14 +01001255 _PyRuntime.warnings.filters = init_filters(config);
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001256 if (_PyRuntime.warnings.filters == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001257 return NULL;
1258 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001259 Py_INCREF(_PyRuntime.warnings.filters);
1260 if (PyModule_AddObject(m, "filters", _PyRuntime.warnings.filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001261 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001262
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001263 if (_PyRuntime.warnings.once_registry == NULL) {
1264 _PyRuntime.warnings.once_registry = PyDict_New();
1265 if (_PyRuntime.warnings.once_registry == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001266 return NULL;
1267 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001268 Py_INCREF(_PyRuntime.warnings.once_registry);
1269 if (PyModule_AddObject(m, "_onceregistry",
1270 _PyRuntime.warnings.once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001271 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001272
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001273 if (_PyRuntime.warnings.default_action == NULL) {
1274 _PyRuntime.warnings.default_action = PyUnicode_FromString("default");
1275 if (_PyRuntime.warnings.default_action == NULL)
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001276 return NULL;
1277 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001278 Py_INCREF(_PyRuntime.warnings.default_action);
1279 if (PyModule_AddObject(m, "_defaultaction",
1280 _PyRuntime.warnings.default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001281 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001282
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001283 _PyRuntime.warnings.filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001284 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001285}
Victor Stinner1f151112017-11-23 10:43:14 +01001286
1287
1288PyMODINIT_FUNC
1289_PyWarnings_Init(void)
1290{
1291 PyInterpreterState *interp = PyThreadState_GET()->interp;
1292 const _PyCoreConfig *config = &interp->core_config;
1293 return _PyWarnings_InitWithConfig(config);
1294}