Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "frameobject.h" |
| 3 | |
| 4 | #define MODULE_NAME "_warnings" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 5 | |
| 6 | PyDoc_STRVAR(warnings__doc__, |
| 7 | MODULE_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. */ |
| 12 | static PyObject *_filters; /* List */ |
| 13 | static PyObject *_once_registry; /* Dict */ |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 14 | static PyObject *_default_action; /* String */ |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 15 | static long _filters_version; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 16 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 17 | _Py_IDENTIFIER(argv); |
| 18 | _Py_IDENTIFIER(stderr); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 19 | |
| 20 | static int |
| 21 | check_matched(PyObject *obj, PyObject *arg) |
| 22 | { |
| 23 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 24 | _Py_IDENTIFIER(match); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 25 | int rc; |
| 26 | |
| 27 | if (obj == Py_None) |
| 28 | return 1; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 29 | result = _PyObject_CallMethodId(obj, &PyId_match, "O", arg); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 30 | 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 | */ |
| 42 | static PyObject * |
| 43 | get_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 Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 68 | static PyObject * |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 69 | get_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 Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 85 | static PyObject * |
| 86 | get_default_action(void) |
| 87 | { |
| 88 | PyObject *default_action; |
| 89 | |
| 90 | default_action = get_warnings_attr("defaultaction"); |
| 91 | if (default_action == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | if (PyErr_Occurred()) { |
| 93 | return NULL; |
| 94 | } |
| 95 | return _default_action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Py_DECREF(_default_action); |
| 99 | _default_action = default_action; |
| 100 | return default_action; |
| 101 | } |
| 102 | |
| 103 | |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 104 | /* The item is a new reference. */ |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 105 | static PyObject* |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 106 | get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, |
| 107 | PyObject *module, PyObject **item) |
| 108 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 109 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 110 | 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 Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 123 | if (_filters == NULL || !PyList_Check(_filters)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 124 | 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 Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 135 | tmp_item = PyList_GET_ITEM(_filters, i); |
| 136 | if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 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 */ |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 143 | Py_INCREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 144 | 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 Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 151 | if (good_msg == -1) { |
| 152 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 153 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 154 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 155 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 156 | good_mod = check_matched(mod, module); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 157 | if (good_mod == -1) { |
| 158 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 159 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 160 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 161 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 162 | is_subclass = PyObject_IsSubclass(category, cat); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 163 | if (is_subclass == -1) { |
| 164 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 165 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 166 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 167 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 168 | ln = PyLong_AsSsize_t(ln_obj); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 169 | if (ln == -1 && PyErr_Occurred()) { |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 170 | Py_DECREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 171 | return NULL; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 172 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 173 | |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 174 | if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) { |
| 175 | *item = tmp_item; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 176 | return action; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | Py_DECREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 182 | action = get_default_action(); |
| 183 | if (action != NULL) { |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 184 | Py_INCREF(Py_None); |
| 185 | *item = Py_None; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 186 | return action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 187 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 188 | |
| 189 | PyErr_SetString(PyExc_ValueError, |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 190 | MODULE_NAME ".defaultaction not found"); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
| 193 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 194 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 195 | static int |
| 196 | already_warned(PyObject *registry, PyObject *key, int should_set) |
| 197 | { |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 198 | PyObject *version_obj, *already_warned; |
| 199 | _Py_IDENTIFIER(version); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 200 | |
| 201 | if (key == NULL) |
| 202 | return -1; |
| 203 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 204 | 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 Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 225 | } |
| 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. */ |
| 234 | static PyObject * |
| 235 | normalize_module(PyObject *filename) |
| 236 | { |
| 237 | PyObject *module; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 238 | int kind; |
| 239 | void *data; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 240 | Py_ssize_t len; |
| 241 | |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 242 | len = PyUnicode_GetLength(filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 243 | if (len < 0) |
| 244 | return NULL; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 245 | |
| 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 Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 253 | if (len >= 3 && |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 254 | PyUnicode_READ(kind, data, len-3) == '.' && |
| 255 | PyUnicode_READ(kind, data, len-2) == 'p' && |
| 256 | PyUnicode_READ(kind, data, len-1) == 'y') |
| 257 | { |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 258 | module = PyUnicode_Substring(filename, 0, len-3); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 259 | } |
| 260 | else { |
| 261 | module = filename; |
| 262 | Py_INCREF(module); |
| 263 | } |
| 264 | return module; |
| 265 | } |
| 266 | |
| 267 | static int |
| 268 | update_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 | |
| 289 | static void |
| 290 | show_warning(PyObject *filename, int lineno, PyObject *text, PyObject |
| 291 | *category, PyObject *sourceline) |
| 292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | PyObject *f_stderr; |
| 294 | PyObject *name; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 295 | char lineno_str[128]; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 296 | _Py_IDENTIFIER(__name__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 297 | |
| 298 | PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); |
| 299 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 300 | name = _PyObject_GetAttrId(category, &PyId___name__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 301 | if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 302 | goto error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 303 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 304 | f_stderr = _PySys_GetObjectId(&PyId_stderr); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 305 | if (f_stderr == NULL) { |
| 306 | fprintf(stderr, "lost sys.stderr\n"); |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 307 | goto error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* Print "filename:lineno: category: text\n" */ |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 311 | 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 Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 324 | |
| 325 | /* Print " source_line\n" */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 326 | if (sourceline) { |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 327 | int kind; |
| 328 | void *data; |
| 329 | Py_ssize_t i, len; |
| 330 | Py_UCS4 ch; |
| 331 | PyObject *truncated; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 332 | |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 333 | 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 Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 351 | PyFile_WriteString("\n", f_stderr); |
| 352 | } |
Victor Stinner | 78e2c98 | 2013-07-16 01:54:37 +0200 | [diff] [blame] | 353 | else { |
| 354 | _Py_DisplaySourceLine(f_stderr, filename, lineno, 2); |
| 355 | } |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 356 | |
| 357 | error: |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 358 | Py_XDECREF(name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 359 | PyErr_Clear(); |
| 360 | } |
| 361 | |
| 362 | static PyObject * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | warn_explicit(PyObject *category, PyObject *message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 364 | PyObject *filename, int lineno, |
| 365 | PyObject *module, PyObject *registry, PyObject *sourceline) |
| 366 | { |
| 367 | PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 368 | PyObject *item = NULL; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 369 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 370 | int rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | |
Victor Stinner | dcdd05b | 2013-11-01 00:55:30 +0100 | [diff] [blame] | 372 | /* 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 Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 379 | if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
| 380 | PyErr_SetString(PyExc_TypeError, "'registry' must be a dict"); |
| 381 | return NULL; |
| 382 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 383 | |
| 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 Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 401 | if (text == NULL) |
| 402 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 403 | category = (PyObject*)message->ob_type; |
| 404 | } |
| 405 | else { |
| 406 | text = message; |
| 407 | message = PyObject_CallFunction(category, "O", message); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 408 | if (message == NULL) |
| 409 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 410 | } |
| 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 Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 421 | if ((registry != NULL) && (registry != Py_None)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 422 | rc = already_warned(registry, key, 0); |
| 423 | if (rc == -1) |
| 424 | goto cleanup; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | else if (rc == 1) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 426 | 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 Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 434 | if (PyUnicode_CompareWithASCIIString(action, "error") == 0) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 435 | 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 Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 442 | if (PyUnicode_CompareWithASCIIString(action, "always") != 0) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 443 | if (registry != NULL && registry != Py_None && |
| 444 | PyDict_SetItem(registry, key, Py_True) < 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 445 | goto cleanup; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 446 | else if (PyUnicode_CompareWithASCIIString(action, "ignore") == 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 447 | goto return_none; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 448 | else if (PyUnicode_CompareWithASCIIString(action, "once") == 0) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 449 | if (registry == NULL || registry == Py_None) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 450 | registry = get_once_registry(); |
| 451 | if (registry == NULL) |
| 452 | goto cleanup; |
| 453 | } |
| 454 | /* _once_registry[(text, category)] = 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 456 | } |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 457 | else if (PyUnicode_CompareWithASCIIString(action, "module") == 0) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 458 | /* registry[(text, category, 0)] = 1 */ |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 459 | if (registry != NULL && registry != Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 461 | } |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 462 | else if (PyUnicode_CompareWithASCIIString(action, "default") != 0) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 463 | PyErr_Format(PyExc_RuntimeError, |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 464 | "Unrecognized action (%R) in warnings.filters:\n %R", |
| 465 | action, item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 466 | goto cleanup; |
| 467 | } |
| 468 | } |
| 469 | |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 470 | if (rc == 1) /* Already warned for this module. */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 471 | 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 Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 480 | PyObject *res; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 481 | |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 482 | if (!PyCallable_Check(show_fxn)) { |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 483 | PyErr_SetString(PyExc_TypeError, |
| 484 | "warnings.showwarning() must be set to a " |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 485 | "callable"); |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 486 | Py_DECREF(show_fxn); |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 487 | goto cleanup; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 488 | } |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 489 | |
| 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 Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 497 | } |
| 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 Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 507 | Py_XDECREF(item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 508 | Py_XDECREF(key); |
| 509 | Py_XDECREF(text); |
| 510 | Py_XDECREF(lineno_obj); |
| 511 | Py_DECREF(module); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 512 | Py_XDECREF(message); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 513 | return result; /* Py_None or NULL. */ |
| 514 | } |
| 515 | |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 516 | static int |
| 517 | is_internal_frame(PyFrameObject *frame) |
| 518 | { |
| 519 | static PyObject *importlib_string = NULL; |
| 520 | static PyObject *bootstrap_string = NULL; |
| 521 | PyObject *filename; |
| 522 | int contains; |
| 523 | |
| 524 | if (importlib_string == NULL) { |
| 525 | importlib_string = PyUnicode_FromString("importlib"); |
| 526 | if (importlib_string == NULL) { |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | bootstrap_string = PyUnicode_FromString("_bootstrap"); |
| 531 | if (bootstrap_string == NULL) { |
| 532 | Py_DECREF(importlib_string); |
| 533 | return 0; |
| 534 | } |
| 535 | Py_INCREF(importlib_string); |
| 536 | Py_INCREF(bootstrap_string); |
| 537 | } |
| 538 | |
| 539 | if (frame == NULL || frame->f_code == NULL || |
| 540 | frame->f_code->co_filename == NULL) { |
| 541 | return 0; |
| 542 | } |
| 543 | filename = frame->f_code->co_filename; |
| 544 | if (!PyUnicode_Check(filename)) { |
| 545 | return 0; |
| 546 | } |
| 547 | contains = PyUnicode_Contains(filename, importlib_string); |
| 548 | if (contains < 0) { |
| 549 | return 0; |
| 550 | } |
| 551 | else if (contains > 0) { |
| 552 | contains = PyUnicode_Contains(filename, bootstrap_string); |
| 553 | if (contains < 0) { |
| 554 | return 0; |
| 555 | } |
| 556 | else if (contains > 0) { |
| 557 | return 1; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static PyFrameObject * |
| 565 | next_external_frame(PyFrameObject *frame) |
| 566 | { |
| 567 | do { |
| 568 | frame = frame->f_back; |
| 569 | } while (frame != NULL && is_internal_frame(frame)); |
| 570 | |
| 571 | return frame; |
| 572 | } |
| 573 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 574 | /* filename, module, and registry are new refs, globals is borrowed */ |
| 575 | /* Returns 0 on error (no new refs), 1 on success */ |
| 576 | static int |
| 577 | setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, |
| 578 | PyObject **module, PyObject **registry) |
| 579 | { |
| 580 | PyObject *globals; |
| 581 | |
| 582 | /* Setup globals and lineno. */ |
| 583 | PyFrameObject *f = PyThreadState_GET()->frame; |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 584 | // Stack level comparisons to Python code is off by one as there is no |
| 585 | // warnings-related stack level to avoid. |
| 586 | if (stack_level <= 0 || is_internal_frame(f)) { |
| 587 | while (--stack_level > 0 && f != NULL) { |
| 588 | f = f->f_back; |
| 589 | } |
| 590 | } |
| 591 | else { |
| 592 | while (--stack_level > 0 && f != NULL) { |
| 593 | f = next_external_frame(f); |
| 594 | } |
| 595 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 596 | |
| 597 | if (f == NULL) { |
| 598 | globals = PyThreadState_Get()->interp->sysdict; |
| 599 | *lineno = 1; |
| 600 | } |
| 601 | else { |
| 602 | globals = f->f_globals; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 603 | *lineno = PyFrame_GetLineNumber(f); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | *module = NULL; |
| 607 | |
| 608 | /* Setup registry. */ |
| 609 | assert(globals != NULL); |
| 610 | assert(PyDict_Check(globals)); |
| 611 | *registry = PyDict_GetItemString(globals, "__warningregistry__"); |
| 612 | if (*registry == NULL) { |
| 613 | int rc; |
| 614 | |
| 615 | *registry = PyDict_New(); |
| 616 | if (*registry == NULL) |
| 617 | return 0; |
| 618 | |
| 619 | rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); |
| 620 | if (rc < 0) |
| 621 | goto handle_error; |
| 622 | } |
| 623 | else |
| 624 | Py_INCREF(*registry); |
| 625 | |
| 626 | /* Setup module. */ |
| 627 | *module = PyDict_GetItemString(globals, "__name__"); |
| 628 | if (*module == NULL) { |
| 629 | *module = PyUnicode_FromString("<string>"); |
| 630 | if (*module == NULL) |
| 631 | goto handle_error; |
| 632 | } |
| 633 | else |
| 634 | Py_INCREF(*module); |
| 635 | |
| 636 | /* Setup filename. */ |
| 637 | *filename = PyDict_GetItemString(globals, "__file__"); |
Victor Stinner | 8b0508e | 2011-07-04 02:43:09 +0200 | [diff] [blame] | 638 | if (*filename != NULL && PyUnicode_Check(*filename)) { |
Victor Stinner | b62a7b2 | 2011-10-06 02:34:51 +0200 | [diff] [blame] | 639 | Py_ssize_t len; |
| 640 | int kind; |
| 641 | void *data; |
| 642 | |
| 643 | if (PyUnicode_READY(*filename)) |
| 644 | goto handle_error; |
| 645 | |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 646 | len = PyUnicode_GetLength(*filename); |
Victor Stinner | b62a7b2 | 2011-10-06 02:34:51 +0200 | [diff] [blame] | 647 | kind = PyUnicode_KIND(*filename); |
| 648 | data = PyUnicode_DATA(*filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 649 | |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 650 | #define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 651 | /* if filename.lower().endswith(".pyc"): */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 652 | if (len >= 4 && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 653 | PyUnicode_READ(kind, data, len-4) == '.' && |
Benjamin Peterson | 21e0da2 | 2012-01-11 21:00:42 -0500 | [diff] [blame] | 654 | ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' && |
| 655 | ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' && |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 656 | ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 657 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 658 | *filename = PyUnicode_Substring(*filename, 0, |
| 659 | PyUnicode_GET_LENGTH(*filename)-1); |
Victor Stinner | 2e5f117 | 2010-08-08 22:12:45 +0000 | [diff] [blame] | 660 | if (*filename == NULL) |
| 661 | goto handle_error; |
| 662 | } |
| 663 | else |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 664 | Py_INCREF(*filename); |
| 665 | } |
| 666 | else { |
Benjamin Peterson | bb4a747 | 2011-07-04 22:27:16 -0500 | [diff] [blame] | 667 | *filename = NULL; |
Victor Stinner | 856f45f | 2013-10-30 00:04:59 +0100 | [diff] [blame] | 668 | if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) { |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 669 | PyObject *argv = _PySys_GetObjectId(&PyId_argv); |
Victor Stinner | ce5f4fb | 2013-10-28 18:47:22 +0100 | [diff] [blame] | 670 | /* PyList_Check() is needed because sys.argv is set to None during |
| 671 | Python finalization */ |
| 672 | if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 673 | int is_true; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 674 | *filename = PyList_GetItem(argv, 0); |
| 675 | Py_INCREF(*filename); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 676 | /* If sys.argv[0] is false, then use '__main__'. */ |
| 677 | is_true = PyObject_IsTrue(*filename); |
| 678 | if (is_true < 0) { |
| 679 | Py_DECREF(*filename); |
| 680 | goto handle_error; |
| 681 | } |
| 682 | else if (!is_true) { |
Serhiy Storchaka | 5a57ade | 2015-12-24 10:35:59 +0200 | [diff] [blame] | 683 | Py_SETREF(*filename, PyUnicode_FromString("__main__")); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 684 | if (*filename == NULL) |
| 685 | goto handle_error; |
| 686 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 687 | } |
| 688 | else { |
| 689 | /* embedded interpreters don't have sys.argv, see bug #839151 */ |
| 690 | *filename = PyUnicode_FromString("__main__"); |
Victor Stinner | 856f45f | 2013-10-30 00:04:59 +0100 | [diff] [blame] | 691 | if (*filename == NULL) |
| 692 | goto handle_error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | if (*filename == NULL) { |
| 696 | *filename = *module; |
| 697 | Py_INCREF(*filename); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return 1; |
| 702 | |
| 703 | handle_error: |
| 704 | /* filename not XDECREF'ed here as there is no way to jump here with a |
| 705 | dangling reference. */ |
| 706 | Py_XDECREF(*registry); |
| 707 | Py_XDECREF(*module); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static PyObject * |
| 712 | get_category(PyObject *message, PyObject *category) |
| 713 | { |
| 714 | int rc; |
| 715 | |
| 716 | /* Get category. */ |
| 717 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 718 | if (rc == -1) |
| 719 | return NULL; |
| 720 | |
| 721 | if (rc == 1) |
| 722 | category = (PyObject*)message->ob_type; |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 723 | else if (category == NULL || category == Py_None) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 724 | category = PyExc_UserWarning; |
| 725 | |
| 726 | /* Validate category. */ |
| 727 | rc = PyObject_IsSubclass(category, PyExc_Warning); |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 728 | /* category is not a subclass of PyExc_Warning or |
| 729 | PyObject_IsSubclass raised an error */ |
| 730 | if (rc == -1 || rc == 0) { |
| 731 | PyErr_Format(PyExc_TypeError, |
| 732 | "category must be a Warning subclass, not '%s'", |
| 733 | Py_TYPE(category)->tp_name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | return category; |
| 738 | } |
| 739 | |
| 740 | static PyObject * |
| 741 | do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level) |
| 742 | { |
| 743 | PyObject *filename, *module, *registry, *res; |
| 744 | int lineno; |
| 745 | |
| 746 | if (!setup_context(stack_level, &filename, &lineno, &module, ®istry)) |
| 747 | return NULL; |
| 748 | |
Victor Stinner | dcdd05b | 2013-11-01 00:55:30 +0100 | [diff] [blame] | 749 | res = warn_explicit(category, message, filename, lineno, module, registry, |
| 750 | NULL); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 751 | Py_DECREF(filename); |
| 752 | Py_DECREF(registry); |
| 753 | Py_DECREF(module); |
| 754 | return res; |
| 755 | } |
| 756 | |
| 757 | static PyObject * |
| 758 | warnings_warn(PyObject *self, PyObject *args, PyObject *kwds) |
| 759 | { |
| 760 | static char *kw_list[] = { "message", "category", "stacklevel", 0 }; |
| 761 | PyObject *message, *category = NULL; |
| 762 | Py_ssize_t stack_level = 1; |
| 763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 765 | &message, &category, &stack_level)) |
| 766 | return NULL; |
| 767 | |
| 768 | category = get_category(message, category); |
| 769 | if (category == NULL) |
| 770 | return NULL; |
| 771 | return do_warn(message, category, stack_level); |
| 772 | } |
| 773 | |
| 774 | static PyObject * |
| 775 | warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) |
| 776 | { |
| 777 | static char *kwd_list[] = {"message", "category", "filename", "lineno", |
| 778 | "module", "registry", "module_globals", 0}; |
| 779 | PyObject *message; |
| 780 | PyObject *category; |
| 781 | PyObject *filename; |
| 782 | int lineno; |
| 783 | PyObject *module = NULL; |
| 784 | PyObject *registry = NULL; |
| 785 | PyObject *module_globals = NULL; |
| 786 | |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 787 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOO:warn_explicit", |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 788 | kwd_list, &message, &category, &filename, &lineno, &module, |
| 789 | ®istry, &module_globals)) |
| 790 | return NULL; |
| 791 | |
| 792 | if (module_globals) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 793 | _Py_IDENTIFIER(get_source); |
| 794 | _Py_IDENTIFIER(splitlines); |
| 795 | PyObject *tmp; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 796 | PyObject *loader; |
| 797 | PyObject *module_name; |
| 798 | PyObject *source; |
| 799 | PyObject *source_list; |
| 800 | PyObject *source_line; |
| 801 | PyObject *returned; |
| 802 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 803 | if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL) |
| 804 | return NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 805 | if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL) |
| 806 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 807 | |
| 808 | /* Check/get the requisite pieces needed for the loader. */ |
| 809 | loader = PyDict_GetItemString(module_globals, "__loader__"); |
| 810 | module_name = PyDict_GetItemString(module_globals, "__name__"); |
| 811 | |
| 812 | if (loader == NULL || module_name == NULL) |
| 813 | goto standard_call; |
| 814 | |
| 815 | /* Make sure the loader implements the optional get_source() method. */ |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 816 | if (!_PyObject_HasAttrId(loader, &PyId_get_source)) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 817 | goto standard_call; |
| 818 | /* Call get_source() to get the source code. */ |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 819 | source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object, |
| 820 | module_name, NULL); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 821 | if (!source) |
| 822 | return NULL; |
| 823 | else if (source == Py_None) { |
| 824 | Py_DECREF(Py_None); |
| 825 | goto standard_call; |
| 826 | } |
| 827 | |
| 828 | /* Split the source into lines. */ |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 829 | source_list = PyObject_CallMethodObjArgs(source, |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 830 | PyId_splitlines.object, |
| 831 | NULL); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 832 | Py_DECREF(source); |
| 833 | if (!source_list) |
| 834 | return NULL; |
| 835 | |
| 836 | /* Get the source line. */ |
| 837 | source_line = PyList_GetItem(source_list, lineno-1); |
| 838 | if (!source_line) { |
| 839 | Py_DECREF(source_list); |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | /* Handle the warning. */ |
| 844 | returned = warn_explicit(category, message, filename, lineno, module, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 845 | registry, source_line); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 846 | Py_DECREF(source_list); |
| 847 | return returned; |
| 848 | } |
| 849 | |
| 850 | standard_call: |
| 851 | return warn_explicit(category, message, filename, lineno, module, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 852 | registry, NULL); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 855 | static PyObject * |
| 856 | warnings_filters_mutated(PyObject *self, PyObject *args) |
| 857 | { |
| 858 | _filters_version++; |
| 859 | Py_RETURN_NONE; |
| 860 | } |
| 861 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 862 | |
| 863 | /* Function to issue a warning message; may raise an exception. */ |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 864 | |
| 865 | static int |
| 866 | warn_unicode(PyObject *category, PyObject *message, |
| 867 | Py_ssize_t stack_level) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 868 | { |
| 869 | PyObject *res; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 870 | |
| 871 | if (category == NULL) |
| 872 | category = PyExc_RuntimeWarning; |
| 873 | |
| 874 | res = do_warn(message, category, stack_level); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 875 | if (res == NULL) |
| 876 | return -1; |
| 877 | Py_DECREF(res); |
| 878 | |
| 879 | return 0; |
| 880 | } |
| 881 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 882 | int |
| 883 | PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, |
| 884 | const char *format, ...) |
| 885 | { |
| 886 | int ret; |
| 887 | PyObject *message; |
| 888 | va_list vargs; |
| 889 | |
| 890 | #ifdef HAVE_STDARG_PROTOTYPES |
| 891 | va_start(vargs, format); |
| 892 | #else |
| 893 | va_start(vargs); |
| 894 | #endif |
| 895 | message = PyUnicode_FromFormatV(format, vargs); |
| 896 | if (message != NULL) { |
| 897 | ret = warn_unicode(category, message, stack_level); |
| 898 | Py_DECREF(message); |
| 899 | } |
| 900 | else |
| 901 | ret = -1; |
| 902 | va_end(vargs); |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | int |
| 907 | PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
| 908 | { |
| 909 | int ret; |
| 910 | PyObject *message = PyUnicode_FromString(text); |
| 911 | if (message == NULL) |
| 912 | return -1; |
| 913 | ret = warn_unicode(category, message, stack_level); |
| 914 | Py_DECREF(message); |
| 915 | return ret; |
| 916 | } |
| 917 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 918 | /* PyErr_Warn is only for backwards compatibility and will be removed. |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 919 | Use PyErr_WarnEx instead. */ |
| 920 | |
| 921 | #undef PyErr_Warn |
| 922 | |
| 923 | PyAPI_FUNC(int) |
| 924 | PyErr_Warn(PyObject *category, char *text) |
| 925 | { |
| 926 | return PyErr_WarnEx(category, text, 1); |
| 927 | } |
| 928 | |
| 929 | /* Warning with explicit origin */ |
| 930 | int |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 931 | PyErr_WarnExplicitObject(PyObject *category, PyObject *message, |
| 932 | PyObject *filename, int lineno, |
| 933 | PyObject *module, PyObject *registry) |
| 934 | { |
| 935 | PyObject *res; |
| 936 | if (category == NULL) |
| 937 | category = PyExc_RuntimeWarning; |
| 938 | res = warn_explicit(category, message, filename, lineno, |
| 939 | module, registry, NULL); |
| 940 | if (res == NULL) |
| 941 | return -1; |
| 942 | Py_DECREF(res); |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | int |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 947 | PyErr_WarnExplicit(PyObject *category, const char *text, |
| 948 | const char *filename_str, int lineno, |
| 949 | const char *module_str, PyObject *registry) |
| 950 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 951 | PyObject *message = PyUnicode_FromString(text); |
Victor Stinner | cb428f0 | 2010-12-27 20:10:36 +0000 | [diff] [blame] | 952 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 953 | PyObject *module = NULL; |
| 954 | int ret = -1; |
| 955 | |
| 956 | if (message == NULL || filename == NULL) |
| 957 | goto exit; |
| 958 | if (module_str != NULL) { |
| 959 | module = PyUnicode_FromString(module_str); |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 960 | if (module == NULL) |
| 961 | goto exit; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 964 | ret = PyErr_WarnExplicitObject(category, message, filename, lineno, |
| 965 | module, registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 966 | |
| 967 | exit: |
| 968 | Py_XDECREF(message); |
| 969 | Py_XDECREF(module); |
| 970 | Py_XDECREF(filename); |
| 971 | return ret; |
| 972 | } |
| 973 | |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 974 | int |
| 975 | PyErr_WarnExplicitFormat(PyObject *category, |
| 976 | const char *filename_str, int lineno, |
| 977 | const char *module_str, PyObject *registry, |
| 978 | const char *format, ...) |
| 979 | { |
| 980 | PyObject *message; |
| 981 | PyObject *module = NULL; |
| 982 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
| 983 | int ret = -1; |
| 984 | va_list vargs; |
| 985 | |
| 986 | if (filename == NULL) |
| 987 | goto exit; |
| 988 | if (module_str != NULL) { |
| 989 | module = PyUnicode_FromString(module_str); |
| 990 | if (module == NULL) |
| 991 | goto exit; |
| 992 | } |
| 993 | |
| 994 | #ifdef HAVE_STDARG_PROTOTYPES |
| 995 | va_start(vargs, format); |
| 996 | #else |
| 997 | va_start(vargs); |
| 998 | #endif |
| 999 | message = PyUnicode_FromFormatV(format, vargs); |
| 1000 | if (message != NULL) { |
| 1001 | PyObject *res; |
| 1002 | res = warn_explicit(category, message, filename, lineno, |
| 1003 | module, registry, NULL); |
| 1004 | Py_DECREF(message); |
| 1005 | if (res != NULL) { |
| 1006 | Py_DECREF(res); |
| 1007 | ret = 0; |
| 1008 | } |
| 1009 | } |
| 1010 | va_end(vargs); |
| 1011 | exit: |
| 1012 | Py_XDECREF(module); |
| 1013 | Py_XDECREF(filename); |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1017 | |
| 1018 | PyDoc_STRVAR(warn_doc, |
| 1019 | "Issue a warning, or maybe ignore it or raise an exception."); |
| 1020 | |
| 1021 | PyDoc_STRVAR(warn_explicit_doc, |
| 1022 | "Low-level inferface to warnings functionality."); |
| 1023 | |
| 1024 | static PyMethodDef warnings_functions[] = { |
| 1025 | {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS, |
| 1026 | warn_doc}, |
| 1027 | {"warn_explicit", (PyCFunction)warnings_warn_explicit, |
| 1028 | METH_VARARGS | METH_KEYWORDS, warn_explicit_doc}, |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1029 | {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS, |
| 1030 | NULL}, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 1031 | /* XXX(brett.cannon): add showwarning? */ |
| 1032 | /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | {NULL, NULL} /* sentinel */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1034 | }; |
| 1035 | |
| 1036 | |
| 1037 | static PyObject * |
| 1038 | create_filter(PyObject *category, const char *action) |
| 1039 | { |
| 1040 | static PyObject *ignore_str = NULL; |
| 1041 | static PyObject *error_str = NULL; |
| 1042 | static PyObject *default_str = NULL; |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1043 | static PyObject *always_str = NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1044 | PyObject *action_obj = NULL; |
| 1045 | PyObject *lineno, *result; |
| 1046 | |
| 1047 | if (!strcmp(action, "ignore")) { |
| 1048 | if (ignore_str == NULL) { |
| 1049 | ignore_str = PyUnicode_InternFromString("ignore"); |
| 1050 | if (ignore_str == NULL) |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | action_obj = ignore_str; |
| 1054 | } |
| 1055 | else if (!strcmp(action, "error")) { |
| 1056 | if (error_str == NULL) { |
| 1057 | error_str = PyUnicode_InternFromString("error"); |
| 1058 | if (error_str == NULL) |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | action_obj = error_str; |
| 1062 | } |
| 1063 | else if (!strcmp(action, "default")) { |
| 1064 | if (default_str == NULL) { |
| 1065 | default_str = PyUnicode_InternFromString("default"); |
| 1066 | if (default_str == NULL) |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | action_obj = default_str; |
| 1070 | } |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1071 | else if (!strcmp(action, "always")) { |
| 1072 | if (always_str == NULL) { |
| 1073 | always_str = PyUnicode_InternFromString("always"); |
| 1074 | if (always_str == NULL) |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | action_obj = always_str; |
| 1078 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1079 | else { |
| 1080 | Py_FatalError("unknown action"); |
| 1081 | } |
| 1082 | |
| 1083 | /* This assumes the line number is zero for now. */ |
| 1084 | lineno = PyLong_FromLong(0); |
| 1085 | if (lineno == NULL) |
| 1086 | return NULL; |
| 1087 | result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno); |
| 1088 | Py_DECREF(lineno); |
| 1089 | return result; |
| 1090 | } |
| 1091 | |
| 1092 | static PyObject * |
| 1093 | init_filters(void) |
| 1094 | { |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1095 | PyObject *filters = PyList_New(5); |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1096 | unsigned int pos = 0; /* Post-incremented in each use. */ |
| 1097 | unsigned int x; |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1098 | const char *bytes_action, *resource_action; |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1099 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1100 | if (filters == NULL) |
| 1101 | return NULL; |
| 1102 | |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1103 | PyList_SET_ITEM(filters, pos++, |
| 1104 | create_filter(PyExc_DeprecationWarning, "ignore")); |
| 1105 | PyList_SET_ITEM(filters, pos++, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1106 | create_filter(PyExc_PendingDeprecationWarning, "ignore")); |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1107 | PyList_SET_ITEM(filters, pos++, |
| 1108 | create_filter(PyExc_ImportWarning, "ignore")); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1109 | if (Py_BytesWarningFlag > 1) |
| 1110 | bytes_action = "error"; |
| 1111 | else if (Py_BytesWarningFlag) |
| 1112 | bytes_action = "default"; |
| 1113 | else |
| 1114 | bytes_action = "ignore"; |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1115 | PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1116 | bytes_action)); |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 1117 | /* resource usage warnings are enabled by default in pydebug mode */ |
| 1118 | #ifdef Py_DEBUG |
| 1119 | resource_action = "always"; |
| 1120 | #else |
| 1121 | resource_action = "ignore"; |
| 1122 | #endif |
| 1123 | PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning, |
| 1124 | resource_action)); |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 1125 | for (x = 0; x < pos; x += 1) { |
| 1126 | if (PyList_GET_ITEM(filters, x) == NULL) { |
| 1127 | Py_DECREF(filters); |
| 1128 | return NULL; |
| 1129 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | return filters; |
| 1133 | } |
| 1134 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1135 | static struct PyModuleDef warningsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | PyModuleDef_HEAD_INIT, |
| 1137 | MODULE_NAME, |
| 1138 | warnings__doc__, |
| 1139 | 0, |
| 1140 | warnings_functions, |
| 1141 | NULL, |
| 1142 | NULL, |
| 1143 | NULL, |
| 1144 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1145 | }; |
| 1146 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1147 | |
| 1148 | PyMODINIT_FUNC |
| 1149 | _PyWarnings_Init(void) |
| 1150 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 1151 | PyObject *m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1152 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1153 | m = PyModule_Create(&warningsmodule); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1154 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1155 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1156 | |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1157 | if (_filters == NULL) { |
| 1158 | _filters = init_filters(); |
| 1159 | if (_filters == NULL) |
| 1160 | return NULL; |
| 1161 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1162 | Py_INCREF(_filters); |
| 1163 | if (PyModule_AddObject(m, "filters", _filters) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1164 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1166 | if (_once_registry == NULL) { |
| 1167 | _once_registry = PyDict_New(); |
| 1168 | if (_once_registry == NULL) |
| 1169 | return NULL; |
| 1170 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1171 | Py_INCREF(_once_registry); |
Brett Cannon | ef0e6c3 | 2010-09-04 18:24:04 +0000 | [diff] [blame] | 1172 | if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1173 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1175 | if (_default_action == NULL) { |
| 1176 | _default_action = PyUnicode_FromString("default"); |
| 1177 | if (_default_action == NULL) |
| 1178 | return NULL; |
| 1179 | } |
| 1180 | Py_INCREF(_default_action); |
Brett Cannon | ef0e6c3 | 2010-09-04 18:24:04 +0000 | [diff] [blame] | 1181 | if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1182 | return NULL; |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1183 | |
| 1184 | _filters_version = 0; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1185 | return m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1186 | } |