blob: bef56479b3357d621f3d36f070bf15dab8686245 [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;
640 else if (category == NULL)
641 category = PyExc_UserWarning;
642
643 /* Validate category. */
644 rc = PyObject_IsSubclass(category, PyExc_Warning);
645 if (rc == -1)
646 return NULL;
647 if (rc == 0) {
648 PyErr_SetString(PyExc_ValueError,
649 "category is not a subclass of Warning");
650 return NULL;
651 }
652
653 return category;
654}
655
656static PyObject *
657do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level)
658{
659 PyObject *filename, *module, *registry, *res;
660 int lineno;
661
662 if (!setup_context(stack_level, &filename, &lineno, &module, &registry))
663 return NULL;
664
Victor Stinnerdcdd05b2013-11-01 00:55:30 +0100665 res = warn_explicit(category, message, filename, lineno, module, registry,
666 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000667 Py_DECREF(filename);
668 Py_DECREF(registry);
669 Py_DECREF(module);
670 return res;
671}
672
673static PyObject *
674warnings_warn(PyObject *self, PyObject *args, PyObject *kwds)
675{
676 static char *kw_list[] = { "message", "category", "stacklevel", 0 };
677 PyObject *message, *category = NULL;
678 Py_ssize_t stack_level = 1;
679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list,
Christian Heimes33fe8092008-04-13 13:53:33 +0000681 &message, &category, &stack_level))
682 return NULL;
683
684 category = get_category(message, category);
685 if (category == NULL)
686 return NULL;
687 return do_warn(message, category, stack_level);
688}
689
690static PyObject *
691warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds)
692{
693 static char *kwd_list[] = {"message", "category", "filename", "lineno",
694 "module", "registry", "module_globals", 0};
695 PyObject *message;
696 PyObject *category;
697 PyObject *filename;
698 int lineno;
699 PyObject *module = NULL;
700 PyObject *registry = NULL;
701 PyObject *module_globals = NULL;
702
Victor Stinnera4c704b2013-10-29 23:43:41 +0100703 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOO:warn_explicit",
Christian Heimes33fe8092008-04-13 13:53:33 +0000704 kwd_list, &message, &category, &filename, &lineno, &module,
705 &registry, &module_globals))
706 return NULL;
707
708 if (module_globals) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200709 _Py_IDENTIFIER(get_source);
710 _Py_IDENTIFIER(splitlines);
711 PyObject *tmp;
Christian Heimes33fe8092008-04-13 13:53:33 +0000712 PyObject *loader;
713 PyObject *module_name;
714 PyObject *source;
715 PyObject *source_list;
716 PyObject *source_line;
717 PyObject *returned;
718
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200719 if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL)
720 return NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200721 if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL)
722 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000723
724 /* Check/get the requisite pieces needed for the loader. */
725 loader = PyDict_GetItemString(module_globals, "__loader__");
726 module_name = PyDict_GetItemString(module_globals, "__name__");
727
728 if (loader == NULL || module_name == NULL)
729 goto standard_call;
730
731 /* Make sure the loader implements the optional get_source() method. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200732 if (!_PyObject_HasAttrId(loader, &PyId_get_source))
Christian Heimes33fe8092008-04-13 13:53:33 +0000733 goto standard_call;
734 /* Call get_source() to get the source code. */
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200735 source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object,
736 module_name, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000737 if (!source)
738 return NULL;
739 else if (source == Py_None) {
740 Py_DECREF(Py_None);
741 goto standard_call;
742 }
743
744 /* Split the source into lines. */
Victor Stinner9e30aa52011-11-21 02:49:52 +0100745 source_list = PyObject_CallMethodObjArgs(source,
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200746 PyId_splitlines.object,
747 NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000748 Py_DECREF(source);
749 if (!source_list)
750 return NULL;
751
752 /* Get the source line. */
753 source_line = PyList_GetItem(source_list, lineno-1);
754 if (!source_line) {
755 Py_DECREF(source_list);
756 return NULL;
757 }
758
759 /* Handle the warning. */
760 returned = warn_explicit(category, message, filename, lineno, module,
Victor Stinner14e461d2013-08-26 22:28:21 +0200761 registry, source_line);
Christian Heimes33fe8092008-04-13 13:53:33 +0000762 Py_DECREF(source_list);
763 return returned;
764 }
765
766 standard_call:
767 return warn_explicit(category, message, filename, lineno, module,
Victor Stinner14e461d2013-08-26 22:28:21 +0200768 registry, NULL);
Christian Heimes33fe8092008-04-13 13:53:33 +0000769}
770
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200771static PyObject *
772warnings_filters_mutated(PyObject *self, PyObject *args)
773{
774 _filters_version++;
775 Py_RETURN_NONE;
776}
777
Christian Heimes33fe8092008-04-13 13:53:33 +0000778
779/* Function to issue a warning message; may raise an exception. */
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000780
781static int
782warn_unicode(PyObject *category, PyObject *message,
783 Py_ssize_t stack_level)
Christian Heimes33fe8092008-04-13 13:53:33 +0000784{
785 PyObject *res;
Christian Heimes33fe8092008-04-13 13:53:33 +0000786
787 if (category == NULL)
788 category = PyExc_RuntimeWarning;
789
790 res = do_warn(message, category, stack_level);
Christian Heimes33fe8092008-04-13 13:53:33 +0000791 if (res == NULL)
792 return -1;
793 Py_DECREF(res);
794
795 return 0;
796}
797
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000798int
799PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
800 const char *format, ...)
801{
802 int ret;
803 PyObject *message;
804 va_list vargs;
805
806#ifdef HAVE_STDARG_PROTOTYPES
807 va_start(vargs, format);
808#else
809 va_start(vargs);
810#endif
811 message = PyUnicode_FromFormatV(format, vargs);
812 if (message != NULL) {
813 ret = warn_unicode(category, message, stack_level);
814 Py_DECREF(message);
815 }
816 else
817 ret = -1;
818 va_end(vargs);
819 return ret;
820}
821
822int
823PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level)
824{
825 int ret;
826 PyObject *message = PyUnicode_FromString(text);
827 if (message == NULL)
828 return -1;
829 ret = warn_unicode(category, message, stack_level);
830 Py_DECREF(message);
831 return ret;
832}
833
Ezio Melotti42da6632011-03-15 05:18:48 +0200834/* PyErr_Warn is only for backwards compatibility and will be removed.
Christian Heimes33fe8092008-04-13 13:53:33 +0000835 Use PyErr_WarnEx instead. */
836
837#undef PyErr_Warn
838
839PyAPI_FUNC(int)
840PyErr_Warn(PyObject *category, char *text)
841{
842 return PyErr_WarnEx(category, text, 1);
843}
844
845/* Warning with explicit origin */
846int
Victor Stinner14e461d2013-08-26 22:28:21 +0200847PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
848 PyObject *filename, int lineno,
849 PyObject *module, PyObject *registry)
850{
851 PyObject *res;
852 if (category == NULL)
853 category = PyExc_RuntimeWarning;
854 res = warn_explicit(category, message, filename, lineno,
855 module, registry, NULL);
856 if (res == NULL)
857 return -1;
858 Py_DECREF(res);
859 return 0;
860}
861
862int
Christian Heimes33fe8092008-04-13 13:53:33 +0000863PyErr_WarnExplicit(PyObject *category, const char *text,
864 const char *filename_str, int lineno,
865 const char *module_str, PyObject *registry)
866{
Christian Heimes33fe8092008-04-13 13:53:33 +0000867 PyObject *message = PyUnicode_FromString(text);
Victor Stinnercb428f02010-12-27 20:10:36 +0000868 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
Christian Heimes33fe8092008-04-13 13:53:33 +0000869 PyObject *module = NULL;
870 int ret = -1;
871
872 if (message == NULL || filename == NULL)
873 goto exit;
874 if (module_str != NULL) {
875 module = PyUnicode_FromString(module_str);
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200876 if (module == NULL)
877 goto exit;
Christian Heimes33fe8092008-04-13 13:53:33 +0000878 }
879
Victor Stinner14e461d2013-08-26 22:28:21 +0200880 ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
881 module, registry);
Christian Heimes33fe8092008-04-13 13:53:33 +0000882
883 exit:
884 Py_XDECREF(message);
885 Py_XDECREF(module);
886 Py_XDECREF(filename);
887 return ret;
888}
889
Antoine Pitrou070cb3c2013-05-08 13:23:25 +0200890int
891PyErr_WarnExplicitFormat(PyObject *category,
892 const char *filename_str, int lineno,
893 const char *module_str, PyObject *registry,
894 const char *format, ...)
895{
896 PyObject *message;
897 PyObject *module = NULL;
898 PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
899 int ret = -1;
900 va_list vargs;
901
902 if (filename == NULL)
903 goto exit;
904 if (module_str != NULL) {
905 module = PyUnicode_FromString(module_str);
906 if (module == NULL)
907 goto exit;
908 }
909
910#ifdef HAVE_STDARG_PROTOTYPES
911 va_start(vargs, format);
912#else
913 va_start(vargs);
914#endif
915 message = PyUnicode_FromFormatV(format, vargs);
916 if (message != NULL) {
917 PyObject *res;
918 res = warn_explicit(category, message, filename, lineno,
919 module, registry, NULL);
920 Py_DECREF(message);
921 if (res != NULL) {
922 Py_DECREF(res);
923 ret = 0;
924 }
925 }
926 va_end(vargs);
927exit:
928 Py_XDECREF(module);
929 Py_XDECREF(filename);
930 return ret;
931}
932
Christian Heimes33fe8092008-04-13 13:53:33 +0000933
934PyDoc_STRVAR(warn_doc,
935"Issue a warning, or maybe ignore it or raise an exception.");
936
937PyDoc_STRVAR(warn_explicit_doc,
938"Low-level inferface to warnings functionality.");
939
940static PyMethodDef warnings_functions[] = {
941 {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS,
942 warn_doc},
943 {"warn_explicit", (PyCFunction)warnings_warn_explicit,
944 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc},
Antoine Pitroucb0a0062014-09-18 02:40:46 +0200945 {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS,
946 NULL},
Christian Heimes1a8501c2008-10-02 19:56:01 +0000947 /* XXX(brett.cannon): add showwarning? */
948 /* XXX(brett.cannon): Reasonable to add formatwarning? */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 {NULL, NULL} /* sentinel */
Christian Heimes33fe8092008-04-13 13:53:33 +0000950};
951
952
953static PyObject *
954create_filter(PyObject *category, const char *action)
955{
956 static PyObject *ignore_str = NULL;
957 static PyObject *error_str = NULL;
958 static PyObject *default_str = NULL;
Georg Brandl08be72d2010-10-24 15:11:22 +0000959 static PyObject *always_str = NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +0000960 PyObject *action_obj = NULL;
961 PyObject *lineno, *result;
962
963 if (!strcmp(action, "ignore")) {
964 if (ignore_str == NULL) {
965 ignore_str = PyUnicode_InternFromString("ignore");
966 if (ignore_str == NULL)
967 return NULL;
968 }
969 action_obj = ignore_str;
970 }
971 else if (!strcmp(action, "error")) {
972 if (error_str == NULL) {
973 error_str = PyUnicode_InternFromString("error");
974 if (error_str == NULL)
975 return NULL;
976 }
977 action_obj = error_str;
978 }
979 else if (!strcmp(action, "default")) {
980 if (default_str == NULL) {
981 default_str = PyUnicode_InternFromString("default");
982 if (default_str == NULL)
983 return NULL;
984 }
985 action_obj = default_str;
986 }
Georg Brandl08be72d2010-10-24 15:11:22 +0000987 else if (!strcmp(action, "always")) {
988 if (always_str == NULL) {
989 always_str = PyUnicode_InternFromString("always");
990 if (always_str == NULL)
991 return NULL;
992 }
993 action_obj = always_str;
994 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000995 else {
996 Py_FatalError("unknown action");
997 }
998
999 /* This assumes the line number is zero for now. */
1000 lineno = PyLong_FromLong(0);
1001 if (lineno == NULL)
1002 return NULL;
1003 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno);
1004 Py_DECREF(lineno);
1005 return result;
1006}
1007
1008static PyObject *
1009init_filters(void)
1010{
Georg Brandl08be72d2010-10-24 15:11:22 +00001011 PyObject *filters = PyList_New(5);
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001012 unsigned int pos = 0; /* Post-incremented in each use. */
1013 unsigned int x;
Georg Brandl08be72d2010-10-24 15:11:22 +00001014 const char *bytes_action, *resource_action;
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001015
Christian Heimes33fe8092008-04-13 13:53:33 +00001016 if (filters == NULL)
1017 return NULL;
1018
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001019 PyList_SET_ITEM(filters, pos++,
1020 create_filter(PyExc_DeprecationWarning, "ignore"));
1021 PyList_SET_ITEM(filters, pos++,
Christian Heimes33fe8092008-04-13 13:53:33 +00001022 create_filter(PyExc_PendingDeprecationWarning, "ignore"));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001023 PyList_SET_ITEM(filters, pos++,
1024 create_filter(PyExc_ImportWarning, "ignore"));
Christian Heimes33fe8092008-04-13 13:53:33 +00001025 if (Py_BytesWarningFlag > 1)
1026 bytes_action = "error";
1027 else if (Py_BytesWarningFlag)
1028 bytes_action = "default";
1029 else
1030 bytes_action = "ignore";
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001031 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning,
Christian Heimes33fe8092008-04-13 13:53:33 +00001032 bytes_action));
Georg Brandl08be72d2010-10-24 15:11:22 +00001033 /* resource usage warnings are enabled by default in pydebug mode */
1034#ifdef Py_DEBUG
1035 resource_action = "always";
1036#else
1037 resource_action = "ignore";
1038#endif
1039 PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning,
1040 resource_action));
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +00001041 for (x = 0; x < pos; x += 1) {
1042 if (PyList_GET_ITEM(filters, x) == NULL) {
1043 Py_DECREF(filters);
1044 return NULL;
1045 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001046 }
1047
1048 return filters;
1049}
1050
Martin v. Löwis1a214512008-06-11 05:26:20 +00001051static struct PyModuleDef warningsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 PyModuleDef_HEAD_INIT,
1053 MODULE_NAME,
1054 warnings__doc__,
1055 0,
1056 warnings_functions,
1057 NULL,
1058 NULL,
1059 NULL,
1060 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001061};
1062
Christian Heimes33fe8092008-04-13 13:53:33 +00001063
1064PyMODINIT_FUNC
1065_PyWarnings_Init(void)
1066{
Brett Cannon0759dd62009-04-01 18:13:07 +00001067 PyObject *m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001068
Martin v. Löwis1a214512008-06-11 05:26:20 +00001069 m = PyModule_Create(&warningsmodule);
Christian Heimes33fe8092008-04-13 13:53:33 +00001070 if (m == NULL)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001071 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001072
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001073 if (_filters == NULL) {
1074 _filters = init_filters();
1075 if (_filters == NULL)
1076 return NULL;
1077 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001078 Py_INCREF(_filters);
1079 if (PyModule_AddObject(m, "filters", _filters) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001080 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001081
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001082 if (_once_registry == NULL) {
1083 _once_registry = PyDict_New();
1084 if (_once_registry == NULL)
1085 return NULL;
1086 }
Christian Heimes33fe8092008-04-13 13:53:33 +00001087 Py_INCREF(_once_registry);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001088 if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001089 return NULL;
Christian Heimes33fe8092008-04-13 13:53:33 +00001090
Antoine Pitrouaa5c5c62012-01-18 21:45:15 +01001091 if (_default_action == NULL) {
1092 _default_action = PyUnicode_FromString("default");
1093 if (_default_action == NULL)
1094 return NULL;
1095 }
1096 Py_INCREF(_default_action);
Brett Cannonef0e6c32010-09-04 18:24:04 +00001097 if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +00001098 return NULL;
Antoine Pitroucb0a0062014-09-18 02:40:46 +02001099
1100 _filters_version = 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +00001101 return m;
Christian Heimes33fe8092008-04-13 13:53:33 +00001102}