blob: 8d33fbe0f878b8a709ba6d662893fe6e134467b3 [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. */
Victor Stinneref75a622020-11-12 15:14:13 +0100117int
118_PyWarnings_InitState(PyThreadState *tstate)
Eric Snow86ea5812019-05-10 13:29:55 -0400119{
Victor Stinneref75a622020-11-12 15:14:13 +0100120 WarningsState *st = &tstate->interp->warnings;
121
Eric Snow86ea5812019-05-10 13:29:55 -0400122 if (st->filters == NULL) {
123 st->filters = init_filters();
124 if (st->filters == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100125 return -1;
Eric Snow86ea5812019-05-10 13:29:55 -0400126 }
127 }
128
129 if (st->once_registry == NULL) {
130 st->once_registry = PyDict_New();
131 if (st->once_registry == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100132 return -1;
Eric Snow86ea5812019-05-10 13:29:55 -0400133 }
134 }
135
136 if (st->default_action == NULL) {
137 st->default_action = PyUnicode_FromString("default");
138 if (st->default_action == NULL) {
Victor Stinneref75a622020-11-12 15:14:13 +0100139 return -1;
Eric Snow86ea5812019-05-10 13:29:55 -0400140 }
141 }
142
143 st->filters_version = 0;
Eric Snow86ea5812019-05-10 13:29:55 -0400144 return 0;
Eric Snow86ea5812019-05-10 13:29:55 -0400145}
146
147
148/*************************************************************************/
149
Christian Heimes33fe8092008-04-13 13:53:33 +0000150static int
151check_matched(PyObject *obj, PyObject *arg)
152{
153 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200154 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +0000155 int rc;
156
Nick Coghlan9b997472018-01-08 12:45:02 +1000157 /* A 'None' filter always matches */
Christian Heimes33fe8092008-04-13 13:53:33 +0000158 if (obj == Py_None)
159 return 1;
Nick Coghlan9b997472018-01-08 12:45:02 +1000160
161 /* An internal plain text default filter must match exactly */
162 if (PyUnicode_CheckExact(obj)) {
163 int cmp_result = PyUnicode_Compare(obj, arg);
164 if (cmp_result == -1 && PyErr_Occurred()) {
165 return -1;
166 }
167 return !cmp_result;
168 }
169
170 /* Otherwise assume a regex filter and call its match() method */
Jeroen Demeyer59ad1102019-07-11 10:59:05 +0200171 result = _PyObject_CallMethodIdOneArg(obj, &PyId_match, arg);
Christian Heimes33fe8092008-04-13 13:53:33 +0000172 if (result == NULL)
173 return -1;
174
175 rc = PyObject_IsTrue(result);
176 Py_DECREF(result);
177 return rc;
178}
179
180/*
181 Returns a new reference.
182 A NULL return value can mean false or an error.
183*/
184static PyObject *
Victor Stinner82656272017-11-22 23:51:42 +0100185get_warnings_attr(_Py_Identifier *attr_id, int try_import)
Christian Heimes33fe8092008-04-13 13:53:33 +0000186{
Victor Stinner82656272017-11-22 23:51:42 +0100187 PyObject *warnings_str;
Victor Stinnere98445a2016-03-23 00:54:48 +0100188 PyObject *warnings_module, *obj;
Victor Stinner82656272017-11-22 23:51:42 +0100189 _Py_IDENTIFIER(warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +0000190
Victor Stinner82656272017-11-22 23:51:42 +0100191 warnings_str = _PyUnicode_FromId(&PyId_warnings);
Christian Heimes33fe8092008-04-13 13:53:33 +0000192 if (warnings_str == NULL) {
Victor Stinner82656272017-11-22 23:51:42 +0100193 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000194 }
195
Victor Stinnere98445a2016-03-23 00:54:48 +0100196 /* don't try to import after the start of the Python finallization */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600197 if (try_import && !_Py_IsFinalizing()) {
Victor Stinnere98445a2016-03-23 00:54:48 +0100198 warnings_module = PyImport_Import(warnings_str);
199 if (warnings_module == NULL) {
200 /* Fallback to the C implementation if we cannot get
201 the Python implementation */
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200202 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
203 PyErr_Clear();
204 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000205 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +0100206 }
207 }
208 else {
Nathaniel J. Smithdba976b2018-01-26 11:28:31 -0800209 /* if we're so late into Python finalization that the module dict is
210 gone, then we can't even use PyImport_GetModule without triggering
211 an interpreter abort.
212 */
Victor Stinner81a7be32020-04-14 15:14:01 +0200213 if (!_PyInterpreterState_GET()->modules) {
Nathaniel J. Smithdba976b2018-01-26 11:28:31 -0800214 return NULL;
215 }
Eric Snow3f9eee62017-09-15 16:35:20 -0600216 warnings_module = PyImport_GetModule(warnings_str);
Victor Stinner023654f2016-03-23 17:48:22 +0100217 if (warnings_module == NULL)
218 return NULL;
Victor Stinnere98445a2016-03-23 00:54:48 +0100219 }
220
Serhiy Storchakaf320be72018-01-25 10:49:40 +0200221 (void)_PyObject_LookupAttrId(warnings_module, attr_id, &obj);
Victor Stinnere98445a2016-03-23 00:54:48 +0100222 Py_DECREF(warnings_module);
223 return obj;
Christian Heimes33fe8092008-04-13 13:53:33 +0000224}
225
226
Neal Norwitz32dde222008-04-15 06:43:13 +0000227static PyObject *
Eric Snow86ea5812019-05-10 13:29:55 -0400228get_once_registry(WarningsState *st)
Christian Heimes33fe8092008-04-13 13:53:33 +0000229{
230 PyObject *registry;
Victor Stinner82656272017-11-22 23:51:42 +0100231 _Py_IDENTIFIER(onceregistry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000232
Victor Stinner82656272017-11-22 23:51:42 +0100233 registry = get_warnings_attr(&PyId_onceregistry, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000234 if (registry == NULL) {
235 if (PyErr_Occurred())
236 return NULL;
Eric Snow86ea5812019-05-10 13:29:55 -0400237 assert(st->once_registry);
238 return st->once_registry;
Christian Heimes33fe8092008-04-13 13:53:33 +0000239 }
Oren Milman252033d2017-09-11 09:28:39 +0300240 if (!PyDict_Check(registry)) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200241 PyErr_Format(PyExc_TypeError,
242 MODULE_NAME ".onceregistry must be a dict, "
243 "not '%.200s'",
244 Py_TYPE(registry)->tp_name);
Oren Milman252033d2017-09-11 09:28:39 +0300245 Py_DECREF(registry);
246 return NULL;
247 }
Eric Snow86ea5812019-05-10 13:29:55 -0400248 Py_SETREF(st->once_registry, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000249 return registry;
250}
251
252
Brett Cannon0759dd62009-04-01 18:13:07 +0000253static PyObject *
Eric Snow86ea5812019-05-10 13:29:55 -0400254get_default_action(WarningsState *st)
Brett Cannon0759dd62009-04-01 18:13:07 +0000255{
256 PyObject *default_action;
Victor Stinner82656272017-11-22 23:51:42 +0100257 _Py_IDENTIFIER(defaultaction);
Brett Cannon0759dd62009-04-01 18:13:07 +0000258
Victor Stinner82656272017-11-22 23:51:42 +0100259 default_action = get_warnings_attr(&PyId_defaultaction, 0);
Brett Cannon0759dd62009-04-01 18:13:07 +0000260 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (PyErr_Occurred()) {
262 return NULL;
263 }
Eric Snow86ea5812019-05-10 13:29:55 -0400264 assert(st->default_action);
265 return st->default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000266 }
Oren Milman9d984fd2017-09-12 00:18:09 +0300267 if (!PyUnicode_Check(default_action)) {
268 PyErr_Format(PyExc_TypeError,
269 MODULE_NAME ".defaultaction must be a string, "
270 "not '%.200s'",
271 Py_TYPE(default_action)->tp_name);
272 Py_DECREF(default_action);
273 return NULL;
274 }
Eric Snow86ea5812019-05-10 13:29:55 -0400275 Py_SETREF(st->default_action, default_action);
Brett Cannon0759dd62009-04-01 18:13:07 +0000276 return default_action;
277}
278
279
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400280/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100281static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000282get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
283 PyObject *module, PyObject **item)
284{
Brett Cannon0759dd62009-04-01 18:13:07 +0000285 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000286 Py_ssize_t i;
287 PyObject *warnings_filters;
Victor Stinner82656272017-11-22 23:51:42 +0100288 _Py_IDENTIFIER(filters);
Victor Stinner66b79732020-03-02 15:02:18 +0100289 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -0400290 if (st == NULL) {
291 return NULL;
292 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000293
Victor Stinner82656272017-11-22 23:51:42 +0100294 warnings_filters = get_warnings_attr(&PyId_filters, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000295 if (warnings_filters == NULL) {
296 if (PyErr_Occurred())
297 return NULL;
298 }
299 else {
Eric Snow86ea5812019-05-10 13:29:55 -0400300 Py_SETREF(st->filters, warnings_filters);
Christian Heimes33fe8092008-04-13 13:53:33 +0000301 }
302
Eric Snow86ea5812019-05-10 13:29:55 -0400303 PyObject *filters = st->filters;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600304 if (filters == NULL || !PyList_Check(filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000305 PyErr_SetString(PyExc_ValueError,
306 MODULE_NAME ".filters must be a list");
307 return NULL;
308 }
309
Eric Snow86ea5812019-05-10 13:29:55 -0400310 /* WarningsState.filters could change while we are iterating over it. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600311 for (i = 0; i < PyList_GET_SIZE(filters); i++) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000312 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
313 Py_ssize_t ln;
314 int is_subclass, good_msg, good_mod;
315
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600316 tmp_item = PyList_GET_ITEM(filters, i);
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400317 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000318 PyErr_Format(PyExc_ValueError,
319 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
320 return NULL;
321 }
322
323 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400324 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000325 action = PyTuple_GET_ITEM(tmp_item, 0);
326 msg = PyTuple_GET_ITEM(tmp_item, 1);
327 cat = PyTuple_GET_ITEM(tmp_item, 2);
328 mod = PyTuple_GET_ITEM(tmp_item, 3);
329 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
330
Oren Milman9d984fd2017-09-12 00:18:09 +0300331 if (!PyUnicode_Check(action)) {
332 PyErr_Format(PyExc_TypeError,
333 "action must be a string, not '%.200s'",
334 Py_TYPE(action)->tp_name);
335 Py_DECREF(tmp_item);
336 return NULL;
337 }
338
Christian Heimes33fe8092008-04-13 13:53:33 +0000339 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400340 if (good_msg == -1) {
341 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100342 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400343 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100344
Christian Heimes33fe8092008-04-13 13:53:33 +0000345 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400346 if (good_mod == -1) {
347 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100348 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400349 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100350
Christian Heimes33fe8092008-04-13 13:53:33 +0000351 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400352 if (is_subclass == -1) {
353 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100354 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400355 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100356
Christian Heimes33fe8092008-04-13 13:53:33 +0000357 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400358 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400359 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000360 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400361 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000362
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400363 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
364 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100365 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400366 }
367
368 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000369 }
370
Eric Snow86ea5812019-05-10 13:29:55 -0400371 action = get_default_action(st);
Brett Cannon0759dd62009-04-01 18:13:07 +0000372 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400373 Py_INCREF(Py_None);
374 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100375 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000376 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000377
Christian Heimes33fe8092008-04-13 13:53:33 +0000378 return NULL;
379}
380
Brett Cannon0759dd62009-04-01 18:13:07 +0000381
Christian Heimes33fe8092008-04-13 13:53:33 +0000382static int
383already_warned(PyObject *registry, PyObject *key, int should_set)
384{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200385 PyObject *version_obj, *already_warned;
386 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000387
388 if (key == NULL)
389 return -1;
390
Victor Stinner66b79732020-03-02 15:02:18 +0100391 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -0400392 if (st == NULL) {
393 return -1;
394 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200395 version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200396 if (version_obj == NULL
397 || !PyLong_CheckExact(version_obj)
Eric Snow86ea5812019-05-10 13:29:55 -0400398 || PyLong_AsLong(version_obj) != st->filters_version)
Serhiy Storchaka8905fcc2018-12-11 08:38:03 +0200399 {
400 if (PyErr_Occurred()) {
401 return -1;
402 }
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200403 PyDict_Clear(registry);
Eric Snow86ea5812019-05-10 13:29:55 -0400404 version_obj = PyLong_FromLong(st->filters_version);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200405 if (version_obj == NULL)
406 return -1;
407 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
408 Py_DECREF(version_obj);
409 return -1;
410 }
411 Py_DECREF(version_obj);
412 }
413 else {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200414 already_warned = PyDict_GetItemWithError(registry, key);
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200415 if (already_warned != NULL) {
416 int rc = PyObject_IsTrue(already_warned);
417 if (rc != 0)
418 return rc;
419 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200420 else if (PyErr_Occurred()) {
421 return -1;
422 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000423 }
424
425 /* This warning wasn't found in the registry, set it. */
426 if (should_set)
427 return PyDict_SetItem(registry, key, Py_True);
428 return 0;
429}
430
431/* New reference. */
432static PyObject *
433normalize_module(PyObject *filename)
434{
435 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100436 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +0300437 const void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000438 Py_ssize_t len;
439
Victor Stinner9e30aa52011-11-21 02:49:52 +0100440 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000441 if (len < 0)
442 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100443
444 if (len == 0)
445 return PyUnicode_FromString("<unknown>");
446
447 kind = PyUnicode_KIND(filename);
448 data = PyUnicode_DATA(filename);
449
450 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000451 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100452 PyUnicode_READ(kind, data, len-3) == '.' &&
453 PyUnicode_READ(kind, data, len-2) == 'p' &&
454 PyUnicode_READ(kind, data, len-1) == 'y')
455 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100456 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000457 }
458 else {
459 module = filename;
460 Py_INCREF(module);
461 }
462 return module;
463}
464
465static int
466update_registry(PyObject *registry, PyObject *text, PyObject *category,
467 int add_zero)
468{
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300469 PyObject *altkey;
Christian Heimes33fe8092008-04-13 13:53:33 +0000470 int rc;
471
Serhiy Storchakaba85d692017-03-30 09:09:41 +0300472 if (add_zero)
Victor Stinnerc9bc2902020-10-27 02:24:34 +0100473 altkey = PyTuple_Pack(3, text, category, _PyLong_GetZero());
Christian Heimes33fe8092008-04-13 13:53:33 +0000474 else
475 altkey = PyTuple_Pack(2, text, category);
476
477 rc = already_warned(registry, altkey, 1);
Christian Heimes33fe8092008-04-13 13:53:33 +0000478 Py_XDECREF(altkey);
479 return rc;
480}
481
482static void
Victor Stinner914cde82016-03-19 01:03:51 +0100483show_warning(PyObject *filename, int lineno, PyObject *text,
484 PyObject *category, PyObject *sourceline)
Christian Heimes33fe8092008-04-13 13:53:33 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyObject *f_stderr;
487 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000488 char lineno_str[128];
489
490 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
491
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200492 name = _PyObject_GetAttrId(category, &PyId___name__);
Hai Shi57c78102020-03-14 21:40:58 +0800493 if (name == NULL) {
Victor Stinnerae233ea2013-10-31 14:51:38 +0100494 goto error;
Hai Shi57c78102020-03-14 21:40:58 +0800495 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000496
Victor Stinnerbd303c12013-11-07 23:07:29 +0100497 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000498 if (f_stderr == NULL) {
499 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100500 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000501 }
502
503 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100504 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
505 goto error;
506 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
507 goto error;
508 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
509 goto error;
510 if (PyFile_WriteString(": ", f_stderr) < 0)
511 goto error;
512 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
513 goto error;
514 if (PyFile_WriteString("\n", f_stderr) < 0)
515 goto error;
516 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000517
518 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000519 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100520 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +0300521 const void *data;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100522 Py_ssize_t i, len;
523 Py_UCS4 ch;
524 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000525
Victor Stinnera4c704b2013-10-29 23:43:41 +0100526 if (PyUnicode_READY(sourceline) < 1)
527 goto error;
528
529 kind = PyUnicode_KIND(sourceline);
530 data = PyUnicode_DATA(sourceline);
531 len = PyUnicode_GET_LENGTH(sourceline);
532 for (i=0; i<len; i++) {
533 ch = PyUnicode_READ(kind, data, i);
534 if (ch != ' ' && ch != '\t' && ch != '\014')
535 break;
536 }
537
538 truncated = PyUnicode_Substring(sourceline, i, len);
539 if (truncated == NULL)
540 goto error;
541
542 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
543 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000544 PyFile_WriteString("\n", f_stderr);
545 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200546 else {
547 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
548 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100549
550error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100551 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000552 PyErr_Clear();
553}
554
Victor Stinner1231a462016-03-19 00:47:17 +0100555static int
556call_show_warning(PyObject *category, PyObject *text, PyObject *message,
557 PyObject *filename, int lineno, PyObject *lineno_obj,
Victor Stinner914cde82016-03-19 01:03:51 +0100558 PyObject *sourceline, PyObject *source)
Victor Stinner1231a462016-03-19 00:47:17 +0100559{
560 PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL;
Victor Stinner82656272017-11-22 23:51:42 +0100561 _Py_IDENTIFIER(_showwarnmsg);
562 _Py_IDENTIFIER(WarningMessage);
Victor Stinner1231a462016-03-19 00:47:17 +0100563
Victor Stinnere98445a2016-03-23 00:54:48 +0100564 /* If the source parameter is set, try to get the Python implementation.
565 The Python implementation is able to log the traceback where the source
luzpaza5293b42017-11-05 07:37:50 -0600566 was allocated, whereas the C implementation doesn't. */
Victor Stinner82656272017-11-22 23:51:42 +0100567 show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL);
Victor Stinner1231a462016-03-19 00:47:17 +0100568 if (show_fn == NULL) {
569 if (PyErr_Occurred())
570 return -1;
571 show_warning(filename, lineno, text, category, sourceline);
572 return 0;
573 }
574
575 if (!PyCallable_Check(show_fn)) {
576 PyErr_SetString(PyExc_TypeError,
577 "warnings._showwarnmsg() must be set to a callable");
578 goto error;
579 }
580
Victor Stinner82656272017-11-22 23:51:42 +0100581 warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0);
Victor Stinner1231a462016-03-19 00:47:17 +0100582 if (warnmsg_cls == NULL) {
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200583 if (!PyErr_Occurred()) {
584 PyErr_SetString(PyExc_RuntimeError,
585 "unable to get warnings.WarningMessage");
586 }
Victor Stinner1231a462016-03-19 00:47:17 +0100587 goto error;
588 }
589
590 msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
Victor Stinner914cde82016-03-19 01:03:51 +0100591 filename, lineno_obj, Py_None, Py_None, source,
Victor Stinner1231a462016-03-19 00:47:17 +0100592 NULL);
593 Py_DECREF(warnmsg_cls);
594 if (msg == NULL)
595 goto error;
596
Petr Viktorinffd97532020-02-11 17:46:57 +0100597 res = PyObject_CallOneArg(show_fn, msg);
Victor Stinner1231a462016-03-19 00:47:17 +0100598 Py_DECREF(show_fn);
599 Py_DECREF(msg);
600
601 if (res == NULL)
602 return -1;
603
604 Py_DECREF(res);
605 return 0;
606
607error:
608 Py_XDECREF(show_fn);
609 return -1;
610}
611
Christian Heimes33fe8092008-04-13 13:53:33 +0000612static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000614 PyObject *filename, int lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100615 PyObject *module, PyObject *registry, PyObject *sourceline,
616 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000617{
618 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400619 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100620 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000621 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100623 /* module can be None if a warning is emitted late during Python shutdown.
624 In this case, the Python warnings module was probably unloaded, filters
625 are no more available to choose as action. It is safer to ignore the
626 warning and do nothing. */
627 if (module == Py_None)
628 Py_RETURN_NONE;
629
Brett Cannondb734912008-06-27 00:52:15 +0000630 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
Oren Milman252033d2017-09-11 09:28:39 +0300631 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None");
Brett Cannondb734912008-06-27 00:52:15 +0000632 return NULL;
633 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000634
635 /* Normalize module. */
636 if (module == NULL) {
637 module = normalize_module(filename);
638 if (module == NULL)
639 return NULL;
640 }
641 else
642 Py_INCREF(module);
643
644 /* Normalize message. */
645 Py_INCREF(message); /* DECREF'ed in cleanup. */
646 rc = PyObject_IsInstance(message, PyExc_Warning);
647 if (rc == -1) {
648 goto cleanup;
649 }
650 if (rc == 1) {
651 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000652 if (text == NULL)
653 goto cleanup;
Victor Stinnera102ed72020-02-07 02:24:48 +0100654 category = (PyObject*)Py_TYPE(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000655 }
656 else {
657 text = message;
Petr Viktorinffd97532020-02-11 17:46:57 +0100658 message = PyObject_CallOneArg(category, message);
Brett Cannondb734912008-06-27 00:52:15 +0000659 if (message == NULL)
660 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000661 }
662
663 lineno_obj = PyLong_FromLong(lineno);
664 if (lineno_obj == NULL)
665 goto cleanup;
666
Victor Stinner22f18752016-12-09 18:08:18 +0100667 if (source == Py_None) {
668 source = NULL;
669 }
670
Christian Heimes33fe8092008-04-13 13:53:33 +0000671 /* Create key. */
672 key = PyTuple_Pack(3, text, category, lineno_obj);
673 if (key == NULL)
674 goto cleanup;
675
Brett Cannondb734912008-06-27 00:52:15 +0000676 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000677 rc = already_warned(registry, key, 0);
678 if (rc == -1)
679 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000681 goto return_none;
682 /* Else this warning hasn't been generated before. */
683 }
684
685 action = get_filter(category, text, lineno, module, &item);
686 if (action == NULL)
687 goto cleanup;
688
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200689 if (_PyUnicode_EqualToASCIIString(action, "error")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000690 PyErr_SetObject(category, message);
691 goto cleanup;
692 }
693
Victor Stinnerc9758782017-11-27 16:57:07 +0100694 if (_PyUnicode_EqualToASCIIString(action, "ignore")) {
695 goto return_none;
696 }
697
Christian Heimes33fe8092008-04-13 13:53:33 +0000698 /* Store in the registry that we've been here, *except* when the action
699 is "always". */
700 rc = 0;
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200701 if (!_PyUnicode_EqualToASCIIString(action, "always")) {
Brett Cannondb734912008-06-27 00:52:15 +0000702 if (registry != NULL && registry != Py_None &&
Victor Stinnerc9758782017-11-27 16:57:07 +0100703 PyDict_SetItem(registry, key, Py_True) < 0)
704 {
Christian Heimes33fe8092008-04-13 13:53:33 +0000705 goto cleanup;
Victor Stinnerc9758782017-11-27 16:57:07 +0100706 }
707
708 if (_PyUnicode_EqualToASCIIString(action, "once")) {
Brett Cannondb734912008-06-27 00:52:15 +0000709 if (registry == NULL || registry == Py_None) {
Victor Stinner66b79732020-03-02 15:02:18 +0100710 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -0400711 if (st == NULL) {
712 goto cleanup;
713 }
714 registry = get_once_registry(st);
Christian Heimes33fe8092008-04-13 13:53:33 +0000715 if (registry == NULL)
716 goto cleanup;
717 }
Eric Snow86ea5812019-05-10 13:29:55 -0400718 /* WarningsState.once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000720 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200721 else if (_PyUnicode_EqualToASCIIString(action, "module")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000722 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000723 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000725 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200726 else if (!_PyUnicode_EqualToASCIIString(action, "default")) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000727 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100728 "Unrecognized action (%R) in warnings.filters:\n %R",
729 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000730 goto cleanup;
731 }
732 }
733
Christian Heimes1a8501c2008-10-02 19:56:01 +0000734 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000735 goto return_none;
736 if (rc == 0) {
Victor Stinner1231a462016-03-19 00:47:17 +0100737 if (call_show_warning(category, text, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +0100738 lineno_obj, sourceline, source) < 0)
Victor Stinner1231a462016-03-19 00:47:17 +0100739 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000740 }
741 else /* if (rc == -1) */
742 goto cleanup;
743
744 return_none:
745 result = Py_None;
746 Py_INCREF(result);
747
748 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400749 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000750 Py_XDECREF(key);
751 Py_XDECREF(text);
752 Py_XDECREF(lineno_obj);
753 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000754 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000755 return result; /* Py_None or NULL. */
756}
757
Larry Hastings714e4932015-09-06 00:39:37 -0700758static int
759is_internal_frame(PyFrameObject *frame)
760{
761 static PyObject *importlib_string = NULL;
762 static PyObject *bootstrap_string = NULL;
Larry Hastings714e4932015-09-06 00:39:37 -0700763 int contains;
764
765 if (importlib_string == NULL) {
766 importlib_string = PyUnicode_FromString("importlib");
767 if (importlib_string == NULL) {
768 return 0;
769 }
770
771 bootstrap_string = PyUnicode_FromString("_bootstrap");
772 if (bootstrap_string == NULL) {
773 Py_DECREF(importlib_string);
774 return 0;
775 }
776 Py_INCREF(importlib_string);
777 Py_INCREF(bootstrap_string);
778 }
779
Victor Stinnera42ca742020-04-28 19:01:31 +0200780 if (frame == NULL) {
Larry Hastings714e4932015-09-06 00:39:37 -0700781 return 0;
782 }
Victor Stinnera42ca742020-04-28 19:01:31 +0200783
784 PyCodeObject *code = PyFrame_GetCode(frame);
Victor Stinnera42ca742020-04-28 19:01:31 +0200785 PyObject *filename = code->co_filename;
Victor Stinner8852ad42020-04-29 01:28:13 +0200786 Py_DECREF(code);
787
Victor Stinnera42ca742020-04-28 19:01:31 +0200788 if (filename == NULL) {
789 return 0;
790 }
Larry Hastings714e4932015-09-06 00:39:37 -0700791 if (!PyUnicode_Check(filename)) {
792 return 0;
793 }
Victor Stinnera42ca742020-04-28 19:01:31 +0200794
Larry Hastings714e4932015-09-06 00:39:37 -0700795 contains = PyUnicode_Contains(filename, importlib_string);
796 if (contains < 0) {
797 return 0;
798 }
799 else if (contains > 0) {
800 contains = PyUnicode_Contains(filename, bootstrap_string);
801 if (contains < 0) {
802 return 0;
803 }
804 else if (contains > 0) {
805 return 1;
806 }
807 }
808
809 return 0;
810}
811
812static PyFrameObject *
813next_external_frame(PyFrameObject *frame)
814{
815 do {
Victor Stinner70364772020-04-29 03:28:46 +0200816 PyFrameObject *back = PyFrame_GetBack(frame);
817 Py_DECREF(frame);
818 frame = back;
Larry Hastings714e4932015-09-06 00:39:37 -0700819 } while (frame != NULL && is_internal_frame(frame));
820
821 return frame;
822}
823
Christian Heimes33fe8092008-04-13 13:53:33 +0000824/* filename, module, and registry are new refs, globals is borrowed */
825/* Returns 0 on error (no new refs), 1 on success */
826static int
827setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
828 PyObject **module, PyObject **registry)
829{
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200830 _Py_IDENTIFIER(__warningregistry__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000831 PyObject *globals;
832
Thomas Kluyver11a89662018-06-08 21:28:37 +0200833 /* Setup globals, filename and lineno. */
Victor Stinner70364772020-04-29 03:28:46 +0200834 PyThreadState *tstate = _PyThreadState_GET();
835 PyFrameObject *f = PyThreadState_GetFrame(tstate);
Larry Hastings714e4932015-09-06 00:39:37 -0700836 // Stack level comparisons to Python code is off by one as there is no
837 // warnings-related stack level to avoid.
838 if (stack_level <= 0 || is_internal_frame(f)) {
839 while (--stack_level > 0 && f != NULL) {
Victor Stinner70364772020-04-29 03:28:46 +0200840 PyFrameObject *back = PyFrame_GetBack(f);
841 Py_DECREF(f);
842 f = back;
Larry Hastings714e4932015-09-06 00:39:37 -0700843 }
844 }
845 else {
846 while (--stack_level > 0 && f != NULL) {
847 f = next_external_frame(f);
848 }
849 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000850
851 if (f == NULL) {
Victor Stinner45df61f2020-11-02 23:17:46 +0100852 globals = tstate->interp->sysdict;
Thomas Kluyver11a89662018-06-08 21:28:37 +0200853 *filename = PyUnicode_FromString("sys");
Christian Heimes33fe8092008-04-13 13:53:33 +0000854 *lineno = 1;
855 }
856 else {
857 globals = f->f_globals;
Victor Stinner8852ad42020-04-29 01:28:13 +0200858 PyCodeObject *code = PyFrame_GetCode(f);
859 *filename = code->co_filename;
860 Py_DECREF(code);
Thomas Kluyver11a89662018-06-08 21:28:37 +0200861 Py_INCREF(*filename);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000862 *lineno = PyFrame_GetLineNumber(f);
Victor Stinner70364772020-04-29 03:28:46 +0200863 Py_DECREF(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000864 }
865
866 *module = NULL;
867
868 /* Setup registry. */
869 assert(globals != NULL);
870 assert(PyDict_Check(globals));
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200871 *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000872 if (*registry == NULL) {
873 int rc;
874
Victor Stinner70364772020-04-29 03:28:46 +0200875 if (_PyErr_Occurred(tstate)) {
Serhiy Storchaka2d2f8552020-03-02 22:05:08 +0200876 goto handle_error;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200877 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000878 *registry = PyDict_New();
879 if (*registry == NULL)
Serhiy Storchaka2d2f8552020-03-02 22:05:08 +0200880 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000881
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200882 rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000883 if (rc < 0)
884 goto handle_error;
885 }
886 else
887 Py_INCREF(*registry);
888
889 /* Setup module. */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200890 *module = _PyDict_GetItemIdWithError(globals, &PyId___name__);
Oren Milman5d3e8002017-09-24 21:28:42 +0300891 if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) {
892 Py_INCREF(*module);
893 }
Victor Stinner70364772020-04-29 03:28:46 +0200894 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200895 goto handle_error;
896 }
Oren Milman5d3e8002017-09-24 21:28:42 +0300897 else {
Christian Heimes33fe8092008-04-13 13:53:33 +0000898 *module = PyUnicode_FromString("<string>");
899 if (*module == NULL)
900 goto handle_error;
901 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000902
Christian Heimes33fe8092008-04-13 13:53:33 +0000903 return 1;
904
905 handle_error:
Christian Heimes33fe8092008-04-13 13:53:33 +0000906 Py_XDECREF(*registry);
907 Py_XDECREF(*module);
Serhiy Storchakaae75a292020-03-03 19:43:29 +0200908 Py_DECREF(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000909 return 0;
910}
911
912static PyObject *
913get_category(PyObject *message, PyObject *category)
914{
915 int rc;
916
917 /* Get category. */
918 rc = PyObject_IsInstance(message, PyExc_Warning);
919 if (rc == -1)
920 return NULL;
921
922 if (rc == 1)
Victor Stinnera102ed72020-02-07 02:24:48 +0100923 category = (PyObject*)Py_TYPE(message);
Berker Peksagd8089e02014-07-11 19:50:25 +0300924 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000925 category = PyExc_UserWarning;
926
927 /* Validate category. */
928 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300929 /* category is not a subclass of PyExc_Warning or
930 PyObject_IsSubclass raised an error */
931 if (rc == -1 || rc == 0) {
932 PyErr_Format(PyExc_TypeError,
933 "category must be a Warning subclass, not '%s'",
934 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000935 return NULL;
936 }
937
938 return category;
939}
940
941static PyObject *
Victor Stinner914cde82016-03-19 01:03:51 +0100942do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
943 PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +0000944{
945 PyObject *filename, *module, *registry, *res;
946 int lineno;
947
948 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
949 return NULL;
950
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100951 res = warn_explicit(category, message, filename, lineno, module, registry,
Victor Stinner914cde82016-03-19 01:03:51 +0100952 NULL, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000953 Py_DECREF(filename);
954 Py_DECREF(registry);
955 Py_DECREF(module);
956 return res;
957}
958
Victor Stinner22f18752016-12-09 18:08:18 +0100959/*[clinic input]
960warn as warnings_warn
961
962 message: object
963 category: object = None
964 stacklevel: Py_ssize_t = 1
965 source: object = None
966
967Issue a warning, or maybe ignore it or raise an exception.
968[clinic start generated code]*/
969
Christian Heimes33fe8092008-04-13 13:53:33 +0000970static PyObject *
Victor Stinner22f18752016-12-09 18:08:18 +0100971warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category,
972 Py_ssize_t stacklevel, PyObject *source)
973/*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/
Christian Heimes33fe8092008-04-13 13:53:33 +0000974{
Christian Heimes33fe8092008-04-13 13:53:33 +0000975 category = get_category(message, category);
976 if (category == NULL)
977 return NULL;
Victor Stinner22f18752016-12-09 18:08:18 +0100978 return do_warn(message, category, stacklevel, source);
Christian Heimes33fe8092008-04-13 13:53:33 +0000979}
980
981static PyObject *
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200982get_source_line(PyObject *module_globals, int lineno)
983{
984 _Py_IDENTIFIER(get_source);
985 _Py_IDENTIFIER(__loader__);
Serhiy Storchakad4f84802017-11-11 15:19:47 +0200986 PyObject *loader;
987 PyObject *module_name;
988 PyObject *get_source;
989 PyObject *source;
990 PyObject *source_list;
991 PyObject *source_line;
992
993 /* Check/get the requisite pieces needed for the loader. */
994 loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__);
995 if (loader == NULL) {
996 return NULL;
997 }
998 Py_INCREF(loader);
999 module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__);
1000 if (!module_name) {
1001 Py_DECREF(loader);
1002 return NULL;
1003 }
1004 Py_INCREF(module_name);
1005
1006 /* Make sure the loader implements the optional get_source() method. */
Serhiy Storchakaf320be72018-01-25 10:49:40 +02001007 (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source);
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001008 Py_DECREF(loader);
1009 if (!get_source) {
1010 Py_DECREF(module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001011 return NULL;
1012 }
1013 /* Call get_source() to get the source code. */
Petr Viktorinffd97532020-02-11 17:46:57 +01001014 source = PyObject_CallOneArg(get_source, module_name);
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001015 Py_DECREF(get_source);
1016 Py_DECREF(module_name);
1017 if (!source) {
1018 return NULL;
1019 }
1020 if (source == Py_None) {
1021 Py_DECREF(source);
1022 return NULL;
1023 }
1024
1025 /* Split the source into lines. */
1026 source_list = PyUnicode_Splitlines(source, 0);
1027 Py_DECREF(source);
1028 if (!source_list) {
1029 return NULL;
1030 }
1031
1032 /* Get the source line. */
1033 source_line = PyList_GetItem(source_list, lineno-1);
1034 Py_XINCREF(source_line);
1035 Py_DECREF(source_list);
1036 return source_line;
1037}
1038
1039static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +00001040warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
1041{
1042 static char *kwd_list[] = {"message", "category", "filename", "lineno",
Victor Stinner914cde82016-03-19 01:03:51 +01001043 "module", "registry", "module_globals",
1044 "source", 0};
Christian Heimes33fe8092008-04-13 13:53:33 +00001045 PyObject *message;
1046 PyObject *category;
1047 PyObject *filename;
1048 int lineno;
1049 PyObject *module = NULL;
1050 PyObject *registry = NULL;
1051 PyObject *module_globals = NULL;
Victor Stinner914cde82016-03-19 01:03:51 +01001052 PyObject *sourceobj = NULL;
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001053 PyObject *source_line = NULL;
1054 PyObject *returned;
Christian Heimes33fe8092008-04-13 13:53:33 +00001055
Victor Stinner914cde82016-03-19 01:03:51 +01001056 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +00001057 kwd_list, &message, &category, &filename, &lineno, &module,
Victor Stinner914cde82016-03-19 01:03:51 +01001058 &registry, &module_globals, &sourceobj))
Christian Heimes33fe8092008-04-13 13:53:33 +00001059 return NULL;
1060
Victor Stinnerb0565622018-05-15 20:42:12 +02001061 if (module_globals && module_globals != Py_None) {
1062 if (!PyDict_Check(module_globals)) {
1063 PyErr_Format(PyExc_TypeError,
1064 "module_globals must be a dict, not '%.200s'",
1065 Py_TYPE(module_globals)->tp_name);
1066 return NULL;
1067 }
1068
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001069 source_line = get_source_line(module_globals, lineno);
1070 if (source_line == NULL && PyErr_Occurred()) {
Christian Heimes33fe8092008-04-13 13:53:33 +00001071 return NULL;
1072 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001073 }
Serhiy Storchakad4f84802017-11-11 15:19:47 +02001074 returned = warn_explicit(category, message, filename, lineno, module,
1075 registry, source_line, sourceobj);
1076 Py_XDECREF(source_line);
1077 return returned;
Christian Heimes33fe8092008-04-13 13:53:33 +00001078}
1079
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001080static PyObject *
1081warnings_filters_mutated(PyObject *self, PyObject *args)
1082{
Victor Stinner66b79732020-03-02 15:02:18 +01001083 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -04001084 if (st == NULL) {
1085 return NULL;
1086 }
1087 st->filters_version++;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001088 Py_RETURN_NONE;
1089}
1090
Christian Heimes33fe8092008-04-13 13:53:33 +00001091
1092/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001093
1094static int
1095warn_unicode(PyObject *category, PyObject *message,
Victor Stinner914cde82016-03-19 01:03:51 +01001096 Py_ssize_t stack_level, PyObject *source)
Christian Heimes33fe8092008-04-13 13:53:33 +00001097{
1098 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +00001099
1100 if (category == NULL)
1101 category = PyExc_RuntimeWarning;
1102
Victor Stinner914cde82016-03-19 01:03:51 +01001103 res = do_warn(message, category, stack_level, source);
Christian Heimes33fe8092008-04-13 13:53:33 +00001104 if (res == NULL)
1105 return -1;
1106 Py_DECREF(res);
1107
1108 return 0;
1109}
1110
Victor Stinner914cde82016-03-19 01:03:51 +01001111static int
1112_PyErr_WarnFormatV(PyObject *source,
1113 PyObject *category, Py_ssize_t stack_level,
1114 const char *format, va_list vargs)
1115{
1116 PyObject *message;
1117 int res;
1118
1119 message = PyUnicode_FromFormatV(format, vargs);
1120 if (message == NULL)
1121 return -1;
1122
1123 res = warn_unicode(category, message, stack_level, source);
1124 Py_DECREF(message);
1125 return res;
1126}
1127
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001128int
1129PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
1130 const char *format, ...)
1131{
Victor Stinner914cde82016-03-19 01:03:51 +01001132 int res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001133 va_list vargs;
1134
1135#ifdef HAVE_STDARG_PROTOTYPES
1136 va_start(vargs, format);
1137#else
1138 va_start(vargs);
1139#endif
Victor Stinner914cde82016-03-19 01:03:51 +01001140 res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001141 va_end(vargs);
Victor Stinner914cde82016-03-19 01:03:51 +01001142 return res;
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001143}
1144
Victor Stinner8d84adc2020-03-31 17:25:12 +02001145static int
1146_PyErr_WarnFormat(PyObject *source, PyObject *category, Py_ssize_t stack_level,
1147 const char *format, ...)
1148{
1149 int res;
1150 va_list vargs;
1151
1152#ifdef HAVE_STDARG_PROTOTYPES
1153 va_start(vargs, format);
1154#else
1155 va_start(vargs);
1156#endif
1157 res = _PyErr_WarnFormatV(source, category, stack_level, format, vargs);
1158 va_end(vargs);
1159 return res;
1160}
1161
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001162int
Victor Stinner914cde82016-03-19 01:03:51 +01001163PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level,
1164 const char *format, ...)
1165{
1166 int res;
1167 va_list vargs;
1168
1169#ifdef HAVE_STDARG_PROTOTYPES
1170 va_start(vargs, format);
1171#else
1172 va_start(vargs);
1173#endif
1174 res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning,
1175 stack_level, format, vargs);
1176 va_end(vargs);
1177 return res;
1178}
1179
1180
1181int
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001182PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
1183{
1184 int ret;
1185 PyObject *message = PyUnicode_FromString(text);
1186 if (message == NULL)
1187 return -1;
Victor Stinner914cde82016-03-19 01:03:51 +01001188 ret = warn_unicode(category, message, stack_level, NULL);
Victor Stinner4a2b7a12010-08-13 14:03:48 +00001189 Py_DECREF(message);
1190 return ret;
1191}
1192
Ezio Melotti42da6632011-03-15 05:18:48 +02001193/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +00001194 Use PyErr_WarnEx instead. */
1195
1196#undef PyErr_Warn
1197
Benjamin Petersone5024512018-09-12 12:06:42 -07001198int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001199PyErr_Warn(PyObject *category, const char *text)
Christian Heimes33fe8092008-04-13 13:53:33 +00001200{
1201 return PyErr_WarnEx(category, text, 1);
1202}
1203
1204/* Warning with explicit origin */
1205int
Victor Stinner14e461d2013-08-26 22:28:21 +02001206PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1207 PyObject *filename, int lineno,
1208 PyObject *module, PyObject *registry)
1209{
1210 PyObject *res;
1211 if (category == NULL)
1212 category = PyExc_RuntimeWarning;
1213 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001214 module, registry, NULL, NULL);
Victor Stinner14e461d2013-08-26 22:28:21 +02001215 if (res == NULL)
1216 return -1;
1217 Py_DECREF(res);
1218 return 0;
1219}
1220
1221int
Christian Heimes33fe8092008-04-13 13:53:33 +00001222PyErr_WarnExplicit(PyObject *category, const char *text,
1223 const char *filename_str, int lineno,
1224 const char *module_str, PyObject *registry)
1225{
Christian Heimes33fe8092008-04-13 13:53:33 +00001226 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +00001227 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +00001228 PyObject *module = NULL;
1229 int ret = -1;
1230
1231 if (message == NULL || filename == NULL)
1232 goto exit;
1233 if (module_str != NULL) {
1234 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001235 if (module == NULL)
1236 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +00001237 }
1238
Victor Stinner14e461d2013-08-26 22:28:21 +02001239 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1240 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +00001241
1242 exit:
1243 Py_XDECREF(message);
1244 Py_XDECREF(module);
1245 Py_XDECREF(filename);
1246 return ret;
1247}
1248
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001249int
1250PyErr_WarnExplicitFormat(PyObject *category,
1251 const char *filename_str, int lineno,
1252 const char *module_str, PyObject *registry,
1253 const char *format, ...)
1254{
1255 PyObject *message;
1256 PyObject *module = NULL;
1257 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1258 int ret = -1;
1259 va_list vargs;
1260
1261 if (filename == NULL)
1262 goto exit;
1263 if (module_str != NULL) {
1264 module = PyUnicode_FromString(module_str);
1265 if (module == NULL)
1266 goto exit;
1267 }
1268
1269#ifdef HAVE_STDARG_PROTOTYPES
1270 va_start(vargs, format);
1271#else
1272 va_start(vargs);
1273#endif
1274 message = PyUnicode_FromFormatV(format, vargs);
1275 if (message != NULL) {
1276 PyObject *res;
1277 res = warn_explicit(category, message, filename, lineno,
Victor Stinner914cde82016-03-19 01:03:51 +01001278 module, registry, NULL, NULL);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +02001279 Py_DECREF(message);
1280 if (res != NULL) {
1281 Py_DECREF(res);
1282 ret = 0;
1283 }
1284 }
1285 va_end(vargs);
1286exit:
1287 Py_XDECREF(module);
1288 Py_XDECREF(filename);
1289 return ret;
1290}
1291
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001292void
1293_PyErr_WarnUnawaitedCoroutine(PyObject *coro)
1294{
1295 /* First, we attempt to funnel the warning through
1296 warnings._warn_unawaited_coroutine.
1297
1298 This could raise an exception, due to:
1299 - a bug
1300 - some kind of shutdown-related brokenness
1301 - succeeding, but with an "error" warning filter installed, so the
1302 warning is converted into a RuntimeWarning exception
1303
1304 In the first two cases, we want to print the error (so we know what it
1305 is!), and then print a warning directly as a fallback. In the last
1306 case, we want to print the error (since it's the warning!), but *not*
1307 do a fallback. And after we print the error we can't check for what
1308 type of error it was (because PyErr_WriteUnraisable clears it), so we
1309 need a flag to keep track.
1310
1311 Since this is called from __del__ context, it's careful to never raise
1312 an exception.
1313 */
1314 _Py_IDENTIFIER(_warn_unawaited_coroutine);
1315 int warned = 0;
1316 PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1);
1317 if (fn) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001318 PyObject *res = PyObject_CallOneArg(fn, coro);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001319 Py_DECREF(fn);
1320 if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) {
1321 warned = 1;
1322 }
1323 Py_XDECREF(res);
1324 }
1325
1326 if (PyErr_Occurred()) {
1327 PyErr_WriteUnraisable(coro);
1328 }
1329 if (!warned) {
Victor Stinner8d84adc2020-03-31 17:25:12 +02001330 if (_PyErr_WarnFormat(coro, PyExc_RuntimeWarning, 1,
1331 "coroutine '%S' was never awaited",
1332 ((PyCoroObject *)coro)->cr_qualname) < 0)
Yury Selivanov35103342018-01-21 20:47:04 -05001333 {
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08001334 PyErr_WriteUnraisable(coro);
1335 }
1336 }
1337}
Christian Heimes33fe8092008-04-13 13:53:33 +00001338
Christian Heimes33fe8092008-04-13 13:53:33 +00001339PyDoc_STRVAR(warn_explicit_doc,
Hansraj Das5dfbb4d2019-10-08 14:26:07 +05301340"Low-level interface to warnings functionality.");
Christian Heimes33fe8092008-04-13 13:53:33 +00001341
1342static PyMethodDef warnings_functions[] = {
Victor Stinner22f18752016-12-09 18:08:18 +01001343 WARNINGS_WARN_METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02001344 {"warn_explicit", (PyCFunction)(void(*)(void))warnings_warn_explicit,
Christian Heimes33fe8092008-04-13 13:53:33 +00001345 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001346 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
1347 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +00001348 /* XXX(brett.cannon): add showwarning? */
1349 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +00001351};
1352
1353
Martin v. Löwis1a214512008-06-11 05:26:20 +00001354static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 PyModuleDef_HEAD_INIT,
Eric Snow86ea5812019-05-10 13:29:55 -04001356 MODULE_NAME, /* m_name */
1357 warnings__doc__, /* m_doc */
1358 0, /* m_size */
1359 warnings_functions, /* m_methods */
1360 NULL, /* m_reload */
1361 NULL, /* m_traverse */
1362 NULL, /* m_clear */
1363 NULL /* m_free */
Martin v. Löwis1a214512008-06-11 05:26:20 +00001364};
1365
Christian Heimes33fe8092008-04-13 13:53:33 +00001366
Victor Stinner5d862462017-12-19 11:35:58 +01001367PyMODINIT_FUNC
1368_PyWarnings_Init(void)
Christian Heimes33fe8092008-04-13 13:53:33 +00001369{
Brett Cannon0759dd62009-04-01 18:13:07 +00001370 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001371
Martin v. Löwis1a214512008-06-11 05:26:20 +00001372 m = PyModule_Create(&warningsmodule);
Eric Snow86ea5812019-05-10 13:29:55 -04001373 if (m == NULL) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00001374 return NULL;
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001375 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001376
Victor Stinner66b79732020-03-02 15:02:18 +01001377 WarningsState *st = warnings_get_state();
Eric Snow86ea5812019-05-10 13:29:55 -04001378 if (st == NULL) {
1379 goto error;
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001380 }
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001381
Victor Stinner58ca33b2020-11-04 17:33:06 +01001382 if (PyModule_AddObjectRef(m, "filters", st->filters) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001383 goto error;
1384 }
Victor Stinner58ca33b2020-11-04 17:33:06 +01001385 if (PyModule_AddObjectRef(m, "_onceregistry", st->once_registry) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001386 goto error;
1387 }
Victor Stinner58ca33b2020-11-04 17:33:06 +01001388 if (PyModule_AddObjectRef(m, "_defaultaction", st->default_action) < 0) {
Eric Snow86ea5812019-05-10 13:29:55 -04001389 goto error;
1390 }
1391
Martin v. Löwis1a214512008-06-11 05:26:20 +00001392 return m;
Eric Snow86ea5812019-05-10 13:29:55 -04001393
1394error:
1395 if (st != NULL) {
Victor Stinner66b79732020-03-02 15:02:18 +01001396 warnings_clear_state(st);
Eric Snow86ea5812019-05-10 13:29:55 -04001397 }
1398 Py_DECREF(m);
1399 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001400}
Victor Stinner87d23a02019-04-26 05:49:26 +02001401
Eric Snow86ea5812019-05-10 13:29:55 -04001402// We need this to ensure that warnings still work until late in finalization.
Victor Stinner87d23a02019-04-26 05:49:26 +02001403void
Eric Snow86ea5812019-05-10 13:29:55 -04001404_PyWarnings_Fini(PyInterpreterState *interp)
Victor Stinner87d23a02019-04-26 05:49:26 +02001405{
Victor Stinner66b79732020-03-02 15:02:18 +01001406 warnings_clear_state(&interp->warnings);
Victor Stinner87d23a02019-04-26 05:49:26 +02001407}