blob: 6dff0a2c114a502a8054a3adb4b2b8c90d90b915 [file] [log] [blame]
Christian Heimes33fe8092008-04-13 13:53:33 +00001#include "Python.h"
2#include "frameobject.h"
3
4#define MODULE_NAME "_warnings"
Christian Heimes33fe8092008-04-13 13:53:33 +00005
6PyDoc_STRVAR(warnings__doc__,
7MODULE_NAME " provides basic warning filtering support.\n"
8"It is a helper module to speed up interpreter start-up.");
9
10/* Both 'filters' and 'onceregistry' can be set in warnings.py;
11 get_warnings_attr() will reset these variables accordingly. */
12static PyObject *_filters; /* List */
13static PyObject *_once_registry; /* Dict */
Brett Cannon0759dd62009-04-01 18:13:07 +000014static PyObject *_default_action; /* String */
Antoine Pitroucb0a0062014-09-18 02:40:46 +020015static long _filters_version;
Christian Heimes33fe8092008-04-13 13:53:33 +000016
Victor Stinnerbd303c12013-11-07 23:07:29 +010017_Py_IDENTIFIER(argv);
18_Py_IDENTIFIER(stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +000019
20static int
21check_matched(PyObject *obj, PyObject *arg)
22{
23 PyObject *result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +020024 _Py_IDENTIFIER(match);
Christian Heimes33fe8092008-04-13 13:53:33 +000025 int rc;
26
27 if (obj == Py_None)
28 return 1;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020029 result = _PyObject_CallMethodId(obj, &PyId_match, "O", arg);
Christian Heimes33fe8092008-04-13 13:53:33 +000030 if (result == NULL)
31 return -1;
32
33 rc = PyObject_IsTrue(result);
34 Py_DECREF(result);
35 return rc;
36}
37
38/*
39 Returns a new reference.
40 A NULL return value can mean false or an error.
41*/
42static PyObject *
43get_warnings_attr(const char *attr)
44{
45 static PyObject *warnings_str = NULL;
46 PyObject *all_modules;
47 PyObject *warnings_module;
48 int result;
49
50 if (warnings_str == NULL) {
51 warnings_str = PyUnicode_InternFromString("warnings");
52 if (warnings_str == NULL)
53 return NULL;
54 }
55
56 all_modules = PyImport_GetModuleDict();
57 result = PyDict_Contains(all_modules, warnings_str);
58 if (result == -1 || result == 0)
59 return NULL;
60
61 warnings_module = PyDict_GetItem(all_modules, warnings_str);
62 if (!PyObject_HasAttrString(warnings_module, attr))
63 return NULL;
64 return PyObject_GetAttrString(warnings_module, attr);
65}
66
67
Neal Norwitz32dde222008-04-15 06:43:13 +000068static PyObject *
Christian Heimes33fe8092008-04-13 13:53:33 +000069get_once_registry(void)
70{
71 PyObject *registry;
72
73 registry = get_warnings_attr("onceregistry");
74 if (registry == NULL) {
75 if (PyErr_Occurred())
76 return NULL;
77 return _once_registry;
78 }
79 Py_DECREF(_once_registry);
80 _once_registry = registry;
81 return registry;
82}
83
84
Brett Cannon0759dd62009-04-01 18:13:07 +000085static PyObject *
86get_default_action(void)
87{
88 PyObject *default_action;
89
90 default_action = get_warnings_attr("defaultaction");
91 if (default_action == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (PyErr_Occurred()) {
93 return NULL;
94 }
95 return _default_action;
Brett Cannon0759dd62009-04-01 18:13:07 +000096 }
97
98 Py_DECREF(_default_action);
99 _default_action = default_action;
100 return default_action;
101}
102
103
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400104/* The item is a new reference. */
Victor Stinnera4c704b2013-10-29 23:43:41 +0100105static PyObject*
Christian Heimes33fe8092008-04-13 13:53:33 +0000106get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno,
107 PyObject *module, PyObject **item)
108{
Brett Cannon0759dd62009-04-01 18:13:07 +0000109 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000110 Py_ssize_t i;
111 PyObject *warnings_filters;
112
113 warnings_filters = get_warnings_attr("filters");
114 if (warnings_filters == NULL) {
115 if (PyErr_Occurred())
116 return NULL;
117 }
118 else {
119 Py_DECREF(_filters);
120 _filters = warnings_filters;
121 }
122
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000123 if (_filters == NULL || !PyList_Check(_filters)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000124 PyErr_SetString(PyExc_ValueError,
125 MODULE_NAME ".filters must be a list");
126 return NULL;
127 }
128
129 /* _filters could change while we are iterating over it. */
130 for (i = 0; i < PyList_GET_SIZE(_filters); i++) {
131 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj;
132 Py_ssize_t ln;
133 int is_subclass, good_msg, good_mod;
134
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400135 tmp_item = PyList_GET_ITEM(_filters, i);
136 if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000137 PyErr_Format(PyExc_ValueError,
138 MODULE_NAME ".filters item %zd isn't a 5-tuple", i);
139 return NULL;
140 }
141
142 /* Python code: action, msg, cat, mod, ln = item */
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400143 Py_INCREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000144 action = PyTuple_GET_ITEM(tmp_item, 0);
145 msg = PyTuple_GET_ITEM(tmp_item, 1);
146 cat = PyTuple_GET_ITEM(tmp_item, 2);
147 mod = PyTuple_GET_ITEM(tmp_item, 3);
148 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
149
150 good_msg = check_matched(msg, text);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400151 if (good_msg == -1) {
152 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100153 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400154 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100155
Christian Heimes33fe8092008-04-13 13:53:33 +0000156 good_mod = check_matched(mod, module);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400157 if (good_mod == -1) {
158 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100159 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400160 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100161
Christian Heimes33fe8092008-04-13 13:53:33 +0000162 is_subclass = PyObject_IsSubclass(category, cat);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400163 if (is_subclass == -1) {
164 Py_DECREF(tmp_item);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100165 return NULL;
Benjamin Peterson8c598162015-05-03 11:28:46 -0400166 }
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100167
Christian Heimes33fe8092008-04-13 13:53:33 +0000168 ln = PyLong_AsSsize_t(ln_obj);
Benjamin Peterson8c598162015-05-03 11:28:46 -0400169 if (ln == -1 && PyErr_Occurred()) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400170 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000171 return NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400172 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000173
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400174 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) {
175 *item = tmp_item;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100176 return action;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400177 }
178
179 Py_DECREF(tmp_item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000180 }
181
Brett Cannon0759dd62009-04-01 18:13:07 +0000182 action = get_default_action();
183 if (action != NULL) {
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400184 Py_INCREF(Py_None);
185 *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100186 return action;
Brett Cannon0759dd62009-04-01 18:13:07 +0000187 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000188
189 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000190 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000191 return NULL;
192}
193
Brett Cannon0759dd62009-04-01 18:13:07 +0000194
Christian Heimes33fe8092008-04-13 13:53:33 +0000195static int
196already_warned(PyObject *registry, PyObject *key, int should_set)
197{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200198 PyObject *version_obj, *already_warned;
199 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000200
201 if (key == NULL)
202 return -1;
203
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200204 version_obj = _PyDict_GetItemId(registry, &PyId_version);
205 if (version_obj == NULL
206 || !PyLong_CheckExact(version_obj)
207 || PyLong_AsLong(version_obj) != _filters_version) {
208 PyDict_Clear(registry);
209 version_obj = PyLong_FromLong(_filters_version);
210 if (version_obj == NULL)
211 return -1;
212 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
213 Py_DECREF(version_obj);
214 return -1;
215 }
216 Py_DECREF(version_obj);
217 }
218 else {
219 already_warned = PyDict_GetItem(registry, key);
220 if (already_warned != NULL) {
221 int rc = PyObject_IsTrue(already_warned);
222 if (rc != 0)
223 return rc;
224 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000225 }
226
227 /* This warning wasn't found in the registry, set it. */
228 if (should_set)
229 return PyDict_SetItem(registry, key, Py_True);
230 return 0;
231}
232
233/* New reference. */
234static PyObject *
235normalize_module(PyObject *filename)
236{
237 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100238 int kind;
239 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000240 Py_ssize_t len;
241
Victor Stinner9e30aa52011-11-21 02:49:52 +0100242 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000243 if (len < 0)
244 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100245
246 if (len == 0)
247 return PyUnicode_FromString("<unknown>");
248
249 kind = PyUnicode_KIND(filename);
250 data = PyUnicode_DATA(filename);
251
252 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000253 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100254 PyUnicode_READ(kind, data, len-3) == '.' &&
255 PyUnicode_READ(kind, data, len-2) == 'p' &&
256 PyUnicode_READ(kind, data, len-1) == 'y')
257 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100258 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000259 }
260 else {
261 module = filename;
262 Py_INCREF(module);
263 }
264 return module;
265}
266
267static int
268update_registry(PyObject *registry, PyObject *text, PyObject *category,
269 int add_zero)
270{
271 PyObject *altkey, *zero = NULL;
272 int rc;
273
274 if (add_zero) {
275 zero = PyLong_FromLong(0);
276 if (zero == NULL)
277 return -1;
278 altkey = PyTuple_Pack(3, text, category, zero);
279 }
280 else
281 altkey = PyTuple_Pack(2, text, category);
282
283 rc = already_warned(registry, altkey, 1);
284 Py_XDECREF(zero);
285 Py_XDECREF(altkey);
286 return rc;
287}
288
289static void
290show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
291 *category, PyObject *sourceline)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyObject *f_stderr;
294 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000295 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200296 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000297
298 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
299
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200300 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000301 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100302 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000303
Victor Stinnerbd303c12013-11-07 23:07:29 +0100304 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000305 if (f_stderr == NULL) {
306 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100307 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000308 }
309
310 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100311 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
312 goto error;
313 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
314 goto error;
315 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
316 goto error;
317 if (PyFile_WriteString(": ", f_stderr) < 0)
318 goto error;
319 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
320 goto error;
321 if (PyFile_WriteString("\n", f_stderr) < 0)
322 goto error;
323 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000324
325 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000326 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100327 int kind;
328 void *data;
329 Py_ssize_t i, len;
330 Py_UCS4 ch;
331 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000332
Victor Stinnera4c704b2013-10-29 23:43:41 +0100333 if (PyUnicode_READY(sourceline) < 1)
334 goto error;
335
336 kind = PyUnicode_KIND(sourceline);
337 data = PyUnicode_DATA(sourceline);
338 len = PyUnicode_GET_LENGTH(sourceline);
339 for (i=0; i<len; i++) {
340 ch = PyUnicode_READ(kind, data, i);
341 if (ch != ' ' && ch != '\t' && ch != '\014')
342 break;
343 }
344
345 truncated = PyUnicode_Substring(sourceline, i, len);
346 if (truncated == NULL)
347 goto error;
348
349 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
350 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000351 PyFile_WriteString("\n", f_stderr);
352 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200353 else {
354 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
355 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100356
357error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100358 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000359 PyErr_Clear();
360}
361
362static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000364 PyObject *filename, int lineno,
365 PyObject *module, PyObject *registry, PyObject *sourceline)
366{
367 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400368 PyObject *item = NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100369 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000370 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100372 /* module can be None if a warning is emitted late during Python shutdown.
373 In this case, the Python warnings module was probably unloaded, filters
374 are no more available to choose as action. It is safer to ignore the
375 warning and do nothing. */
376 if (module == Py_None)
377 Py_RETURN_NONE;
378
Brett Cannondb734912008-06-27 00:52:15 +0000379 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
380 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
381 return NULL;
382 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000383
384 /* Normalize module. */
385 if (module == NULL) {
386 module = normalize_module(filename);
387 if (module == NULL)
388 return NULL;
389 }
390 else
391 Py_INCREF(module);
392
393 /* Normalize message. */
394 Py_INCREF(message); /* DECREF'ed in cleanup. */
395 rc = PyObject_IsInstance(message, PyExc_Warning);
396 if (rc == -1) {
397 goto cleanup;
398 }
399 if (rc == 1) {
400 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000401 if (text == NULL)
402 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000403 category = (PyObject*)message->ob_type;
404 }
405 else {
406 text = message;
407 message = PyObject_CallFunction(category, "O", message);
Brett Cannondb734912008-06-27 00:52:15 +0000408 if (message == NULL)
409 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000410 }
411
412 lineno_obj = PyLong_FromLong(lineno);
413 if (lineno_obj == NULL)
414 goto cleanup;
415
416 /* Create key. */
417 key = PyTuple_Pack(3, text, category, lineno_obj);
418 if (key == NULL)
419 goto cleanup;
420
Brett Cannondb734912008-06-27 00:52:15 +0000421 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000422 rc = already_warned(registry, key, 0);
423 if (rc == -1)
424 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000426 goto return_none;
427 /* Else this warning hasn't been generated before. */
428 }
429
430 action = get_filter(category, text, lineno, module, &item);
431 if (action == NULL)
432 goto cleanup;
433
Victor Stinnera4c704b2013-10-29 23:43:41 +0100434 if (PyUnicode_CompareWithASCIIString(action, "error") == 0) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000435 PyErr_SetObject(category, message);
436 goto cleanup;
437 }
438
439 /* Store in the registry that we've been here, *except* when the action
440 is "always". */
441 rc = 0;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100442 if (PyUnicode_CompareWithASCIIString(action, "always") != 0) {
Brett Cannondb734912008-06-27 00:52:15 +0000443 if (registry != NULL && registry != Py_None &&
444 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000445 goto cleanup;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100446 else if (PyUnicode_CompareWithASCIIString(action, "ignore") == 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000447 goto return_none;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100448 else if (PyUnicode_CompareWithASCIIString(action, "once") == 0) {
Brett Cannondb734912008-06-27 00:52:15 +0000449 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000450 registry = get_once_registry();
451 if (registry == NULL)
452 goto cleanup;
453 }
454 /* _once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000456 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100457 else if (PyUnicode_CompareWithASCIIString(action, "module") == 0) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000458 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000459 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000461 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100462 else if (PyUnicode_CompareWithASCIIString(action, "default") != 0) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000463 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100464 "Unrecognized action (%R) in warnings.filters:\n %R",
465 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000466 goto cleanup;
467 }
468 }
469
Christian Heimes1a8501c2008-10-02 19:56:01 +0000470 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000471 goto return_none;
472 if (rc == 0) {
473 PyObject *show_fxn = get_warnings_attr("showwarning");
474 if (show_fxn == NULL) {
475 if (PyErr_Occurred())
476 goto cleanup;
477 show_warning(filename, lineno, text, category, sourceline);
478 }
479 else {
Brett Cannonec92e182008-09-02 02:46:59 +0000480 PyObject *res;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000481
Brett Cannon52a7d982011-07-17 19:17:55 -0700482 if (!PyCallable_Check(show_fxn)) {
Brett Cannonec92e182008-09-02 02:46:59 +0000483 PyErr_SetString(PyExc_TypeError,
484 "warnings.showwarning() must be set to a "
Brett Cannon52a7d982011-07-17 19:17:55 -0700485 "callable");
Christian Heimes8dc226f2008-05-06 23:45:46 +0000486 Py_DECREF(show_fxn);
Brett Cannonec92e182008-09-02 02:46:59 +0000487 goto cleanup;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000488 }
Brett Cannonec92e182008-09-02 02:46:59 +0000489
490 res = PyObject_CallFunctionObjArgs(show_fxn, message, category,
491 filename, lineno_obj,
492 NULL);
493 Py_DECREF(show_fxn);
494 Py_XDECREF(res);
495 if (res == NULL)
496 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000497 }
498 }
499 else /* if (rc == -1) */
500 goto cleanup;
501
502 return_none:
503 result = Py_None;
504 Py_INCREF(result);
505
506 cleanup:
Benjamin Petersondeff2b72015-05-03 11:23:37 -0400507 Py_XDECREF(item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000508 Py_XDECREF(key);
509 Py_XDECREF(text);
510 Py_XDECREF(lineno_obj);
511 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000512 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000513 return result; /* Py_None or NULL. */
514}
515
516/* filename, module, and registry are new refs, globals is borrowed */
517/* Returns 0 on error (no new refs), 1 on success */
518static int
519setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
520 PyObject **module, PyObject **registry)
521{
522 PyObject *globals;
523
524 /* Setup globals and lineno. */
525 PyFrameObject *f = PyThreadState_GET()->frame;
Christian Heimes5d8da202008-05-06 13:58:24 +0000526 while (--stack_level > 0 && f != NULL)
Christian Heimes33fe8092008-04-13 13:53:33 +0000527 f = f->f_back;
Christian Heimes33fe8092008-04-13 13:53:33 +0000528
529 if (f == NULL) {
530 globals = PyThreadState_Get()->interp->sysdict;
531 *lineno = 1;
532 }
533 else {
534 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000535 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000536 }
537
538 *module = NULL;
539
540 /* Setup registry. */
541 assert(globals != NULL);
542 assert(PyDict_Check(globals));
543 *registry = PyDict_GetItemString(globals, "__warningregistry__");
544 if (*registry == NULL) {
545 int rc;
546
547 *registry = PyDict_New();
548 if (*registry == NULL)
549 return 0;
550
551 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
552 if (rc < 0)
553 goto handle_error;
554 }
555 else
556 Py_INCREF(*registry);
557
558 /* Setup module. */
559 *module = PyDict_GetItemString(globals, "__name__");
560 if (*module == NULL) {
561 *module = PyUnicode_FromString("<string>");
562 if (*module == NULL)
563 goto handle_error;
564 }
565 else
566 Py_INCREF(*module);
567
568 /* Setup filename. */
569 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200570 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200571 Py_ssize_t len;
572 int kind;
573 void *data;
574
575 if (PyUnicode_READY(*filename))
576 goto handle_error;
577
Victor Stinner9e30aa52011-11-21 02:49:52 +0100578 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200579 kind = PyUnicode_KIND(*filename);
580 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000581
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500582#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000583 /* if filename.lower().endswith((".pyc", ".pyo")): */
584 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500586 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
587 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
588 (ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' ||
589 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o'))
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000590 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200591 *filename = PyUnicode_Substring(*filename, 0,
592 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000593 if (*filename == NULL)
594 goto handle_error;
595 }
596 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000597 Py_INCREF(*filename);
598 }
599 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500600 *filename = NULL;
Victor Stinner856f45f2013-10-30 00:04:59 +0100601 if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100602 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100603 /* PyList_Check() is needed because sys.argv is set to None during
604 Python finalization */
605 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000606 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000607 *filename = PyList_GetItem(argv, 0);
608 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000609 /* If sys.argv[0] is false, then use '__main__'. */
610 is_true = PyObject_IsTrue(*filename);
611 if (is_true < 0) {
612 Py_DECREF(*filename);
613 goto handle_error;
614 }
615 else if (!is_true) {
616 Py_DECREF(*filename);
Benjamin Peterson9f4bf1d2008-05-04 23:22:13 +0000617 *filename = PyUnicode_FromString("__main__");
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000618 if (*filename == NULL)
619 goto handle_error;
620 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000621 }
622 else {
623 /* embedded interpreters don't have sys.argv, see bug #839151 */
624 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100625 if (*filename == NULL)
626 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000627 }
628 }
629 if (*filename == NULL) {
630 *filename = *module;
631 Py_INCREF(*filename);
632 }
633 }
634
635 return 1;
636
637 handle_error:
638 /* filename not XDECREF'ed here as there is no way to jump here with a
639 dangling reference. */
640 Py_XDECREF(*registry);
641 Py_XDECREF(*module);
642 return 0;
643}
644
645static PyObject *
646get_category(PyObject *message, PyObject *category)
647{
648 int rc;
649
650 /* Get category. */
651 rc = PyObject_IsInstance(message, PyExc_Warning);
652 if (rc == -1)
653 return NULL;
654
655 if (rc == 1)
656 category = (PyObject*)message->ob_type;
657 else if (category == NULL)
658 category = PyExc_UserWarning;
659
660 /* Validate category. */
661 rc = PyObject_IsSubclass(category, PyExc_Warning);
662 if (rc == -1)
663 return NULL;
664 if (rc == 0) {
665 PyErr_SetString(PyExc_ValueError,
666 "category is not a subclass of Warning");
667 return NULL;
668 }
669
670 return category;
671}
672
673static PyObject *
674do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
675{
676 PyObject *filename, *module, *registry, *res;
677 int lineno;
678
679 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
680 return NULL;
681
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100682 res = warn_explicit(category, message, filename, lineno, module, registry,
683 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000684 Py_DECREF(filename);
685 Py_DECREF(registry);
686 Py_DECREF(module);
687 return res;
688}
689
690static PyObject *
691warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
692{
693 static char *kw_list[] = { "message", "category", "stacklevel", 0 };
694 PyObject *message, *category = NULL;
695 Py_ssize_t stack_level = 1;
696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list,
Christian Heimes33fe8092008-04-13 13:53:33 +0000698 &message, &category, &stack_level))
699 return NULL;
700
701 category = get_category(message, category);
702 if (category == NULL)
703 return NULL;
704 return do_warn(message, category, stack_level);
705}
706
707static PyObject *
708warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
709{
710 static char *kwd_list[] = {"message", "category", "filename", "lineno",
711 "module", "registry", "module_globals", 0};
712 PyObject *message;
713 PyObject *category;
714 PyObject *filename;
715 int lineno;
716 PyObject *module = NULL;
717 PyObject *registry = NULL;
718 PyObject *module_globals = NULL;
719
Victor Stinnera4c704b2013-10-29 23:43:41 +0100720 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000721 kwd_list, &message, &category, &filename, &lineno, &module,
722 &registry, &module_globals))
723 return NULL;
724
725 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200726 _Py_IDENTIFIER(get_source);
727 _Py_IDENTIFIER(splitlines);
728 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000729 PyObject *loader;
730 PyObject *module_name;
731 PyObject *source;
732 PyObject *source_list;
733 PyObject *source_line;
734 PyObject *returned;
735
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200736 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
737 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200738 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
739 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000740
741 /* Check/get the requisite pieces needed for the loader. */
742 loader = PyDict_GetItemString(module_globals, "__loader__");
743 module_name = PyDict_GetItemString(module_globals, "__name__");
744
745 if (loader == NULL || module_name == NULL)
746 goto standard_call;
747
748 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200749 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000750 goto standard_call;
751 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200752 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
753 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000754 if (!source)
755 return NULL;
756 else if (source == Py_None) {
757 Py_DECREF(Py_None);
758 goto standard_call;
759 }
760
761 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100762 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200763 PyId_splitlines.object,
764 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000765 Py_DECREF(source);
766 if (!source_list)
767 return NULL;
768
769 /* Get the source line. */
770 source_line = PyList_GetItem(source_list, lineno-1);
771 if (!source_line) {
772 Py_DECREF(source_list);
773 return NULL;
774 }
775
776 /* Handle the warning. */
777 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner14e461d2013-08-26 22:28:21 +0200778 registry, source_line);
Christian Heimes33fe8092008-04-13 13:53:33 +0000779 Py_DECREF(source_list);
780 return returned;
781 }
782
783 standard_call:
784 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner14e461d2013-08-26 22:28:21 +0200785 registry, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000786}
787
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200788static PyObject *
789warnings_filters_mutated(PyObject *self, PyObject *args)
790{
791 _filters_version++;
792 Py_RETURN_NONE;
793}
794
Christian Heimes33fe8092008-04-13 13:53:33 +0000795
796/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000797
798static int
799warn_unicode(PyObject *category, PyObject *message,
800 Py_ssize_t stack_level)
Christian Heimes33fe8092008-04-13 13:53:33 +0000801{
802 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000803
804 if (category == NULL)
805 category = PyExc_RuntimeWarning;
806
807 res = do_warn(message, category, stack_level);
Christian Heimes33fe8092008-04-13 13:53:33 +0000808 if (res == NULL)
809 return -1;
810 Py_DECREF(res);
811
812 return 0;
813}
814
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000815int
816PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
817 const char *format, ...)
818{
819 int ret;
820 PyObject *message;
821 va_list vargs;
822
823#ifdef HAVE_STDARG_PROTOTYPES
824 va_start(vargs, format);
825#else
826 va_start(vargs);
827#endif
828 message = PyUnicode_FromFormatV(format, vargs);
829 if (message != NULL) {
830 ret = warn_unicode(category, message, stack_level);
831 Py_DECREF(message);
832 }
833 else
834 ret = -1;
835 va_end(vargs);
836 return ret;
837}
838
839int
840PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
841{
842 int ret;
843 PyObject *message = PyUnicode_FromString(text);
844 if (message == NULL)
845 return -1;
846 ret = warn_unicode(category, message, stack_level);
847 Py_DECREF(message);
848 return ret;
849}
850
Ezio Melotti42da6632011-03-15 05:18:48 +0200851/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +0000852 Use PyErr_WarnEx instead. */
853
854#undef PyErr_Warn
855
856PyAPI_FUNC(int)
857PyErr_Warn(PyObject *category, char *text)
858{
859 return PyErr_WarnEx(category, text, 1);
860}
861
862/* Warning with explicit origin */
863int
Victor Stinner14e461d2013-08-26 22:28:21 +0200864PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
865 PyObject *filename, int lineno,
866 PyObject *module, PyObject *registry)
867{
868 PyObject *res;
869 if (category == NULL)
870 category = PyExc_RuntimeWarning;
871 res = warn_explicit(category, message, filename, lineno,
872 module, registry, NULL);
873 if (res == NULL)
874 return -1;
875 Py_DECREF(res);
876 return 0;
877}
878
879int
Christian Heimes33fe8092008-04-13 13:53:33 +0000880PyErr_WarnExplicit(PyObject *category, const char *text,
881 const char *filename_str, int lineno,
882 const char *module_str, PyObject *registry)
883{
Christian Heimes33fe8092008-04-13 13:53:33 +0000884 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +0000885 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +0000886 PyObject *module = NULL;
887 int ret = -1;
888
889 if (message == NULL || filename == NULL)
890 goto exit;
891 if (module_str != NULL) {
892 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200893 if (module == NULL)
894 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +0000895 }
896
Victor Stinner14e461d2013-08-26 22:28:21 +0200897 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
898 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000899
900 exit:
901 Py_XDECREF(message);
902 Py_XDECREF(module);
903 Py_XDECREF(filename);
904 return ret;
905}
906
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200907int
908PyErr_WarnExplicitFormat(PyObject *category,
909 const char *filename_str, int lineno,
910 const char *module_str, PyObject *registry,
911 const char *format, ...)
912{
913 PyObject *message;
914 PyObject *module = NULL;
915 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
916 int ret = -1;
917 va_list vargs;
918
919 if (filename == NULL)
920 goto exit;
921 if (module_str != NULL) {
922 module = PyUnicode_FromString(module_str);
923 if (module == NULL)
924 goto exit;
925 }
926
927#ifdef HAVE_STDARG_PROTOTYPES
928 va_start(vargs, format);
929#else
930 va_start(vargs);
931#endif
932 message = PyUnicode_FromFormatV(format, vargs);
933 if (message != NULL) {
934 PyObject *res;
935 res = warn_explicit(category, message, filename, lineno,
936 module, registry, NULL);
937 Py_DECREF(message);
938 if (res != NULL) {
939 Py_DECREF(res);
940 ret = 0;
941 }
942 }
943 va_end(vargs);
944exit:
945 Py_XDECREF(module);
946 Py_XDECREF(filename);
947 return ret;
948}
949
Christian Heimes33fe8092008-04-13 13:53:33 +0000950
951PyDoc_STRVAR(warn_doc,
952"Issue a warning, or maybe ignore it or raise an exception.");
953
954PyDoc_STRVAR(warn_explicit_doc,
955"Low-level inferface to warnings functionality.");
956
957static PyMethodDef warnings_functions[] = {
958 {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS,
959 warn_doc},
960 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
961 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200962 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
963 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +0000964 /* XXX(brett.cannon): add showwarning? */
965 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +0000967};
968
969
970static PyObject *
971create_filter(PyObject *category, const char *action)
972{
973 static PyObject *ignore_str = NULL;
974 static PyObject *error_str = NULL;
975 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +0000976 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000977 PyObject *action_obj = NULL;
978 PyObject *lineno, *result;
979
980 if (!strcmp(action, "ignore")) {
981 if (ignore_str == NULL) {
982 ignore_str = PyUnicode_InternFromString("ignore");
983 if (ignore_str == NULL)
984 return NULL;
985 }
986 action_obj = ignore_str;
987 }
988 else if (!strcmp(action, "error")) {
989 if (error_str == NULL) {
990 error_str = PyUnicode_InternFromString("error");
991 if (error_str == NULL)
992 return NULL;
993 }
994 action_obj = error_str;
995 }
996 else if (!strcmp(action, "default")) {
997 if (default_str == NULL) {
998 default_str = PyUnicode_InternFromString("default");
999 if (default_str == NULL)
1000 return NULL;
1001 }
1002 action_obj = default_str;
1003 }
Georg Brandl08be72d2010-10-24 15:11:22 +00001004 else if (!strcmp(action, "always")) {
1005 if (always_str == NULL) {
1006 always_str = PyUnicode_InternFromString("always");
1007 if (always_str == NULL)
1008 return NULL;
1009 }
1010 action_obj = always_str;
1011 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001012 else {
1013 Py_FatalError("unknown action");
1014 }
1015
1016 /* This assumes the line number is zero for now. */
1017 lineno = PyLong_FromLong(0);
1018 if (lineno == NULL)
1019 return NULL;
1020 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
1021 Py_DECREF(lineno);
1022 return result;
1023}
1024
1025static PyObject *
1026init_filters(void)
1027{
Georg Brandl08be72d2010-10-24 15:11:22 +00001028 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001029 unsigned int pos = 0; /* Post-incremented in each use. */
1030 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001031 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001032
Christian Heimes33fe8092008-04-13 13:53:33 +00001033 if (filters == NULL)
1034 return NULL;
1035
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001036 PyList_SET_ITEM(filters, pos++,
1037 create_filter(PyExc_DeprecationWarning, "ignore"));
1038 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001039 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001040 PyList_SET_ITEM(filters, pos++,
1041 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001042 if (Py_BytesWarningFlag > 1)
1043 bytes_action = "error";
1044 else if (Py_BytesWarningFlag)
1045 bytes_action = "default";
1046 else
1047 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001048 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001049 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001050 /* resource usage warnings are enabled by default in pydebug mode */
1051#ifdef Py_DEBUG
1052 resource_action = "always";
1053#else
1054 resource_action = "ignore";
1055#endif
1056 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1057 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001058 for (x = 0; x < pos; x += 1) {
1059 if (PyList_GET_ITEM(filters, x) == NULL) {
1060 Py_DECREF(filters);
1061 return NULL;
1062 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001063 }
1064
1065 return filters;
1066}
1067
Martin v. Löwis1a214512008-06-11 05:26:20 +00001068static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyModuleDef_HEAD_INIT,
1070 MODULE_NAME,
1071 warnings__doc__,
1072 0,
1073 warnings_functions,
1074 NULL,
1075 NULL,
1076 NULL,
1077 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001078};
1079
Christian Heimes33fe8092008-04-13 13:53:33 +00001080
1081PyMODINIT_FUNC
1082_PyWarnings_Init(void)
1083{
Brett Cannon0759dd62009-04-01 18:13:07 +00001084 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001085
Martin v. Löwis1a214512008-06-11 05:26:20 +00001086 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001087 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001088 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001089
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001090 if (_filters == NULL) {
1091 _filters = init_filters();
1092 if (_filters == NULL)
1093 return NULL;
1094 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001095 Py_INCREF(_filters);
1096 if (PyModule_AddObject(m, "filters", _filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001097 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001098
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001099 if (_once_registry == NULL) {
1100 _once_registry = PyDict_New();
1101 if (_once_registry == NULL)
1102 return NULL;
1103 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001104 Py_INCREF(_once_registry);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001105 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001106 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001107
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001108 if (_default_action == NULL) {
1109 _default_action = PyUnicode_FromString("default");
1110 if (_default_action == NULL)
1111 return NULL;
1112 }
1113 Py_INCREF(_default_action);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001114 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001115 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001116
1117 _filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001118 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001119}