blob: e42b7c3be3db9afbd06a2b69e8eca43f03a5aca6 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
Victor Stinner66b79732020-03-02 15:02:18 +01002#include "pycore_initconfig.h"
Victor Stinner4a3fe082020-04-14 14:26:24 +02003#include "pycore_interp.h" // PyInterpreterState.warnings
Victor Stinnerc9bc2902020-10-27 02:24:34 +01004#include "pycore_long.h" // _PyLong_GetZero()
Victor Stinner4d231bc2019-11-14 13:36:21 +01005#include "pycore_pyerrors.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02006#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinner70364772020-04-29 03:28:46 +02007#include "frameobject.h" // PyFrame_GetBack()
Victor Stinner22f18752016-12-09 18:08:18 +01008#include "clinic/_warnings.c.h"
Christian Heimes33fe8092008-04-13 13:53:33 +00009
10#define MODULE_NAME "_warnings"
Christian Heimes33fe8092008-04-13 13:53:33 +000011
12PyDoc_STRVAR(warnings__doc__,
13MODULE_NAME " provides basic warning filtering support.\n"
14"It is a helper module to speed up interpreter start-up.");
15
Victor Stinnerbd303c12013-11-07 23:07:29 +010016_Py_IDENTIFIER(stderr);
Victor Stinner747f48e2017-12-12 22:59:48 +010017#ifndef Py_DEBUG
Nick Coghlan9b997472018-01-08 12:45:02 +100018_Py_IDENTIFIER(default);
Victor Stinnerb98f1712017-11-23 17:13:44 +010019_Py_IDENTIFIER(ignore);
Victor Stinner747f48e2017-12-12 22:59:48 +010020#endif
Christian Heimes33fe8092008-04-13 13:53:33 +000021
Eric Snow86ea5812019-05-10 13:29:55 -040022
23/*************************************************************************/
24
25typedef struct _warnings_runtime_state WarningsState;
26
27/* Forward declaration of the _warnings module definition. */
28static struct PyModuleDef warningsmodule;
29
Hai Shi46874c22020-01-30 17:20:25 -060030_Py_IDENTIFIER(__name__);
31
Eric Snow86ea5812019-05-10 13:29:55 -040032/* Given a module object, get its per-module state. */
33static WarningsState *
Victor Stinner66b79732020-03-02 15:02:18 +010034warnings_get_state(void)
Eric Snow86ea5812019-05-10 13:29:55 -040035{
Victor Stinner1bcc32f2020-06-10 20:08:26 +020036 PyInterpreterState *interp = _PyInterpreterState_GET();
37 if (interp == NULL) {
38 PyErr_SetString(PyExc_RuntimeError,
39 "warnings_get_state: could not identify "
40 "current interpreter");
Eric Snow86ea5812019-05-10 13:29:55 -040041 return NULL;
42 }
Victor Stinner1bcc32f2020-06-10 20:08:26 +020043 return &interp->warnings;
Eric Snow86ea5812019-05-10 13:29:55 -040044}
45
46/* Clear the given warnings module state. */
47static void
Victor Stinner66b79732020-03-02 15:02:18 +010048warnings_clear_state(WarningsState *st)
Eric Snow86ea5812019-05-10 13:29:55 -040049{
50 Py_CLEAR(st->filters);
51 Py_CLEAR(st->once_registry);
52 Py_CLEAR(st->default_action);
53}
54
55#ifndef Py_DEBUG
56static PyObject *
57create_filter(PyObject *category, _Py_Identifier *id, const char *modname)
58{
59 PyObject *modname_obj = NULL;
60 PyObject *action_str = _PyUnicode_FromId(id);
61 if (action_str == NULL) {
62 return NULL;
63 }
64
65 /* Default to "no module name" for initial filter set */
66 if (modname != NULL) {
67 modname_obj = PyUnicode_InternFromString(modname);
68 if (modname_obj == NULL) {
69 return NULL;
70 }
71 } else {
72 modname_obj = Py_None;
73 }
74
75 /* This assumes the line number is zero for now. */
76 return PyTuple_Pack(5, action_str, Py_None,
Victor Stinnerc9bc2902020-10-27 02:24:34 +010077 category, modname_obj, _PyLong_GetZero());
Eric Snow86ea5812019-05-10 13:29:55 -040078}
79#endif
80
81static PyObject *
82init_filters(void)
83{
84#ifdef Py_DEBUG
85 /* Py_DEBUG builds show all warnings by default */
86 return PyList_New(0);
87#else
88 /* Other builds ignore a number of warning categories by default */
89 PyObject *filters = PyList_New(5);
90 if (filters == NULL) {
91 return NULL;
92 }
93
94 size_t pos = 0; /* Post-incremented in each use. */
95 PyList_SET_ITEM(filters, pos++,
96 create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__"));
97 PyList_SET_ITEM(filters, pos++,
98 create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL));
99 PyList_SET_ITEM(filters, pos++,
100 create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL));
101 PyList_SET_ITEM(filters, pos++,
102 create_filter(PyExc_ImportWarning, &PyId_ignore, NULL));
103 PyList_SET_ITEM(filters, pos++,
104 create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL));
105
106 for (size_t x = 0; x < pos; x++) {
107 if (PyList_GET_ITEM(filters, x) == NULL) {
108 Py_DECREF(filters);
109 return NULL;
110 }
111 }
112 return filters;
113#endif
114}
115
116/* Initialize the given warnings module state. */
117static int
Victor Stinner66b79732020-03-02 15:02:18 +0100118warnings_init_state(WarningsState *st)
Eric Snow86ea5812019-05-10 13:29:55 -0400119{
120 if (st->filters == NULL) {
121 st->filters = init_filters();
122 if (st->filters == NULL) {
123 goto error;
124 }
125 }
126
127 if (st->once_registry == NULL) {
128 st->once_registry = PyDict_New();
129 if (st->once_registry == NULL) {
130 goto error;
131 }
132 }
133
134 if (st->default_action == NULL) {
135 st->default_action = PyUnicode_FromString("default");
136 if (st->default_action == NULL) {
137 goto error;
138 }
139 }
140
141 st->filters_version = 0;
142
143 return 0;
144
145error:
Victor Stinner66b79732020-03-02 15:02:18 +0100146 warnings_clear_state(st);
Eric Snow86ea5812019-05-10 13:29:55 -0400147 return -1;
148}
149
150
151/*************************************************************************/
152
Christian Heimes33fe8092008-04-13 13:53:33 +0000153static int
154check_matched(PyObject *obj, PyObject *arg)
155{
156 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200157 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +0000158 int rc;
159
Nick Coghlan9b997472018-01-08 12:45:02 +1000160 /* A 'None' filter always matches */
Christian Heimes33fe8092008-04-13 13:53:33 +0000161 if (obj == Py_None)
162 return 1;
Nick Coghlan9b997472018-01-08 12:45:02 +1000163
164 /* An internal plain text default filter must match exactly */
165 if (PyUnicode_CheckExact(obj)) {
166 int cmp_result = PyUnicode_Compare(obj, arg);
167 if (cmp_result == -1 && PyErr_Occurred()) {
168 return -1;
169 }
170 return !cmp_result;
171 }
172
173 /* Otherwise assume a regex filter and call its match() method */
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200174 result = _PyObject_CallMethodIdOneArg(obj, &PyId_match, arg);
Christian Heimes33fe8092008-04-13 13:53:33 +0000175 if (result == NULL)
176 return -1;
177
178 rc = PyObject_IsTrue(result);
179 Py_DECREF(result);
180 return rc;
181}
182
183/*
184 Returns a new reference.
185 A NULL return value can mean false or an error.
186*/
187static PyObject *
Victor Stinner82656272017-11-22 23:51:42 +0100188get_warnings_attr(_Py_Identifier *attr_id, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +0000189{
Victor Stinner82656272017-11-22 23:51:42 +0100190 PyObject *warnings_str;
Victor Stinnere98445a2016-03-23 00:54:48 +0100191 PyObject *warnings_module, *obj;
Victor Stinner82656272017-11-22 23:51:42 +0100192 _Py_IDENTIFIER(warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +0000193
Victor Stinner82656272017-11-22 23:51:42 +0100194 warnings_str = _PyUnicode_FromId(&PyId_warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +0000195 if (warnings_str == NULL) {
Victor Stinner82656272017-11-22 23:51:42 +0100196 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000197 }
198
Victor Stinnere98445a2016-03-23 00:54:48 +0100199 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600200 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +0100201 warnings_module = PyImport_Import(warnings_str);
202 if (warnings_module == NULL) {
203 /* Fallback to the C implementation if we cannot get
204 the Python implementation */
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200205 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
206 PyErr_Clear();
207 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000208 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +0100209 }
210 }
211 else {
Nathaniel J. Smithdba976b2018-01-26 11:28:31 -0800212 /* if we're so late into Python finalization that the module dict is
213 gone, then we can't even use PyImport_GetModule without triggering
214 an interpreter abort.
215 */
Victor Stinner81a7be32020-04-14 15:14:01 +0200216 if (!_PyInterpreterState_GET()->modules) {
Nathaniel J. Smithdba976b2018-01-26 11:28:31 -0800217 return NULL;
218 }
Eric Snow3f9eee62017-09-15 16:35:20 -0600219 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +0100220 if (warnings_module == NULL)
221 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +0100222 }
223
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200224 (void)_PyObject_LookupAttrId(warnings_module, attr_id, &obj);
Victor Stinnere98445a2016-03-23 00:54:48 +0100225 Py_DECREF(warnings_module);
226 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +0000227}
228
229
Neal Norwitz32dde222008-04-15 06:43:13 +0000230static PyObject *
Eric Snow86ea5812019-05-10 13:29:55 -0400231get_once_registry(WarningsState *st)
Christian Heimes33fe8092008-04-13 13:53:33 +0000232{
233 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +0100234 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000235
Victor Stinner82656272017-11-22 23:51:42 +0100236 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000237 if (registry == NULL) {
238 if (PyErr_Occurred())
239 return NULL;
Eric Snow86ea5812019-05-10 13:29:55 -0400240 assert(st->once_registry);
241 return st->once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +0000242 }
Oren Milman252033d2017-09-11 09:28:39 +0300243 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200244 PyErr_Format(PyExc_TypeError,
245 MODULE_NAME ".onceregistry must be a dict, "
246 "not '%.200s'",
247 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +0300248 Py_DECREF(registry);
249 return NULL;
250 }
Eric Snow86ea5812019-05-10 13:29:55 -0400251 Py_SETREF(st->once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000252 return registry;
253}
254
255
Brett Cannon0759dd62009-04-01 18:13:07 +0000256static PyObject *
Eric Snow86ea5812019-05-10 13:29:55 -0400257get_default_action(WarningsState *st)
Brett Cannon0759dd62009-04-01 18:13:07 +0000258{
259 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100260 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000261
Victor Stinner82656272017-11-22 23:51:42 +0100262 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000263 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (PyErr_Occurred()) {
265 return NULL;
266 }
Eric Snow86ea5812019-05-10 13:29:55 -0400267 assert(st->default_action);
268 return st->default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000269 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300270 if (!PyUnicode_Check(default_action)) {
271 PyErr_Format(PyExc_TypeError,
272 MODULE_NAME ".defaultaction must be a string, "
273 "not '%.200s'",
274 Py_TYPE(default_action)->tp_name);
275 Py_DECREF(default_action);
276 return NULL;
277 }
Eric Snow86ea5812019-05-10 13:29:55 -0400278 Py_SETREF(st->default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000279 return default_action;
280}
281
282
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400283/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100284static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000285get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
286 PyObject *module, PyObject **item)
287{
Brett Cannon0759dd62009-04-01 18:13:07 +0000288 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000289 Py_ssize_t i;
290 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100291 _Py_IDENTIFIER(filters);
Victor Stinner66b79732020-03-02 15:02:18 +0100292 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -0400293 if (st == NULL) {
294 return NULL;
295 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000296
Victor Stinner82656272017-11-22 23:51:42 +0100297 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000298 if (warnings_filters == NULL) {
299 if (PyErr_Occurred())
300 return NULL;
301 }
302 else {
Eric Snow86ea5812019-05-10 13:29:55 -0400303 Py_SETREF(st->filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000304 }
305
Eric Snow86ea5812019-05-10 13:29:55 -0400306 PyObject *filters = st->filters;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600307 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000308 PyErr_SetString(PyExc_ValueError,
309 MODULE_NAME ".filters must be a list");
310 return NULL;
311 }
312
Eric Snow86ea5812019-05-10 13:29:55 -0400313 /* WarningsState.filters could change while we are iterating over it. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600314 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000315 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
316 Py_ssize_t ln;
317 int is_subclass, good_msg, good_mod;
318
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600319 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400320 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000321 PyErr_Format(PyExc_ValueError,
322 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
323 return NULL;
324 }
325
326 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400327 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000328 action = PyTuple_GET_ITEM(tmp_item, 0);
329 msg = PyTuple_GET_ITEM(tmp_item, 1);
330 cat = PyTuple_GET_ITEM(tmp_item, 2);
331 mod = PyTuple_GET_ITEM(tmp_item, 3);
332 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
333
Oren Milman9d984fd2017-09-12 00:18:09 +0300334 if (!PyUnicode_Check(action)) {
335 PyErr_Format(PyExc_TypeError,
336 "action must be a string, not '%.200s'",
337 Py_TYPE(action)->tp_name);
338 Py_DECREF(tmp_item);
339 return NULL;
340 }
341
Christian Heimes33fe8092008-04-13 13:53:33 +0000342 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400343 if (good_msg == -1) {
344 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100345 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400346 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100347
Christian Heimes33fe8092008-04-13 13:53:33 +0000348 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400349 if (good_mod == -1) {
350 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100351 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400352 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100353
Christian Heimes33fe8092008-04-13 13:53:33 +0000354 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400355 if (is_subclass == -1) {
356 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100357 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400358 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100359
Christian Heimes33fe8092008-04-13 13:53:33 +0000360 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400361 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400362 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000363 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400364 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000365
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400366 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
367 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100368 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400369 }
370
371 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000372 }
373
Eric Snow86ea5812019-05-10 13:29:55 -0400374 action = get_default_action(st);
Brett Cannon0759dd62009-04-01 18:13:07 +0000375 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400376 Py_INCREF(Py_None);
377 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100378 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000379 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000380
Christian Heimes33fe8092008-04-13 13:53:33 +0000381 return NULL;
382}
383
Brett Cannon0759dd62009-04-01 18:13:07 +0000384
Christian Heimes33fe8092008-04-13 13:53:33 +0000385static int
386already_warned(PyObject *registry, PyObject *key, int should_set)
387{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200388 PyObject *version_obj, *already_warned;
389 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000390
391 if (key == NULL)
392 return -1;
393
Victor Stinner66b79732020-03-02 15:02:18 +0100394 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -0400395 if (st == NULL) {
396 return -1;
397 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200398 version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200399 if (version_obj == NULL
400 || !PyLong_CheckExact(version_obj)
Eric Snow86ea5812019-05-10 13:29:55 -0400401 || PyLong_AsLong(version_obj) != st->filters_version)
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +0200402 {
403 if (PyErr_Occurred()) {
404 return -1;
405 }
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200406 PyDict_Clear(registry);
Eric Snow86ea5812019-05-10 13:29:55 -0400407 version_obj = PyLong_FromLong(st->filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200408 if (version_obj == NULL)
409 return -1;
410 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
411 Py_DECREF(version_obj);
412 return -1;
413 }
414 Py_DECREF(version_obj);
415 }
416 else {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200417 already_warned = PyDict_GetItemWithError(registry, key);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200418 if (already_warned != NULL) {
419 int rc = PyObject_IsTrue(already_warned);
420 if (rc != 0)
421 return rc;
422 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200423 else if (PyErr_Occurred()) {
424 return -1;
425 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000426 }
427
428 /* This warning wasn't found in the registry, set it. */
429 if (should_set)
430 return PyDict_SetItem(registry, key, Py_True);
431 return 0;
432}
433
434/* New reference. */
435static PyObject *
436normalize_module(PyObject *filename)
437{
438 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100439 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +0300440 const void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000441 Py_ssize_t len;
442
Victor Stinner9e30aa52011-11-21 02:49:52 +0100443 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000444 if (len < 0)
445 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100446
447 if (len == 0)
448 return PyUnicode_FromString("<unknown>");
449
450 kind = PyUnicode_KIND(filename);
451 data = PyUnicode_DATA(filename);
452
453 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000454 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100455 PyUnicode_READ(kind, data, len-3) == '.' &&
456 PyUnicode_READ(kind, data, len-2) == 'p' &&
457 PyUnicode_READ(kind, data, len-1) == 'y')
458 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100459 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000460 }
461 else {
462 module = filename;
463 Py_INCREF(module);
464 }
465 return module;
466}
467
468static int
469update_registry(PyObject *registry, PyObject *text, PyObject *category,
470 int add_zero)
471{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300472 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000473 int rc;
474
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300475 if (add_zero)
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100476 altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero());
Christian Heimes33fe8092008-04-13 13:53:33 +0000477 else
478 altkey = PyTuple_Pack(2, text, category);
479
480 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000481 Py_XDECREF(altkey);
482 return rc;
483}
484
485static void
Victor Stinner914cde82016-03-19 01:03:51 +0100486show_warning(PyObject *filename, int lineno, PyObject *text,
487 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject *f_stderr;
490 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000491 char lineno_str[128];
492
493 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
494
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200495 name = _PyObject_GetAttrId(category, &PyId___name__);
Hai Shi57c78102020-03-14 21:40:58 +0800496 if (name == NULL) {
Victor Stinnerae233ea2013-10-31 14:51:38 +0100497 goto error;
Hai Shi57c78102020-03-14 21:40:58 +0800498 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000499
Victor Stinnerbd303c12013-11-07 23:07:29 +0100500 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000501 if (f_stderr == NULL) {
502 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100503 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000504 }
505
506 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100507 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
508 goto error;
509 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
510 goto error;
511 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
512 goto error;
513 if (PyFile_WriteString(": ", f_stderr) < 0)
514 goto error;
515 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
516 goto error;
517 if (PyFile_WriteString("\n", f_stderr) < 0)
518 goto error;
519 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000520
521 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000522 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100523 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +0300524 const void *data;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100525 Py_ssize_t i, len;
526 Py_UCS4 ch;
527 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000528
Victor Stinnera4c704b2013-10-29 23:43:41 +0100529 if (PyUnicode_READY(sourceline) < 1)
530 goto error;
531
532 kind = PyUnicode_KIND(sourceline);
533 data = PyUnicode_DATA(sourceline);
534 len = PyUnicode_GET_LENGTH(sourceline);
535 for (i=0; i<len; i++) {
536 ch = PyUnicode_READ(kind, data, i);
537 if (ch != ' ' && ch != '\t' && ch != '\014')
538 break;
539 }
540
541 truncated = PyUnicode_Substring(sourceline, i, len);
542 if (truncated == NULL)
543 goto error;
544
545 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
546 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000547 PyFile_WriteString("\n", f_stderr);
548 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200549 else {
550 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
551 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100552
553error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100554 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000555 PyErr_Clear();
556}
557
Victor Stinner1231a462016-03-19 00:47:17 +0100558static int
559call_show_warning(PyObject *category, PyObject *text, PyObject *message,
560 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100561 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100562{
563 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100564 _Py_IDENTIFIER(_showwarnmsg);
565 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100566
Victor Stinnere98445a2016-03-23 00:54:48 +0100567 /* If the source parameter is set, try to get the Python implementation.
568 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600569 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100570 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100571 if (show_fn == NULL) {
572 if (PyErr_Occurred())
573 return -1;
574 show_warning(filename, lineno, text, category, sourceline);
575 return 0;
576 }
577
578 if (!PyCallable_Check(show_fn)) {
579 PyErr_SetString(PyExc_TypeError,
580 "warnings._showwarnmsg() must be set to a callable");
581 goto error;
582 }
583
Victor Stinner82656272017-11-22 23:51:42 +0100584 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100585 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200586 if (!PyErr_Occurred()) {
587 PyErr_SetString(PyExc_RuntimeError,
588 "unable to get warnings.WarningMessage");
589 }
Victor Stinner1231a462016-03-19 00:47:17 +0100590 goto error;
591 }
592
593 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100594 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100595 NULL);
596 Py_DECREF(warnmsg_cls);
597 if (msg == NULL)
598 goto error;
599
Petr Viktorinffd97532020-02-11 17:46:57 +0100600 res = PyObject_CallOneArg(show_fn, msg);
Victor Stinner1231a462016-03-19 00:47:17 +0100601 Py_DECREF(show_fn);
602 Py_DECREF(msg);
603
604 if (res == NULL)
605 return -1;
606
607 Py_DECREF(res);
608 return 0;
609
610error:
611 Py_XDECREF(show_fn);
612 return -1;
613}
614
Christian Heimes33fe8092008-04-13 13:53:33 +0000615static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000617 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100618 PyObject *module, PyObject *registry, PyObject *sourceline,
619 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000620{
621 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400622 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100623 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000624 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100626 /* module can be None if a warning is emitted late during Python shutdown.
627 In this case, the Python warnings module was probably unloaded, filters
628 are no more available to choose as action. It is safer to ignore the
629 warning and do nothing. */
630 if (module == Py_None)
631 Py_RETURN_NONE;
632
Brett Cannondb734912008-06-27 00:52:15 +0000633 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300634 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000635 return NULL;
636 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000637
638 /* Normalize module. */
639 if (module == NULL) {
640 module = normalize_module(filename);
641 if (module == NULL)
642 return NULL;
643 }
644 else
645 Py_INCREF(module);
646
647 /* Normalize message. */
648 Py_INCREF(message); /* DECREF'ed in cleanup. */
649 rc = PyObject_IsInstance(message, PyExc_Warning);
650 if (rc == -1) {
651 goto cleanup;
652 }
653 if (rc == 1) {
654 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000655 if (text == NULL)
656 goto cleanup;
Victor Stinnera102ed72020-02-07 02:24:48 +0100657 category = (PyObject*)Py_TYPE(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000658 }
659 else {
660 text = message;
Petr Viktorinffd97532020-02-11 17:46:57 +0100661 message = PyObject_CallOneArg(category, message);
Brett Cannondb734912008-06-27 00:52:15 +0000662 if (message == NULL)
663 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000664 }
665
666 lineno_obj = PyLong_FromLong(lineno);
667 if (lineno_obj == NULL)
668 goto cleanup;
669
Victor Stinner22f18752016-12-09 18:08:18 +0100670 if (source == Py_None) {
671 source = NULL;
672 }
673
Christian Heimes33fe8092008-04-13 13:53:33 +0000674 /* Create key. */
675 key = PyTuple_Pack(3, text, category, lineno_obj);
676 if (key == NULL)
677 goto cleanup;
678
Brett Cannondb734912008-06-27 00:52:15 +0000679 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000680 rc = already_warned(registry, key, 0);
681 if (rc == -1)
682 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000684 goto return_none;
685 /* Else this warning hasn't been generated before. */
686 }
687
688 action = get_filter(category, text, lineno, module, &item);
689 if (action == NULL)
690 goto cleanup;
691
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200692 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000693 PyErr_SetObject(category, message);
694 goto cleanup;
695 }
696
Victor Stinnerc9758782017-11-27 16:57:07 +0100697 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
698 goto return_none;
699 }
700
Christian Heimes33fe8092008-04-13 13:53:33 +0000701 /* Store in the registry that we've been here, *except* when the action
702 is "always". */
703 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200704 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000705 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100706 PyDict_SetItem(registry, key, Py_True) < 0)
707 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000708 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100709 }
710
711 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000712 if (registry == NULL || registry == Py_None) {
Victor Stinner66b79732020-03-02 15:02:18 +0100713 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -0400714 if (st == NULL) {
715 goto cleanup;
716 }
717 registry = get_once_registry(st);
Christian Heimes33fe8092008-04-13 13:53:33 +0000718 if (registry == NULL)
719 goto cleanup;
720 }
Eric Snow86ea5812019-05-10 13:29:55 -0400721 /* WarningsState.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000723 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200724 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000725 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000726 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000728 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200729 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000730 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100731 "Unrecognized action (%R) in warnings.filters:\n %R",
732 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000733 goto cleanup;
734 }
735 }
736
Christian Heimes1a8501c2008-10-02 19:56:01 +0000737 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000738 goto return_none;
739 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100740 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100741 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100742 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000743 }
744 else /* if (rc == -1) */
745 goto cleanup;
746
747 return_none:
748 result = Py_None;
749 Py_INCREF(result);
750
751 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400752 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000753 Py_XDECREF(key);
754 Py_XDECREF(text);
755 Py_XDECREF(lineno_obj);
756 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000757 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000758 return result; /* Py_None or NULL. */
759}
760
Larry Hastings714e4932015-09-06 00:39:37 -0700761static int
762is_internal_frame(PyFrameObject *frame)
763{
764 static PyObject *importlib_string = NULL;
765 static PyObject *bootstrap_string = NULL;
Larry Hastings714e4932015-09-06 00:39:37 -0700766 int contains;
767
768 if (importlib_string == NULL) {
769 importlib_string = PyUnicode_FromString("importlib");
770 if (importlib_string == NULL) {
771 return 0;
772 }
773
774 bootstrap_string = PyUnicode_FromString("_bootstrap");
775 if (bootstrap_string == NULL) {
776 Py_DECREF(importlib_string);
777 return 0;
778 }
779 Py_INCREF(importlib_string);
780 Py_INCREF(bootstrap_string);
781 }
782
Victor Stinnera42ca742020-04-28 19:01:31 +0200783 if (frame == NULL) {
Larry Hastings714e4932015-09-06 00:39:37 -0700784 return 0;
785 }
Victor Stinnera42ca742020-04-28 19:01:31 +0200786
787 PyCodeObject *code = PyFrame_GetCode(frame);
Victor Stinnera42ca742020-04-28 19:01:31 +0200788 PyObject *filename = code->co_filename;
Victor Stinner8852ad42020-04-29 01:28:13 +0200789 Py_DECREF(code);
790
Victor Stinnera42ca742020-04-28 19:01:31 +0200791 if (filename == NULL) {
792 return 0;
793 }
Larry Hastings714e4932015-09-06 00:39:37 -0700794 if (!PyUnicode_Check(filename)) {
795 return 0;
796 }
Victor Stinnera42ca742020-04-28 19:01:31 +0200797
Larry Hastings714e4932015-09-06 00:39:37 -0700798 contains = PyUnicode_Contains(filename, importlib_string);
799 if (contains < 0) {
800 return 0;
801 }
802 else if (contains > 0) {
803 contains = PyUnicode_Contains(filename, bootstrap_string);
804 if (contains < 0) {
805 return 0;
806 }
807 else if (contains > 0) {
808 return 1;
809 }
810 }
811
812 return 0;
813}
814
815static PyFrameObject *
816next_external_frame(PyFrameObject *frame)
817{
818 do {
Victor Stinner70364772020-04-29 03:28:46 +0200819 PyFrameObject *back = PyFrame_GetBack(frame);
820 Py_DECREF(frame);
821 frame = back;
Larry Hastings714e4932015-09-06 00:39:37 -0700822 } while (frame != NULL && is_internal_frame(frame));
823
824 return frame;
825}
826
Christian Heimes33fe8092008-04-13 13:53:33 +0000827/* filename, module, and registry are new refs, globals is borrowed */
828/* Returns 0 on error (no new refs), 1 on success */
829static int
830setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
831 PyObject **module, PyObject **registry)
832{
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200833 _Py_IDENTIFIER(__warningregistry__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000834 PyObject *globals;
835
Thomas Kluyver11a89662018-06-08 21:28:37 +0200836 /* Setup globals, filename and lineno. */
Victor Stinner70364772020-04-29 03:28:46 +0200837 PyThreadState *tstate = _PyThreadState_GET();
838 PyFrameObject *f = PyThreadState_GetFrame(tstate);
Larry Hastings714e4932015-09-06 00:39:37 -0700839 // Stack level comparisons to Python code is off by one as there is no
840 // warnings-related stack level to avoid.
841 if (stack_level <= 0 || is_internal_frame(f)) {
842 while (--stack_level > 0 && f != NULL) {
Victor Stinner70364772020-04-29 03:28:46 +0200843 PyFrameObject *back = PyFrame_GetBack(f);
844 Py_DECREF(f);
845 f = back;
Larry Hastings714e4932015-09-06 00:39:37 -0700846 }
847 }
848 else {
849 while (--stack_level > 0 && f != NULL) {
850 f = next_external_frame(f);
851 }
852 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000853
854 if (f == NULL) {
Victor Stinner45df61f2020-11-02 23:17:46 +0100855 globals = tstate->interp->sysdict;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200856 *filename = PyUnicode_FromString("sys");
Christian Heimes33fe8092008-04-13 13:53:33 +0000857 *lineno = 1;
858 }
859 else {
860 globals = f->f_globals;
Victor Stinner8852ad42020-04-29 01:28:13 +0200861 PyCodeObject *code = PyFrame_GetCode(f);
862 *filename = code->co_filename;
863 Py_DECREF(code);
Thomas Kluyver11a89662018-06-08 21:28:37 +0200864 Py_INCREF(*filename);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000865 *lineno = PyFrame_GetLineNumber(f);
Victor Stinner70364772020-04-29 03:28:46 +0200866 Py_DECREF(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000867 }
868
869 *module = NULL;
870
871 /* Setup registry. */
872 assert(globals != NULL);
873 assert(PyDict_Check(globals));
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200874 *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000875 if (*registry == NULL) {
876 int rc;
877
Victor Stinner70364772020-04-29 03:28:46 +0200878 if (_PyErr_Occurred(tstate)) {
Serhiy Storchaka2d2f8552020-03-02 22:05:08 +0200879 goto handle_error;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200880 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000881 *registry = PyDict_New();
882 if (*registry == NULL)
Serhiy Storchaka2d2f8552020-03-02 22:05:08 +0200883 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000884
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200885 rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000886 if (rc < 0)
887 goto handle_error;
888 }
889 else
890 Py_INCREF(*registry);
891
892 /* Setup module. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200893 *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
Oren Milman5d3e8002017-09-24 21:28:42 +0300894 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
895 Py_INCREF(*module);
896 }
Victor Stinner70364772020-04-29 03:28:46 +0200897 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200898 goto handle_error;
899 }
Oren Milman5d3e8002017-09-24 21:28:42 +0300900 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000901 *module = PyUnicode_FromString("<string>");
902 if (*module == NULL)
903 goto handle_error;
904 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000905
Christian Heimes33fe8092008-04-13 13:53:33 +0000906 return 1;
907
908 handle_error:
Christian Heimes33fe8092008-04-13 13:53:33 +0000909 Py_XDECREF(*registry);
910 Py_XDECREF(*module);
Serhiy Storchakaae75a292020-03-03 19:43:29 +0200911 Py_DECREF(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000912 return 0;
913}
914
915static PyObject *
916get_category(PyObject *message, PyObject *category)
917{
918 int rc;
919
920 /* Get category. */
921 rc = PyObject_IsInstance(message, PyExc_Warning);
922 if (rc == -1)
923 return NULL;
924
925 if (rc == 1)
Victor Stinnera102ed72020-02-07 02:24:48 +0100926 category = (PyObject*)Py_TYPE(message);
Berker Peksagd8089e02014-07-11 19:50:25 +0300927 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000928 category = PyExc_UserWarning;
929
930 /* Validate category. */
931 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300932 /* category is not a subclass of PyExc_Warning or
933 PyObject_IsSubclass raised an error */
934 if (rc == -1 || rc == 0) {
935 PyErr_Format(PyExc_TypeError,
936 "category must be a Warning subclass, not '%s'",
937 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000938 return NULL;
939 }
940
941 return category;
942}
943
944static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100945do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
946 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000947{
948 PyObject *filename, *module, *registry, *res;
949 int lineno;
950
951 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
952 return NULL;
953
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100954 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100955 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000956 Py_DECREF(filename);
957 Py_DECREF(registry);
958 Py_DECREF(module);
959 return res;
960}
961
Victor Stinner22f18752016-12-09 18:08:18 +0100962/*[clinic input]
963warn as warnings_warn
964
965 message: object
966 category: object = None
967 stacklevel: Py_ssize_t = 1
968 source: object = None
969
970Issue a warning, or maybe ignore it or raise an exception.
971[clinic start generated code]*/
972
Christian Heimes33fe8092008-04-13 13:53:33 +0000973static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100974warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
975 Py_ssize_t stacklevel, PyObject *source)
976/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000977{
Christian Heimes33fe8092008-04-13 13:53:33 +0000978 category = get_category(message, category);
979 if (category == NULL)
980 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100981 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000982}
983
984static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200985get_source_line(PyObject *module_globals, int lineno)
986{
987 _Py_IDENTIFIER(get_source);
988 _Py_IDENTIFIER(__loader__);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200989 PyObject *loader;
990 PyObject *module_name;
991 PyObject *get_source;
992 PyObject *source;
993 PyObject *source_list;
994 PyObject *source_line;
995
996 /* Check/get the requisite pieces needed for the loader. */
997 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
998 if (loader == NULL) {
999 return NULL;
1000 }
1001 Py_INCREF(loader);
1002 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
1003 if (!module_name) {
1004 Py_DECREF(loader);
1005 return NULL;
1006 }
1007 Py_INCREF(module_name);
1008
1009 /* Make sure the loader implements the optional get_source() method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001010 (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source);
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001011 Py_DECREF(loader);
1012 if (!get_source) {
1013 Py_DECREF(module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001014 return NULL;
1015 }
1016 /* Call get_source() to get the source code. */
Petr Viktorinffd97532020-02-11 17:46:57 +01001017 source = PyObject_CallOneArg(get_source, module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001018 Py_DECREF(get_source);
1019 Py_DECREF(module_name);
1020 if (!source) {
1021 return NULL;
1022 }
1023 if (source == Py_None) {
1024 Py_DECREF(source);
1025 return NULL;
1026 }
1027
1028 /* Split the source into lines. */
1029 source_list = PyUnicode_Splitlines(source, 0);
1030 Py_DECREF(source);
1031 if (!source_list) {
1032 return NULL;
1033 }
1034
1035 /* Get the source line. */
1036 source_line = PyList_GetItem(source_list, lineno-1);
1037 Py_XINCREF(source_line);
1038 Py_DECREF(source_list);
1039 return source_line;
1040}
1041
1042static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +00001043warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
1044{
1045 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +01001046 "module", "registry", "module_globals",
1047 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +00001048 PyObject *message;
1049 PyObject *category;
1050 PyObject *filename;
1051 int lineno;
1052 PyObject *module = NULL;
1053 PyObject *registry = NULL;
1054 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +01001055 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001056 PyObject *source_line = NULL;
1057 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +00001058
Victor Stinner914cde82016-03-19 01:03:51 +01001059 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +00001060 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +01001061 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +00001062 return NULL;
1063
Victor Stinnerb0565622018-05-15 20:42:12 +02001064 if (module_globals && module_globals != Py_None) {
1065 if (!PyDict_Check(module_globals)) {
1066 PyErr_Format(PyExc_TypeError,
1067 "module_globals must be a dict, not '%.200s'",
1068 Py_TYPE(module_globals)->tp_name);
1069 return NULL;
1070 }
1071
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001072 source_line = get_source_line(module_globals, lineno);
1073 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001074 return NULL;
1075 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001076 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001077 returned = warn_explicit(category, message, filename, lineno, module,
1078 registry, source_line, sourceobj);
1079 Py_XDECREF(source_line);
1080 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +00001081}
1082
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001083static PyObject *
1084warnings_filters_mutated(PyObject *self, PyObject *args)
1085{
Victor Stinner66b79732020-03-02 15:02:18 +01001086 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -04001087 if (st == NULL) {
1088 return NULL;
1089 }
1090 st->filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001091 Py_RETURN_NONE;
1092}
1093
Christian Heimes33fe8092008-04-13 13:53:33 +00001094
1095/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001096
1097static int
1098warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +01001099 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +00001100{
1101 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +00001102
1103 if (category == NULL)
1104 category = PyExc_RuntimeWarning;
1105
Victor Stinner914cde82016-03-19 01:03:51 +01001106 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +00001107 if (res == NULL)
1108 return -1;
1109 Py_DECREF(res);
1110
1111 return 0;
1112}
1113
Victor Stinner914cde82016-03-19 01:03:51 +01001114static int
1115_PyErr_WarnFormatV(PyObject *source,
1116 PyObject *category, Py_ssize_t stack_level,
1117 const char *format, va_list vargs)
1118{
1119 PyObject *message;
1120 int res;
1121
1122 message = PyUnicode_FromFormatV(format, vargs);
1123 if (message == NULL)
1124 return -1;
1125
1126 res = warn_unicode(category, message, stack_level, source);
1127 Py_DECREF(message);
1128 return res;
1129}
1130
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001131int
1132PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
1133 const char *format, ...)
1134{
Victor Stinner914cde82016-03-19 01:03:51 +01001135 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001136 va_list vargs;
1137
1138#ifdef HAVE_STDARG_PROTOTYPES
1139 va_start(vargs, format);
1140#else
1141 va_start(vargs);
1142#endif
Victor Stinner914cde82016-03-19 01:03:51 +01001143 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001144 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +01001145 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001146}
1147
Victor Stinner8d84adc2020-03-31 17:25:12 +02001148static int
1149_PyErr_WarnFormat(PyObject *source, PyObject *category, Py_ssize_t stack_level,
1150 const char *format, ...)
1151{
1152 int res;
1153 va_list vargs;
1154
1155#ifdef HAVE_STDARG_PROTOTYPES
1156 va_start(vargs, format);
1157#else
1158 va_start(vargs);
1159#endif
1160 res = _PyErr_WarnFormatV(source, category, stack_level, format, vargs);
1161 va_end(vargs);
1162 return res;
1163}
1164
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001165int
Victor Stinner914cde82016-03-19 01:03:51 +01001166PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
1167 const char *format, ...)
1168{
1169 int res;
1170 va_list vargs;
1171
1172#ifdef HAVE_STDARG_PROTOTYPES
1173 va_start(vargs, format);
1174#else
1175 va_start(vargs);
1176#endif
1177 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1178 stack_level, format, vargs);
1179 va_end(vargs);
1180 return res;
1181}
1182
1183
1184int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001185PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1186{
1187 int ret;
1188 PyObject *message = PyUnicode_FromString(text);
1189 if (message == NULL)
1190 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001191 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001192 Py_DECREF(message);
1193 return ret;
1194}
1195
Ezio Melotti42da6632011-03-15 05:18:48 +02001196/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001197 Use PyErr_WarnEx instead. */
1198
1199#undef PyErr_Warn
1200
Benjamin Petersone5024512018-09-12 12:06:42 -07001201int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001202PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001203{
1204 return PyErr_WarnEx(category, text, 1);
1205}
1206
1207/* Warning with explicit origin */
1208int
Victor Stinner14e461d2013-08-26 22:28:21 +02001209PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1210 PyObject *filename, int lineno,
1211 PyObject *module, PyObject *registry)
1212{
1213 PyObject *res;
1214 if (category == NULL)
1215 category = PyExc_RuntimeWarning;
1216 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001217 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001218 if (res == NULL)
1219 return -1;
1220 Py_DECREF(res);
1221 return 0;
1222}
1223
1224int
Christian Heimes33fe8092008-04-13 13:53:33 +00001225PyErr_WarnExplicit(PyObject *category, const char *text,
1226 const char *filename_str, int lineno,
1227 const char *module_str, PyObject *registry)
1228{
Christian Heimes33fe8092008-04-13 13:53:33 +00001229 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001230 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001231 PyObject *module = NULL;
1232 int ret = -1;
1233
1234 if (message == NULL || filename == NULL)
1235 goto exit;
1236 if (module_str != NULL) {
1237 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001238 if (module == NULL)
1239 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001240 }
1241
Victor Stinner14e461d2013-08-26 22:28:21 +02001242 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1243 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001244
1245 exit:
1246 Py_XDECREF(message);
1247 Py_XDECREF(module);
1248 Py_XDECREF(filename);
1249 return ret;
1250}
1251
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001252int
1253PyErr_WarnExplicitFormat(PyObject *category,
1254 const char *filename_str, int lineno,
1255 const char *module_str, PyObject *registry,
1256 const char *format, ...)
1257{
1258 PyObject *message;
1259 PyObject *module = NULL;
1260 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1261 int ret = -1;
1262 va_list vargs;
1263
1264 if (filename == NULL)
1265 goto exit;
1266 if (module_str != NULL) {
1267 module = PyUnicode_FromString(module_str);
1268 if (module == NULL)
1269 goto exit;
1270 }
1271
1272#ifdef HAVE_STDARG_PROTOTYPES
1273 va_start(vargs, format);
1274#else
1275 va_start(vargs);
1276#endif
1277 message = PyUnicode_FromFormatV(format, vargs);
1278 if (message != NULL) {
1279 PyObject *res;
1280 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001281 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001282 Py_DECREF(message);
1283 if (res != NULL) {
1284 Py_DECREF(res);
1285 ret = 0;
1286 }
1287 }
1288 va_end(vargs);
1289exit:
1290 Py_XDECREF(module);
1291 Py_XDECREF(filename);
1292 return ret;
1293}
1294
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001295void
1296_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
1297{
1298 /* First, we attempt to funnel the warning through
1299 warnings._warn_unawaited_coroutine.
1300
1301 This could raise an exception, due to:
1302 - a bug
1303 - some kind of shutdown-related brokenness
1304 - succeeding, but with an "error" warning filter installed, so the
1305 warning is converted into a RuntimeWarning exception
1306
1307 In the first two cases, we want to print the error (so we know what it
1308 is!), and then print a warning directly as a fallback. In the last
1309 case, we want to print the error (since it's the warning!), but *not*
1310 do a fallback. And after we print the error we can't check for what
1311 type of error it was (because PyErr_WriteUnraisable clears it), so we
1312 need a flag to keep track.
1313
1314 Since this is called from __del__ context, it's careful to never raise
1315 an exception.
1316 */
1317 _Py_IDENTIFIER(_warn_unawaited_coroutine);
1318 int warned = 0;
1319 PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1);
1320 if (fn) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001321 PyObject *res = PyObject_CallOneArg(fn, coro);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001322 Py_DECREF(fn);
1323 if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) {
1324 warned = 1;
1325 }
1326 Py_XDECREF(res);
1327 }
1328
1329 if (PyErr_Occurred()) {
1330 PyErr_WriteUnraisable(coro);
1331 }
1332 if (!warned) {
Victor Stinner8d84adc2020-03-31 17:25:12 +02001333 if (_PyErr_WarnFormat(coro, PyExc_RuntimeWarning, 1,
1334 "coroutine '%S' was never awaited",
1335 ((PyCoroObject *)coro)->cr_qualname) < 0)
Yury Selivanov35103342018-01-21 20:47:04 -05001336 {
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001337 PyErr_WriteUnraisable(coro);
1338 }
1339 }
1340}
Christian Heimes33fe8092008-04-13 13:53:33 +00001341
Christian Heimes33fe8092008-04-13 13:53:33 +00001342PyDoc_STRVAR(warn_explicit_doc,
Hansraj Das5dfbb4d2019-10-08 14:26:07 +05301343"Low-level interface to warnings functionality.");
Christian Heimes33fe8092008-04-13 13:53:33 +00001344
1345static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001346 WARNINGS_WARN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001347 {"warn_explicit", (PyCFunction)(void(*)(void))warnings_warn_explicit,
Christian Heimes33fe8092008-04-13 13:53:33 +00001348 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001349 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1350 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001351 /* XXX(brett.cannon): add showwarning? */
1352 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001354};
1355
1356
Martin v. Löwis1a214512008-06-11 05:26:20 +00001357static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 PyModuleDef_HEAD_INIT,
Eric Snow86ea5812019-05-10 13:29:55 -04001359 MODULE_NAME, /* m_name */
1360 warnings__doc__, /* m_doc */
1361 0, /* m_size */
1362 warnings_functions, /* m_methods */
1363 NULL, /* m_reload */
1364 NULL, /* m_traverse */
1365 NULL, /* m_clear */
1366 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001367};
1368
Christian Heimes33fe8092008-04-13 13:53:33 +00001369
Victor Stinner66b79732020-03-02 15:02:18 +01001370PyStatus
1371_PyWarnings_InitState(PyThreadState *tstate)
1372{
1373 if (warnings_init_state(&tstate->interp->warnings) < 0) {
1374 return _PyStatus_ERR("can't initialize warnings");
1375 }
1376 return _PyStatus_OK();
1377}
1378
1379
Victor Stinner5d862462017-12-19 11:35:58 +01001380PyMODINIT_FUNC
1381_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001382{
Brett Cannon0759dd62009-04-01 18:13:07 +00001383 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001384
Martin v. Löwis1a214512008-06-11 05:26:20 +00001385 m = PyModule_Create(&warningsmodule);
Eric Snow86ea5812019-05-10 13:29:55 -04001386 if (m == NULL) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001387 return NULL;
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001388 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001389
Victor Stinner66b79732020-03-02 15:02:18 +01001390 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -04001391 if (st == NULL) {
1392 goto error;
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001393 }
Victor Stinner66b79732020-03-02 15:02:18 +01001394 if (warnings_init_state(st) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001395 goto error;
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001396 }
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001397
Victor Stinner58ca33b2020-11-04 17:33:06 +01001398 if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001399 goto error;
1400 }
Victor Stinner58ca33b2020-11-04 17:33:06 +01001401 if (PyModule_AddObjectRef(m, "_onceregistry", st->once_registry) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001402 goto error;
1403 }
Victor Stinner58ca33b2020-11-04 17:33:06 +01001404 if (PyModule_AddObjectRef(m, "_defaultaction", st->default_action) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001405 goto error;
1406 }
1407
Martin v. Löwis1a214512008-06-11 05:26:20 +00001408 return m;
Eric Snow86ea5812019-05-10 13:29:55 -04001409
1410error:
1411 if (st != NULL) {
Victor Stinner66b79732020-03-02 15:02:18 +01001412 warnings_clear_state(st);
Eric Snow86ea5812019-05-10 13:29:55 -04001413 }
1414 Py_DECREF(m);
1415 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001416}
Victor Stinner87d23a02019-04-26 05:49:26 +02001417
Eric Snow86ea5812019-05-10 13:29:55 -04001418// We need this to ensure that warnings still work until late in finalization.
Victor Stinner87d23a02019-04-26 05:49:26 +02001419void
Eric Snow86ea5812019-05-10 13:29:55 -04001420_PyWarnings_Fini(PyInterpreterState *interp)
Victor Stinner87d23a02019-04-26 05:49:26 +02001421{
Victor Stinner66b79732020-03-02 15:02:18 +01001422 warnings_clear_state(&interp->warnings);
Victor Stinner87d23a02019-04-26 05:49:26 +02001423}