blob: a1f4368ebe9236f2d39ad20893ec0db632ee0e82 [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
Christian Heimes33fe8092008-04-13 13:53:33 +0000104/* The item is a borrowed 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
135 tmp_item = *item = PyList_GET_ITEM(_filters, i);
136 if (PyTuple_Size(tmp_item) != 5) {
137 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 */
143 action = PyTuple_GET_ITEM(tmp_item, 0);
144 msg = PyTuple_GET_ITEM(tmp_item, 1);
145 cat = PyTuple_GET_ITEM(tmp_item, 2);
146 mod = PyTuple_GET_ITEM(tmp_item, 3);
147 ln_obj = PyTuple_GET_ITEM(tmp_item, 4);
148
149 good_msg = check_matched(msg, text);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100150 if (good_msg == -1)
151 return NULL;
152
Christian Heimes33fe8092008-04-13 13:53:33 +0000153 good_mod = check_matched(mod, module);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100154 if (good_mod == -1)
155 return NULL;
156
Christian Heimes33fe8092008-04-13 13:53:33 +0000157 is_subclass = PyObject_IsSubclass(category, cat);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100158 if (is_subclass == -1)
159 return NULL;
160
Christian Heimes33fe8092008-04-13 13:53:33 +0000161 ln = PyLong_AsSsize_t(ln_obj);
Victor Stinner3cd04aa2013-10-31 14:46:00 +0100162 if (ln == -1 && PyErr_Occurred())
Christian Heimes33fe8092008-04-13 13:53:33 +0000163 return NULL;
164
165 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln))
Victor Stinnera4c704b2013-10-29 23:43:41 +0100166 return action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000167 }
168
Brett Cannon0759dd62009-04-01 18:13:07 +0000169 action = get_default_action();
Victor Stinnera4c704b2013-10-29 23:43:41 +0100170 if (action != NULL)
171 return action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000172
173 PyErr_SetString(PyExc_ValueError,
Brett Cannon0759dd62009-04-01 18:13:07 +0000174 MODULE_NAME ".defaultaction not found");
Christian Heimes33fe8092008-04-13 13:53:33 +0000175 return NULL;
176}
177
Brett Cannon0759dd62009-04-01 18:13:07 +0000178
Christian Heimes33fe8092008-04-13 13:53:33 +0000179static int
180already_warned(PyObject *registry, PyObject *key, int should_set)
181{
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200182 PyObject *version_obj, *already_warned;
183 _Py_IDENTIFIER(version);
Christian Heimes33fe8092008-04-13 13:53:33 +0000184
185 if (key == NULL)
186 return -1;
187
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200188 version_obj = _PyDict_GetItemId(registry, &PyId_version);
189 if (version_obj == NULL
190 || !PyLong_CheckExact(version_obj)
191 || PyLong_AsLong(version_obj) != _filters_version) {
192 PyDict_Clear(registry);
193 version_obj = PyLong_FromLong(_filters_version);
194 if (version_obj == NULL)
195 return -1;
196 if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) {
197 Py_DECREF(version_obj);
198 return -1;
199 }
200 Py_DECREF(version_obj);
201 }
202 else {
203 already_warned = PyDict_GetItem(registry, key);
204 if (already_warned != NULL) {
205 int rc = PyObject_IsTrue(already_warned);
206 if (rc != 0)
207 return rc;
208 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000209 }
210
211 /* This warning wasn't found in the registry, set it. */
212 if (should_set)
213 return PyDict_SetItem(registry, key, Py_True);
214 return 0;
215}
216
217/* New reference. */
218static PyObject *
219normalize_module(PyObject *filename)
220{
221 PyObject *module;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100222 int kind;
223 void *data;
Christian Heimes33fe8092008-04-13 13:53:33 +0000224 Py_ssize_t len;
225
Victor Stinner9e30aa52011-11-21 02:49:52 +0100226 len = PyUnicode_GetLength(filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000227 if (len < 0)
228 return NULL;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100229
230 if (len == 0)
231 return PyUnicode_FromString("<unknown>");
232
233 kind = PyUnicode_KIND(filename);
234 data = PyUnicode_DATA(filename);
235
236 /* if filename.endswith(".py"): */
Christian Heimes33fe8092008-04-13 13:53:33 +0000237 if (len >= 3 &&
Victor Stinnera4c704b2013-10-29 23:43:41 +0100238 PyUnicode_READ(kind, data, len-3) == '.' &&
239 PyUnicode_READ(kind, data, len-2) == 'p' &&
240 PyUnicode_READ(kind, data, len-1) == 'y')
241 {
Victor Stinner9e30aa52011-11-21 02:49:52 +0100242 module = PyUnicode_Substring(filename, 0, len-3);
Christian Heimes33fe8092008-04-13 13:53:33 +0000243 }
244 else {
245 module = filename;
246 Py_INCREF(module);
247 }
248 return module;
249}
250
251static int
252update_registry(PyObject *registry, PyObject *text, PyObject *category,
253 int add_zero)
254{
255 PyObject *altkey, *zero = NULL;
256 int rc;
257
258 if (add_zero) {
259 zero = PyLong_FromLong(0);
260 if (zero == NULL)
261 return -1;
262 altkey = PyTuple_Pack(3, text, category, zero);
263 }
264 else
265 altkey = PyTuple_Pack(2, text, category);
266
267 rc = already_warned(registry, altkey, 1);
268 Py_XDECREF(zero);
269 Py_XDECREF(altkey);
270 return rc;
271}
272
273static void
274show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
275 *category, PyObject *sourceline)
276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PyObject *f_stderr;
278 PyObject *name;
Christian Heimes33fe8092008-04-13 13:53:33 +0000279 char lineno_str[128];
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200280 _Py_IDENTIFIER(__name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000281
282 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
283
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200284 name = _PyObject_GetAttrId(category, &PyId___name__);
Christian Heimes33fe8092008-04-13 13:53:33 +0000285 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100286 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000287
Victor Stinnerbd303c12013-11-07 23:07:29 +0100288 f_stderr = _PySys_GetObjectId(&PyId_stderr);
Christian Heimes33fe8092008-04-13 13:53:33 +0000289 if (f_stderr == NULL) {
290 fprintf(stderr, "lost sys.stderr\n");
Victor Stinnerae233ea2013-10-31 14:51:38 +0100291 goto error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000292 }
293
294 /* Print "filename:lineno: category: text\n" */
Victor Stinnerae233ea2013-10-31 14:51:38 +0100295 if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
296 goto error;
297 if (PyFile_WriteString(lineno_str, f_stderr) < 0)
298 goto error;
299 if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
300 goto error;
301 if (PyFile_WriteString(": ", f_stderr) < 0)
302 goto error;
303 if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
304 goto error;
305 if (PyFile_WriteString("\n", f_stderr) < 0)
306 goto error;
307 Py_CLEAR(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000308
309 /* Print " source_line\n" */
Christian Heimes33fe8092008-04-13 13:53:33 +0000310 if (sourceline) {
Victor Stinnera4c704b2013-10-29 23:43:41 +0100311 int kind;
312 void *data;
313 Py_ssize_t i, len;
314 Py_UCS4 ch;
315 PyObject *truncated;
Christian Heimes33fe8092008-04-13 13:53:33 +0000316
Victor Stinnera4c704b2013-10-29 23:43:41 +0100317 if (PyUnicode_READY(sourceline) < 1)
318 goto error;
319
320 kind = PyUnicode_KIND(sourceline);
321 data = PyUnicode_DATA(sourceline);
322 len = PyUnicode_GET_LENGTH(sourceline);
323 for (i=0; i<len; i++) {
324 ch = PyUnicode_READ(kind, data, i);
325 if (ch != ' ' && ch != '\t' && ch != '\014')
326 break;
327 }
328
329 truncated = PyUnicode_Substring(sourceline, i, len);
330 if (truncated == NULL)
331 goto error;
332
333 PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW);
334 Py_DECREF(truncated);
Christian Heimes33fe8092008-04-13 13:53:33 +0000335 PyFile_WriteString("\n", f_stderr);
336 }
Victor Stinner78e2c982013-07-16 01:54:37 +0200337 else {
338 _Py_DisplaySourceLine(f_stderr, filename, lineno, 2);
339 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100340
341error:
Victor Stinnerae233ea2013-10-31 14:51:38 +0100342 Py_XDECREF(name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000343 PyErr_Clear();
344}
345
346static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347warn_explicit(PyObject *category, PyObject *message,
Christian Heimes33fe8092008-04-13 13:53:33 +0000348 PyObject *filename, int lineno,
349 PyObject *module, PyObject *registry, PyObject *sourceline)
350{
351 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL;
352 PyObject *item = Py_None;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100353 PyObject *action;
Christian Heimes33fe8092008-04-13 13:53:33 +0000354 int rc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100356 /* module can be None if a warning is emitted late during Python shutdown.
357 In this case, the Python warnings module was probably unloaded, filters
358 are no more available to choose as action. It is safer to ignore the
359 warning and do nothing. */
360 if (module == Py_None)
361 Py_RETURN_NONE;
362
Brett Cannondb734912008-06-27 00:52:15 +0000363 if (registry && !PyDict_Check(registry) && (registry != Py_None)) {
364 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict");
365 return NULL;
366 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000367
368 /* Normalize module. */
369 if (module == NULL) {
370 module = normalize_module(filename);
371 if (module == NULL)
372 return NULL;
373 }
374 else
375 Py_INCREF(module);
376
377 /* Normalize message. */
378 Py_INCREF(message); /* DECREF'ed in cleanup. */
379 rc = PyObject_IsInstance(message, PyExc_Warning);
380 if (rc == -1) {
381 goto cleanup;
382 }
383 if (rc == 1) {
384 text = PyObject_Str(message);
Hirokazu Yamamoto1c0c0032009-07-17 06:55:42 +0000385 if (text == NULL)
386 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000387 category = (PyObject*)message->ob_type;
388 }
389 else {
390 text = message;
391 message = PyObject_CallFunction(category, "O", message);
Brett Cannondb734912008-06-27 00:52:15 +0000392 if (message == NULL)
393 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000394 }
395
396 lineno_obj = PyLong_FromLong(lineno);
397 if (lineno_obj == NULL)
398 goto cleanup;
399
400 /* Create key. */
401 key = PyTuple_Pack(3, text, category, lineno_obj);
402 if (key == NULL)
403 goto cleanup;
404
Brett Cannondb734912008-06-27 00:52:15 +0000405 if ((registry != NULL) && (registry != Py_None)) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000406 rc = already_warned(registry, key, 0);
407 if (rc == -1)
408 goto cleanup;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 else if (rc == 1)
Christian Heimes33fe8092008-04-13 13:53:33 +0000410 goto return_none;
411 /* Else this warning hasn't been generated before. */
412 }
413
414 action = get_filter(category, text, lineno, module, &item);
415 if (action == NULL)
416 goto cleanup;
417
Victor Stinnera4c704b2013-10-29 23:43:41 +0100418 if (PyUnicode_CompareWithASCIIString(action, "error") == 0) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000419 PyErr_SetObject(category, message);
420 goto cleanup;
421 }
422
423 /* Store in the registry that we've been here, *except* when the action
424 is "always". */
425 rc = 0;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100426 if (PyUnicode_CompareWithASCIIString(action, "always") != 0) {
Brett Cannondb734912008-06-27 00:52:15 +0000427 if (registry != NULL && registry != Py_None &&
428 PyDict_SetItem(registry, key, Py_True) < 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000429 goto cleanup;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100430 else if (PyUnicode_CompareWithASCIIString(action, "ignore") == 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000431 goto return_none;
Victor Stinnera4c704b2013-10-29 23:43:41 +0100432 else if (PyUnicode_CompareWithASCIIString(action, "once") == 0) {
Brett Cannondb734912008-06-27 00:52:15 +0000433 if (registry == NULL || registry == Py_None) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000434 registry = get_once_registry();
435 if (registry == NULL)
436 goto cleanup;
437 }
438 /* _once_registry[(text, category)] = 1 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000440 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100441 else if (PyUnicode_CompareWithASCIIString(action, "module") == 0) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000442 /* registry[(text, category, 0)] = 1 */
Brett Cannondb734912008-06-27 00:52:15 +0000443 if (registry != NULL && registry != Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 rc = update_registry(registry, text, category, 0);
Christian Heimes33fe8092008-04-13 13:53:33 +0000445 }
Victor Stinnera4c704b2013-10-29 23:43:41 +0100446 else if (PyUnicode_CompareWithASCIIString(action, "default") != 0) {
Christian Heimes33fe8092008-04-13 13:53:33 +0000447 PyErr_Format(PyExc_RuntimeError,
Victor Stinnera4c704b2013-10-29 23:43:41 +0100448 "Unrecognized action (%R) in warnings.filters:\n %R",
449 action, item);
Christian Heimes33fe8092008-04-13 13:53:33 +0000450 goto cleanup;
451 }
452 }
453
Christian Heimes1a8501c2008-10-02 19:56:01 +0000454 if (rc == 1) /* Already warned for this module. */
Christian Heimes33fe8092008-04-13 13:53:33 +0000455 goto return_none;
456 if (rc == 0) {
457 PyObject *show_fxn = get_warnings_attr("showwarning");
458 if (show_fxn == NULL) {
459 if (PyErr_Occurred())
460 goto cleanup;
461 show_warning(filename, lineno, text, category, sourceline);
462 }
463 else {
Brett Cannonec92e182008-09-02 02:46:59 +0000464 PyObject *res;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000465
Brett Cannon52a7d982011-07-17 19:17:55 -0700466 if (!PyCallable_Check(show_fxn)) {
Brett Cannonec92e182008-09-02 02:46:59 +0000467 PyErr_SetString(PyExc_TypeError,
468 "warnings.showwarning() must be set to a "
Brett Cannon52a7d982011-07-17 19:17:55 -0700469 "callable");
Christian Heimes8dc226f2008-05-06 23:45:46 +0000470 Py_DECREF(show_fxn);
Brett Cannonec92e182008-09-02 02:46:59 +0000471 goto cleanup;
Christian Heimes8dc226f2008-05-06 23:45:46 +0000472 }
Brett Cannonec92e182008-09-02 02:46:59 +0000473
474 res = PyObject_CallFunctionObjArgs(show_fxn, message, category,
475 filename, lineno_obj,
476 NULL);
477 Py_DECREF(show_fxn);
478 Py_XDECREF(res);
479 if (res == NULL)
480 goto cleanup;
Christian Heimes33fe8092008-04-13 13:53:33 +0000481 }
482 }
483 else /* if (rc == -1) */
484 goto cleanup;
485
486 return_none:
487 result = Py_None;
488 Py_INCREF(result);
489
490 cleanup:
491 Py_XDECREF(key);
492 Py_XDECREF(text);
493 Py_XDECREF(lineno_obj);
494 Py_DECREF(module);
Brett Cannondb734912008-06-27 00:52:15 +0000495 Py_XDECREF(message);
Christian Heimes33fe8092008-04-13 13:53:33 +0000496 return result; /* Py_None or NULL. */
497}
498
499/* filename, module, and registry are new refs, globals is borrowed */
500/* Returns 0 on error (no new refs), 1 on success */
501static int
502setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
503 PyObject **module, PyObject **registry)
504{
505 PyObject *globals;
506
507 /* Setup globals and lineno. */
508 PyFrameObject *f = PyThreadState_GET()->frame;
Christian Heimes5d8da202008-05-06 13:58:24 +0000509 while (--stack_level > 0 && f != NULL)
Christian Heimes33fe8092008-04-13 13:53:33 +0000510 f = f->f_back;
Christian Heimes33fe8092008-04-13 13:53:33 +0000511
512 if (f == NULL) {
513 globals = PyThreadState_Get()->interp->sysdict;
514 *lineno = 1;
515 }
516 else {
517 globals = f->f_globals;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000518 *lineno = PyFrame_GetLineNumber(f);
Christian Heimes33fe8092008-04-13 13:53:33 +0000519 }
520
521 *module = NULL;
522
523 /* Setup registry. */
524 assert(globals != NULL);
525 assert(PyDict_Check(globals));
526 *registry = PyDict_GetItemString(globals, "__warningregistry__");
527 if (*registry == NULL) {
528 int rc;
529
530 *registry = PyDict_New();
531 if (*registry == NULL)
532 return 0;
533
534 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry);
535 if (rc < 0)
536 goto handle_error;
537 }
538 else
539 Py_INCREF(*registry);
540
541 /* Setup module. */
542 *module = PyDict_GetItemString(globals, "__name__");
543 if (*module == NULL) {
544 *module = PyUnicode_FromString("<string>");
545 if (*module == NULL)
546 goto handle_error;
547 }
548 else
549 Py_INCREF(*module);
550
551 /* Setup filename. */
552 *filename = PyDict_GetItemString(globals, "__file__");
Victor Stinner8b0508e2011-07-04 02:43:09 +0200553 if (*filename != NULL && PyUnicode_Check(*filename)) {
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200554 Py_ssize_t len;
555 int kind;
556 void *data;
557
558 if (PyUnicode_READY(*filename))
559 goto handle_error;
560
Victor Stinner9e30aa52011-11-21 02:49:52 +0100561 len = PyUnicode_GetLength(*filename);
Victor Stinnerb62a7b22011-10-06 02:34:51 +0200562 kind = PyUnicode_KIND(*filename);
563 data = PyUnicode_DATA(*filename);
Christian Heimes33fe8092008-04-13 13:53:33 +0000564
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500565#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0)
Christian Heimes33fe8092008-04-13 13:53:33 +0000566 /* if filename.lower().endswith((".pyc", ".pyo")): */
567 if (len >= 4 &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200568 PyUnicode_READ(kind, data, len-4) == '.' &&
Benjamin Peterson21e0da22012-01-11 21:00:42 -0500569 ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' &&
570 ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' &&
571 (ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' ||
572 ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o'))
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000573 {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200574 *filename = PyUnicode_Substring(*filename, 0,
575 PyUnicode_GET_LENGTH(*filename)-1);
Victor Stinner2e5f1172010-08-08 22:12:45 +0000576 if (*filename == NULL)
577 goto handle_error;
578 }
579 else
Christian Heimes33fe8092008-04-13 13:53:33 +0000580 Py_INCREF(*filename);
581 }
582 else {
Benjamin Petersonbb4a7472011-07-04 22:27:16 -0500583 *filename = NULL;
Victor Stinner856f45f2013-10-30 00:04:59 +0100584 if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
Victor Stinnerbd303c12013-11-07 23:07:29 +0100585 PyObject *argv = _PySys_GetObjectId(&PyId_argv);
Victor Stinnerce5f4fb2013-10-28 18:47:22 +0100586 /* PyList_Check() is needed because sys.argv is set to None during
587 Python finalization */
588 if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000589 int is_true;
Christian Heimes33fe8092008-04-13 13:53:33 +0000590 *filename = PyList_GetItem(argv, 0);
591 Py_INCREF(*filename);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000592 /* If sys.argv[0] is false, then use '__main__'. */
593 is_true = PyObject_IsTrue(*filename);
594 if (is_true < 0) {
595 Py_DECREF(*filename);
596 goto handle_error;
597 }
598 else if (!is_true) {
599 Py_DECREF(*filename);
Benjamin Peterson9f4bf1d2008-05-04 23:22:13 +0000600 *filename = PyUnicode_FromString("__main__");
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000601 if (*filename == NULL)
602 goto handle_error;
603 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000604 }
605 else {
606 /* embedded interpreters don't have sys.argv, see bug #839151 */
607 *filename = PyUnicode_FromString("__main__");
Victor Stinner856f45f2013-10-30 00:04:59 +0100608 if (*filename == NULL)
609 goto handle_error;
Christian Heimes33fe8092008-04-13 13:53:33 +0000610 }
611 }
612 if (*filename == NULL) {
613 *filename = *module;
614 Py_INCREF(*filename);
615 }
616 }
617
618 return 1;
619
620 handle_error:
621 /* filename not XDECREF'ed here as there is no way to jump here with a
622 dangling reference. */
623 Py_XDECREF(*registry);
624 Py_XDECREF(*module);
625 return 0;
626}
627
628static PyObject *
629get_category(PyObject *message, PyObject *category)
630{
631 int rc;
632
633 /* Get category. */
634 rc = PyObject_IsInstance(message, PyExc_Warning);
635 if (rc == -1)
636 return NULL;
637
638 if (rc == 1)
639 category = (PyObject*)message->ob_type;
Berker Peksagd8089e02014-07-11 19:50:25 +0300640 else if (category == NULL || category == Py_None)
Christian Heimes33fe8092008-04-13 13:53:33 +0000641 category = PyExc_UserWarning;
642
643 /* Validate category. */
644 rc = PyObject_IsSubclass(category, PyExc_Warning);
Berker Peksagd8089e02014-07-11 19:50:25 +0300645 /* category is not a subclass of PyExc_Warning or
646 PyObject_IsSubclass raised an error */
647 if (rc == -1 || rc == 0) {
648 PyErr_Format(PyExc_TypeError,
649 "category must be a Warning subclass, not '%s'",
650 Py_TYPE(category)->tp_name);
Christian Heimes33fe8092008-04-13 13:53:33 +0000651 return NULL;
652 }
653
654 return category;
655}
656
657static PyObject *
658do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
659{
660 PyObject *filename, *module, *registry, *res;
661 int lineno;
662
663 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
664 return NULL;
665
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100666 res = warn_explicit(category, message, filename, lineno, module, registry,
667 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000668 Py_DECREF(filename);
669 Py_DECREF(registry);
670 Py_DECREF(module);
671 return res;
672}
673
674static PyObject *
675warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
676{
677 static char *kw_list[] = { "message", "category", "stacklevel", 0 };
678 PyObject *message, *category = NULL;
679 Py_ssize_t stack_level = 1;
680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list,
Christian Heimes33fe8092008-04-13 13:53:33 +0000682 &message, &category, &stack_level))
683 return NULL;
684
685 category = get_category(message, category);
686 if (category == NULL)
687 return NULL;
688 return do_warn(message, category, stack_level);
689}
690
691static PyObject *
692warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
693{
694 static char *kwd_list[] = {"message", "category", "filename", "lineno",
695 "module", "registry", "module_globals", 0};
696 PyObject *message;
697 PyObject *category;
698 PyObject *filename;
699 int lineno;
700 PyObject *module = NULL;
701 PyObject *registry = NULL;
702 PyObject *module_globals = NULL;
703
Victor Stinnera4c704b2013-10-29 23:43:41 +0100704 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000705 kwd_list, &message, &category, &filename, &lineno, &module,
706 &registry, &module_globals))
707 return NULL;
708
709 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200710 _Py_IDENTIFIER(get_source);
711 _Py_IDENTIFIER(splitlines);
712 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000713 PyObject *loader;
714 PyObject *module_name;
715 PyObject *source;
716 PyObject *source_list;
717 PyObject *source_line;
718 PyObject *returned;
719
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200720 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
721 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200722 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
723 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000724
725 /* Check/get the requisite pieces needed for the loader. */
726 loader = PyDict_GetItemString(module_globals, "__loader__");
727 module_name = PyDict_GetItemString(module_globals, "__name__");
728
729 if (loader == NULL || module_name == NULL)
730 goto standard_call;
731
732 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200733 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000734 goto standard_call;
735 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200736 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
737 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000738 if (!source)
739 return NULL;
740 else if (source == Py_None) {
741 Py_DECREF(Py_None);
742 goto standard_call;
743 }
744
745 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100746 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200747 PyId_splitlines.object,
748 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000749 Py_DECREF(source);
750 if (!source_list)
751 return NULL;
752
753 /* Get the source line. */
754 source_line = PyList_GetItem(source_list, lineno-1);
755 if (!source_line) {
756 Py_DECREF(source_list);
757 return NULL;
758 }
759
760 /* Handle the warning. */
761 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner14e461d2013-08-26 22:28:21 +0200762 registry, source_line);
Christian Heimes33fe8092008-04-13 13:53:33 +0000763 Py_DECREF(source_list);
764 return returned;
765 }
766
767 standard_call:
768 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner14e461d2013-08-26 22:28:21 +0200769 registry, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000770}
771
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200772static PyObject *
773warnings_filters_mutated(PyObject *self, PyObject *args)
774{
775 _filters_version++;
776 Py_RETURN_NONE;
777}
778
Christian Heimes33fe8092008-04-13 13:53:33 +0000779
780/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000781
782static int
783warn_unicode(PyObject *category, PyObject *message,
784 Py_ssize_t stack_level)
Christian Heimes33fe8092008-04-13 13:53:33 +0000785{
786 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000787
788 if (category == NULL)
789 category = PyExc_RuntimeWarning;
790
791 res = do_warn(message, category, stack_level);
Christian Heimes33fe8092008-04-13 13:53:33 +0000792 if (res == NULL)
793 return -1;
794 Py_DECREF(res);
795
796 return 0;
797}
798
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000799int
800PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
801 const char *format, ...)
802{
803 int ret;
804 PyObject *message;
805 va_list vargs;
806
807#ifdef HAVE_STDARG_PROTOTYPES
808 va_start(vargs, format);
809#else
810 va_start(vargs);
811#endif
812 message = PyUnicode_FromFormatV(format, vargs);
813 if (message != NULL) {
814 ret = warn_unicode(category, message, stack_level);
815 Py_DECREF(message);
816 }
817 else
818 ret = -1;
819 va_end(vargs);
820 return ret;
821}
822
823int
824PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
825{
826 int ret;
827 PyObject *message = PyUnicode_FromString(text);
828 if (message == NULL)
829 return -1;
830 ret = warn_unicode(category, message, stack_level);
831 Py_DECREF(message);
832 return ret;
833}
834
Ezio Melotti42da6632011-03-15 05:18:48 +0200835/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +0000836 Use PyErr_WarnEx instead. */
837
838#undef PyErr_Warn
839
840PyAPI_FUNC(int)
841PyErr_Warn(PyObject *category, char *text)
842{
843 return PyErr_WarnEx(category, text, 1);
844}
845
846/* Warning with explicit origin */
847int
Victor Stinner14e461d2013-08-26 22:28:21 +0200848PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
849 PyObject *filename, int lineno,
850 PyObject *module, PyObject *registry)
851{
852 PyObject *res;
853 if (category == NULL)
854 category = PyExc_RuntimeWarning;
855 res = warn_explicit(category, message, filename, lineno,
856 module, registry, NULL);
857 if (res == NULL)
858 return -1;
859 Py_DECREF(res);
860 return 0;
861}
862
863int
Christian Heimes33fe8092008-04-13 13:53:33 +0000864PyErr_WarnExplicit(PyObject *category, const char *text,
865 const char *filename_str, int lineno,
866 const char *module_str, PyObject *registry)
867{
Christian Heimes33fe8092008-04-13 13:53:33 +0000868 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +0000869 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +0000870 PyObject *module = NULL;
871 int ret = -1;
872
873 if (message == NULL || filename == NULL)
874 goto exit;
875 if (module_str != NULL) {
876 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200877 if (module == NULL)
878 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +0000879 }
880
Victor Stinner14e461d2013-08-26 22:28:21 +0200881 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
882 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000883
884 exit:
885 Py_XDECREF(message);
886 Py_XDECREF(module);
887 Py_XDECREF(filename);
888 return ret;
889}
890
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200891int
892PyErr_WarnExplicitFormat(PyObject *category,
893 const char *filename_str, int lineno,
894 const char *module_str, PyObject *registry,
895 const char *format, ...)
896{
897 PyObject *message;
898 PyObject *module = NULL;
899 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
900 int ret = -1;
901 va_list vargs;
902
903 if (filename == NULL)
904 goto exit;
905 if (module_str != NULL) {
906 module = PyUnicode_FromString(module_str);
907 if (module == NULL)
908 goto exit;
909 }
910
911#ifdef HAVE_STDARG_PROTOTYPES
912 va_start(vargs, format);
913#else
914 va_start(vargs);
915#endif
916 message = PyUnicode_FromFormatV(format, vargs);
917 if (message != NULL) {
918 PyObject *res;
919 res = warn_explicit(category, message, filename, lineno,
920 module, registry, NULL);
921 Py_DECREF(message);
922 if (res != NULL) {
923 Py_DECREF(res);
924 ret = 0;
925 }
926 }
927 va_end(vargs);
928exit:
929 Py_XDECREF(module);
930 Py_XDECREF(filename);
931 return ret;
932}
933
Christian Heimes33fe8092008-04-13 13:53:33 +0000934
935PyDoc_STRVAR(warn_doc,
936"Issue a warning, or maybe ignore it or raise an exception.");
937
938PyDoc_STRVAR(warn_explicit_doc,
939"Low-level inferface to warnings functionality.");
940
941static PyMethodDef warnings_functions[] = {
942 {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS,
943 warn_doc},
944 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
945 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200946 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
947 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +0000948 /* XXX(brett.cannon): add showwarning? */
949 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +0000951};
952
953
954static PyObject *
955create_filter(PyObject *category, const char *action)
956{
957 static PyObject *ignore_str = NULL;
958 static PyObject *error_str = NULL;
959 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +0000960 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000961 PyObject *action_obj = NULL;
962 PyObject *lineno, *result;
963
964 if (!strcmp(action, "ignore")) {
965 if (ignore_str == NULL) {
966 ignore_str = PyUnicode_InternFromString("ignore");
967 if (ignore_str == NULL)
968 return NULL;
969 }
970 action_obj = ignore_str;
971 }
972 else if (!strcmp(action, "error")) {
973 if (error_str == NULL) {
974 error_str = PyUnicode_InternFromString("error");
975 if (error_str == NULL)
976 return NULL;
977 }
978 action_obj = error_str;
979 }
980 else if (!strcmp(action, "default")) {
981 if (default_str == NULL) {
982 default_str = PyUnicode_InternFromString("default");
983 if (default_str == NULL)
984 return NULL;
985 }
986 action_obj = default_str;
987 }
Georg Brandl08be72d2010-10-24 15:11:22 +0000988 else if (!strcmp(action, "always")) {
989 if (always_str == NULL) {
990 always_str = PyUnicode_InternFromString("always");
991 if (always_str == NULL)
992 return NULL;
993 }
994 action_obj = always_str;
995 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000996 else {
997 Py_FatalError("unknown action");
998 }
999
1000 /* This assumes the line number is zero for now. */
1001 lineno = PyLong_FromLong(0);
1002 if (lineno == NULL)
1003 return NULL;
1004 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
1005 Py_DECREF(lineno);
1006 return result;
1007}
1008
1009static PyObject *
1010init_filters(void)
1011{
Georg Brandl08be72d2010-10-24 15:11:22 +00001012 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001013 unsigned int pos = 0; /* Post-incremented in each use. */
1014 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001015 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001016
Christian Heimes33fe8092008-04-13 13:53:33 +00001017 if (filters == NULL)
1018 return NULL;
1019
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001020 PyList_SET_ITEM(filters, pos++,
1021 create_filter(PyExc_DeprecationWarning, "ignore"));
1022 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001023 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001024 PyList_SET_ITEM(filters, pos++,
1025 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001026 if (Py_BytesWarningFlag > 1)
1027 bytes_action = "error";
1028 else if (Py_BytesWarningFlag)
1029 bytes_action = "default";
1030 else
1031 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001032 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001033 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001034 /* resource usage warnings are enabled by default in pydebug mode */
1035#ifdef Py_DEBUG
1036 resource_action = "always";
1037#else
1038 resource_action = "ignore";
1039#endif
1040 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1041 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001042 for (x = 0; x < pos; x += 1) {
1043 if (PyList_GET_ITEM(filters, x) == NULL) {
1044 Py_DECREF(filters);
1045 return NULL;
1046 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001047 }
1048
1049 return filters;
1050}
1051
Martin v. Löwis1a214512008-06-11 05:26:20 +00001052static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 PyModuleDef_HEAD_INIT,
1054 MODULE_NAME,
1055 warnings__doc__,
1056 0,
1057 warnings_functions,
1058 NULL,
1059 NULL,
1060 NULL,
1061 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001062};
1063
Christian Heimes33fe8092008-04-13 13:53:33 +00001064
1065PyMODINIT_FUNC
1066_PyWarnings_Init(void)
1067{
Brett Cannon0759dd62009-04-01 18:13:07 +00001068 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001069
Martin v. Löwis1a214512008-06-11 05:26:20 +00001070 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001071 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001072 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001073
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001074 if (_filters == NULL) {
1075 _filters = init_filters();
1076 if (_filters == NULL)
1077 return NULL;
1078 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001079 Py_INCREF(_filters);
1080 if (PyModule_AddObject(m, "filters", _filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001081 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001082
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001083 if (_once_registry == NULL) {
1084 _once_registry = PyDict_New();
1085 if (_once_registry == NULL)
1086 return NULL;
1087 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001088 Py_INCREF(_once_registry);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001089 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001090 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001091
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001092 if (_default_action == NULL) {
1093 _default_action = PyUnicode_FromString("default");
1094 if (_default_action == NULL)
1095 return NULL;
1096 }
1097 Py_INCREF(_default_action);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001098 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001099 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001100
1101 _filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001102 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001103}