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 */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | static int |
| 18 | check_matched(PyObject *obj, PyObject *arg) |
| 19 | { |
| 20 | PyObject *result; |
| 21 | int rc; |
| 22 | |
| 23 | if (obj == Py_None) |
| 24 | return 1; |
| 25 | result = PyObject_CallMethod(obj, "match", "O", arg); |
| 26 | if (result == NULL) |
| 27 | return -1; |
| 28 | |
| 29 | rc = PyObject_IsTrue(result); |
| 30 | Py_DECREF(result); |
| 31 | return rc; |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | Returns a new reference. |
| 36 | A NULL return value can mean false or an error. |
| 37 | */ |
| 38 | static PyObject * |
| 39 | get_warnings_attr(const char *attr) |
| 40 | { |
| 41 | static PyObject *warnings_str = NULL; |
| 42 | PyObject *all_modules; |
| 43 | PyObject *warnings_module; |
| 44 | int result; |
| 45 | |
| 46 | if (warnings_str == NULL) { |
| 47 | warnings_str = PyUnicode_InternFromString("warnings"); |
| 48 | if (warnings_str == NULL) |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | all_modules = PyImport_GetModuleDict(); |
| 53 | result = PyDict_Contains(all_modules, warnings_str); |
| 54 | if (result == -1 || result == 0) |
| 55 | return NULL; |
| 56 | |
| 57 | warnings_module = PyDict_GetItem(all_modules, warnings_str); |
| 58 | if (!PyObject_HasAttrString(warnings_module, attr)) |
| 59 | return NULL; |
| 60 | return PyObject_GetAttrString(warnings_module, attr); |
| 61 | } |
| 62 | |
| 63 | |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 64 | static PyObject * |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 65 | get_once_registry(void) |
| 66 | { |
| 67 | PyObject *registry; |
| 68 | |
| 69 | registry = get_warnings_attr("onceregistry"); |
| 70 | if (registry == NULL) { |
| 71 | if (PyErr_Occurred()) |
| 72 | return NULL; |
| 73 | return _once_registry; |
| 74 | } |
| 75 | Py_DECREF(_once_registry); |
| 76 | _once_registry = registry; |
| 77 | return registry; |
| 78 | } |
| 79 | |
| 80 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 81 | static PyObject * |
| 82 | get_default_action(void) |
| 83 | { |
| 84 | PyObject *default_action; |
| 85 | |
| 86 | default_action = get_warnings_attr("defaultaction"); |
| 87 | if (default_action == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | if (PyErr_Occurred()) { |
| 89 | return NULL; |
| 90 | } |
| 91 | return _default_action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Py_DECREF(_default_action); |
| 95 | _default_action = default_action; |
| 96 | return default_action; |
| 97 | } |
| 98 | |
| 99 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 100 | /* The item is a borrowed reference. */ |
| 101 | static const char * |
| 102 | get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, |
| 103 | PyObject *module, PyObject **item) |
| 104 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 105 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 106 | Py_ssize_t i; |
| 107 | PyObject *warnings_filters; |
| 108 | |
| 109 | warnings_filters = get_warnings_attr("filters"); |
| 110 | if (warnings_filters == NULL) { |
| 111 | if (PyErr_Occurred()) |
| 112 | return NULL; |
| 113 | } |
| 114 | else { |
| 115 | Py_DECREF(_filters); |
| 116 | _filters = warnings_filters; |
| 117 | } |
| 118 | |
Victor Stinner | 7d79b8b | 2010-05-19 20:40:50 +0000 | [diff] [blame] | 119 | if (_filters == NULL || !PyList_Check(_filters)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 120 | PyErr_SetString(PyExc_ValueError, |
| 121 | MODULE_NAME ".filters must be a list"); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | /* _filters could change while we are iterating over it. */ |
| 126 | for (i = 0; i < PyList_GET_SIZE(_filters); i++) { |
| 127 | PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; |
| 128 | Py_ssize_t ln; |
| 129 | int is_subclass, good_msg, good_mod; |
| 130 | |
| 131 | tmp_item = *item = PyList_GET_ITEM(_filters, i); |
| 132 | if (PyTuple_Size(tmp_item) != 5) { |
| 133 | PyErr_Format(PyExc_ValueError, |
| 134 | MODULE_NAME ".filters item %zd isn't a 5-tuple", i); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | /* Python code: action, msg, cat, mod, ln = item */ |
| 139 | action = PyTuple_GET_ITEM(tmp_item, 0); |
| 140 | msg = PyTuple_GET_ITEM(tmp_item, 1); |
| 141 | cat = PyTuple_GET_ITEM(tmp_item, 2); |
| 142 | mod = PyTuple_GET_ITEM(tmp_item, 3); |
| 143 | ln_obj = PyTuple_GET_ITEM(tmp_item, 4); |
| 144 | |
| 145 | good_msg = check_matched(msg, text); |
| 146 | good_mod = check_matched(mod, module); |
| 147 | is_subclass = PyObject_IsSubclass(category, cat); |
| 148 | ln = PyLong_AsSsize_t(ln_obj); |
| 149 | if (good_msg == -1 || good_mod == -1 || is_subclass == -1 || |
| 150 | (ln == -1 && PyErr_Occurred())) |
| 151 | return NULL; |
| 152 | |
| 153 | if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 154 | return _PyUnicode_AsString(action); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 157 | action = get_default_action(); |
| 158 | if (action != NULL) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 159 | return _PyUnicode_AsString(action); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 160 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 161 | |
| 162 | PyErr_SetString(PyExc_ValueError, |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 163 | MODULE_NAME ".defaultaction not found"); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 164 | return NULL; |
| 165 | } |
| 166 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 167 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 168 | static int |
| 169 | already_warned(PyObject *registry, PyObject *key, int should_set) |
| 170 | { |
| 171 | PyObject *already_warned; |
| 172 | |
| 173 | if (key == NULL) |
| 174 | return -1; |
| 175 | |
| 176 | already_warned = PyDict_GetItem(registry, key); |
| 177 | if (already_warned != NULL) { |
| 178 | int rc = PyObject_IsTrue(already_warned); |
| 179 | if (rc != 0) |
| 180 | return rc; |
| 181 | } |
| 182 | |
| 183 | /* This warning wasn't found in the registry, set it. */ |
| 184 | if (should_set) |
| 185 | return PyDict_SetItem(registry, key, Py_True); |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* New reference. */ |
| 190 | static PyObject * |
| 191 | normalize_module(PyObject *filename) |
| 192 | { |
| 193 | PyObject *module; |
| 194 | const char *mod_str; |
| 195 | Py_ssize_t len; |
| 196 | |
| 197 | int rc = PyObject_IsTrue(filename); |
| 198 | if (rc == -1) |
| 199 | return NULL; |
| 200 | else if (rc == 0) |
| 201 | return PyUnicode_FromString("<unknown>"); |
| 202 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 203 | mod_str = _PyUnicode_AsString(filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 204 | if (mod_str == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 206 | len = PyUnicode_GetSize(filename); |
| 207 | if (len < 0) |
| 208 | return NULL; |
| 209 | if (len >= 3 && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | strncmp(mod_str + (len - 3), ".py", 3) == 0) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 211 | module = PyUnicode_FromStringAndSize(mod_str, len-3); |
| 212 | } |
| 213 | else { |
| 214 | module = filename; |
| 215 | Py_INCREF(module); |
| 216 | } |
| 217 | return module; |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | update_registry(PyObject *registry, PyObject *text, PyObject *category, |
| 222 | int add_zero) |
| 223 | { |
| 224 | PyObject *altkey, *zero = NULL; |
| 225 | int rc; |
| 226 | |
| 227 | if (add_zero) { |
| 228 | zero = PyLong_FromLong(0); |
| 229 | if (zero == NULL) |
| 230 | return -1; |
| 231 | altkey = PyTuple_Pack(3, text, category, zero); |
| 232 | } |
| 233 | else |
| 234 | altkey = PyTuple_Pack(2, text, category); |
| 235 | |
| 236 | rc = already_warned(registry, altkey, 1); |
| 237 | Py_XDECREF(zero); |
| 238 | Py_XDECREF(altkey); |
| 239 | return rc; |
| 240 | } |
| 241 | |
| 242 | static void |
| 243 | show_warning(PyObject *filename, int lineno, PyObject *text, PyObject |
| 244 | *category, PyObject *sourceline) |
| 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | PyObject *f_stderr; |
| 247 | PyObject *name; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 248 | char lineno_str[128]; |
| 249 | |
| 250 | PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); |
| 251 | |
| 252 | name = PyObject_GetAttrString(category, "__name__"); |
| 253 | if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 254 | return; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 255 | |
| 256 | f_stderr = PySys_GetObject("stderr"); |
| 257 | if (f_stderr == NULL) { |
| 258 | fprintf(stderr, "lost sys.stderr\n"); |
| 259 | Py_DECREF(name); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | /* Print "filename:lineno: category: text\n" */ |
| 264 | PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW); |
| 265 | PyFile_WriteString(lineno_str, f_stderr); |
| 266 | PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW); |
| 267 | PyFile_WriteString(": ", f_stderr); |
| 268 | PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW); |
| 269 | PyFile_WriteString("\n", f_stderr); |
| 270 | Py_XDECREF(name); |
| 271 | |
| 272 | /* Print " source_line\n" */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 273 | if (sourceline) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 274 | char *source_line_str = _PyUnicode_AsString(sourceline); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | if (source_line_str == NULL) |
| 276 | return; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 277 | while (*source_line_str == ' ' || *source_line_str == '\t' || |
| 278 | *source_line_str == '\014') |
| 279 | source_line_str++; |
| 280 | |
| 281 | PyFile_WriteString(source_line_str, f_stderr); |
| 282 | PyFile_WriteString("\n", f_stderr); |
| 283 | } |
| 284 | else |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 285 | if (_Py_DisplaySourceLine(f_stderr, filename, lineno, 2) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | return; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 287 | PyErr_Clear(); |
| 288 | } |
| 289 | |
| 290 | static PyObject * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | warn_explicit(PyObject *category, PyObject *message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 292 | PyObject *filename, int lineno, |
| 293 | PyObject *module, PyObject *registry, PyObject *sourceline) |
| 294 | { |
| 295 | PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
| 296 | PyObject *item = Py_None; |
| 297 | const char *action; |
| 298 | int rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 300 | if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
| 301 | PyErr_SetString(PyExc_TypeError, "'registry' must be a dict"); |
| 302 | return NULL; |
| 303 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 304 | |
| 305 | /* Normalize module. */ |
| 306 | if (module == NULL) { |
| 307 | module = normalize_module(filename); |
| 308 | if (module == NULL) |
| 309 | return NULL; |
| 310 | } |
| 311 | else |
| 312 | Py_INCREF(module); |
| 313 | |
| 314 | /* Normalize message. */ |
| 315 | Py_INCREF(message); /* DECREF'ed in cleanup. */ |
| 316 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 317 | if (rc == -1) { |
| 318 | goto cleanup; |
| 319 | } |
| 320 | if (rc == 1) { |
| 321 | text = PyObject_Str(message); |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 322 | if (text == NULL) |
| 323 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 324 | category = (PyObject*)message->ob_type; |
| 325 | } |
| 326 | else { |
| 327 | text = message; |
| 328 | message = PyObject_CallFunction(category, "O", message); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 329 | if (message == NULL) |
| 330 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | lineno_obj = PyLong_FromLong(lineno); |
| 334 | if (lineno_obj == NULL) |
| 335 | goto cleanup; |
| 336 | |
| 337 | /* Create key. */ |
| 338 | key = PyTuple_Pack(3, text, category, lineno_obj); |
| 339 | if (key == NULL) |
| 340 | goto cleanup; |
| 341 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 342 | if ((registry != NULL) && (registry != Py_None)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 343 | rc = already_warned(registry, key, 0); |
| 344 | if (rc == -1) |
| 345 | goto cleanup; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | else if (rc == 1) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 347 | goto return_none; |
| 348 | /* Else this warning hasn't been generated before. */ |
| 349 | } |
| 350 | |
| 351 | action = get_filter(category, text, lineno, module, &item); |
| 352 | if (action == NULL) |
| 353 | goto cleanup; |
| 354 | |
| 355 | if (strcmp(action, "error") == 0) { |
| 356 | PyErr_SetObject(category, message); |
| 357 | goto cleanup; |
| 358 | } |
| 359 | |
| 360 | /* Store in the registry that we've been here, *except* when the action |
| 361 | is "always". */ |
| 362 | rc = 0; |
| 363 | if (strcmp(action, "always") != 0) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 364 | if (registry != NULL && registry != Py_None && |
| 365 | PyDict_SetItem(registry, key, Py_True) < 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 366 | goto cleanup; |
| 367 | else if (strcmp(action, "ignore") == 0) |
| 368 | goto return_none; |
| 369 | else if (strcmp(action, "once") == 0) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 370 | if (registry == NULL || registry == Py_None) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 371 | registry = get_once_registry(); |
| 372 | if (registry == NULL) |
| 373 | goto cleanup; |
| 374 | } |
| 375 | /* _once_registry[(text, category)] = 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 377 | } |
| 378 | else if (strcmp(action, "module") == 0) { |
| 379 | /* registry[(text, category, 0)] = 1 */ |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 380 | if (registry != NULL && registry != Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 382 | } |
| 383 | else if (strcmp(action, "default") != 0) { |
| 384 | PyObject *to_str = PyObject_Str(item); |
| 385 | const char *err_str = "???"; |
| 386 | |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 387 | if (to_str != NULL) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 388 | err_str = _PyUnicode_AsString(to_str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (err_str == NULL) |
| 390 | goto cleanup; |
| 391 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 392 | PyErr_Format(PyExc_RuntimeError, |
| 393 | "Unrecognized action (%s) in warnings.filters:\n %s", |
| 394 | action, err_str); |
| 395 | Py_XDECREF(to_str); |
| 396 | goto cleanup; |
| 397 | } |
| 398 | } |
| 399 | |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 400 | if (rc == 1) /* Already warned for this module. */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 401 | goto return_none; |
| 402 | if (rc == 0) { |
| 403 | PyObject *show_fxn = get_warnings_attr("showwarning"); |
| 404 | if (show_fxn == NULL) { |
| 405 | if (PyErr_Occurred()) |
| 406 | goto cleanup; |
| 407 | show_warning(filename, lineno, text, category, sourceline); |
| 408 | } |
| 409 | else { |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 410 | PyObject *res; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 411 | |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 412 | if (!PyCallable_Check(show_fxn)) { |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 413 | PyErr_SetString(PyExc_TypeError, |
| 414 | "warnings.showwarning() must be set to a " |
Brett Cannon | 52a7d98 | 2011-07-17 19:17:55 -0700 | [diff] [blame] | 415 | "callable"); |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 416 | Py_DECREF(show_fxn); |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 417 | goto cleanup; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 418 | } |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 419 | |
| 420 | res = PyObject_CallFunctionObjArgs(show_fxn, message, category, |
| 421 | filename, lineno_obj, |
| 422 | NULL); |
| 423 | Py_DECREF(show_fxn); |
| 424 | Py_XDECREF(res); |
| 425 | if (res == NULL) |
| 426 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | else /* if (rc == -1) */ |
| 430 | goto cleanup; |
| 431 | |
| 432 | return_none: |
| 433 | result = Py_None; |
| 434 | Py_INCREF(result); |
| 435 | |
| 436 | cleanup: |
| 437 | Py_XDECREF(key); |
| 438 | Py_XDECREF(text); |
| 439 | Py_XDECREF(lineno_obj); |
| 440 | Py_DECREF(module); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 441 | Py_XDECREF(message); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 442 | return result; /* Py_None or NULL. */ |
| 443 | } |
| 444 | |
| 445 | /* filename, module, and registry are new refs, globals is borrowed */ |
| 446 | /* Returns 0 on error (no new refs), 1 on success */ |
| 447 | static int |
| 448 | setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, |
| 449 | PyObject **module, PyObject **registry) |
| 450 | { |
| 451 | PyObject *globals; |
| 452 | |
| 453 | /* Setup globals and lineno. */ |
| 454 | PyFrameObject *f = PyThreadState_GET()->frame; |
Christian Heimes | 5d8da20 | 2008-05-06 13:58:24 +0000 | [diff] [blame] | 455 | while (--stack_level > 0 && f != NULL) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 456 | f = f->f_back; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 457 | |
| 458 | if (f == NULL) { |
| 459 | globals = PyThreadState_Get()->interp->sysdict; |
| 460 | *lineno = 1; |
| 461 | } |
| 462 | else { |
| 463 | globals = f->f_globals; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 464 | *lineno = PyFrame_GetLineNumber(f); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | *module = NULL; |
| 468 | |
| 469 | /* Setup registry. */ |
| 470 | assert(globals != NULL); |
| 471 | assert(PyDict_Check(globals)); |
| 472 | *registry = PyDict_GetItemString(globals, "__warningregistry__"); |
| 473 | if (*registry == NULL) { |
| 474 | int rc; |
| 475 | |
| 476 | *registry = PyDict_New(); |
| 477 | if (*registry == NULL) |
| 478 | return 0; |
| 479 | |
| 480 | rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); |
| 481 | if (rc < 0) |
| 482 | goto handle_error; |
| 483 | } |
| 484 | else |
| 485 | Py_INCREF(*registry); |
| 486 | |
| 487 | /* Setup module. */ |
| 488 | *module = PyDict_GetItemString(globals, "__name__"); |
| 489 | if (*module == NULL) { |
| 490 | *module = PyUnicode_FromString("<string>"); |
| 491 | if (*module == NULL) |
| 492 | goto handle_error; |
| 493 | } |
| 494 | else |
| 495 | Py_INCREF(*module); |
| 496 | |
| 497 | /* Setup filename. */ |
| 498 | *filename = PyDict_GetItemString(globals, "__file__"); |
Victor Stinner | 8b0508e | 2011-07-04 02:43:09 +0200 | [diff] [blame] | 499 | if (*filename != NULL && PyUnicode_Check(*filename)) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 500 | Py_ssize_t len = PyUnicode_GetSize(*filename); |
Victor Stinner | 2e5f117 | 2010-08-08 22:12:45 +0000 | [diff] [blame] | 501 | Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 502 | |
| 503 | /* if filename.lower().endswith((".pyc", ".pyo")): */ |
| 504 | if (len >= 4 && |
Victor Stinner | 2e5f117 | 2010-08-08 22:12:45 +0000 | [diff] [blame] | 505 | unicode[len-4] == '.' && |
| 506 | Py_UNICODE_TOLOWER(unicode[len-3]) == 'p' && |
| 507 | Py_UNICODE_TOLOWER(unicode[len-2]) == 'y' && |
| 508 | (Py_UNICODE_TOLOWER(unicode[len-1]) == 'c' || |
| 509 | Py_UNICODE_TOLOWER(unicode[len-1]) == 'o')) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 510 | { |
Victor Stinner | 2e5f117 | 2010-08-08 22:12:45 +0000 | [diff] [blame] | 511 | *filename = PyUnicode_FromUnicode(unicode, len-1); |
| 512 | if (*filename == NULL) |
| 513 | goto handle_error; |
| 514 | } |
| 515 | else |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 516 | Py_INCREF(*filename); |
| 517 | } |
| 518 | else { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 519 | const char *module_str = _PyUnicode_AsString(*module); |
Benjamin Peterson | bb4a747 | 2011-07-04 22:27:16 -0500 | [diff] [blame] | 520 | *filename = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | if (module_str == NULL) |
| 522 | goto handle_error; |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 523 | if (strcmp(module_str, "__main__") == 0) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 524 | PyObject *argv = PySys_GetObject("argv"); |
| 525 | if (argv != NULL && PyList_Size(argv) > 0) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 526 | int is_true; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 527 | *filename = PyList_GetItem(argv, 0); |
| 528 | Py_INCREF(*filename); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 529 | /* If sys.argv[0] is false, then use '__main__'. */ |
| 530 | is_true = PyObject_IsTrue(*filename); |
| 531 | if (is_true < 0) { |
| 532 | Py_DECREF(*filename); |
| 533 | goto handle_error; |
| 534 | } |
| 535 | else if (!is_true) { |
| 536 | Py_DECREF(*filename); |
Benjamin Peterson | 9f4bf1d | 2008-05-04 23:22:13 +0000 | [diff] [blame] | 537 | *filename = PyUnicode_FromString("__main__"); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 538 | if (*filename == NULL) |
| 539 | goto handle_error; |
| 540 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 541 | } |
| 542 | else { |
| 543 | /* embedded interpreters don't have sys.argv, see bug #839151 */ |
| 544 | *filename = PyUnicode_FromString("__main__"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | if (*filename == NULL) |
| 546 | goto handle_error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | if (*filename == NULL) { |
| 550 | *filename = *module; |
| 551 | Py_INCREF(*filename); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return 1; |
| 556 | |
| 557 | handle_error: |
| 558 | /* filename not XDECREF'ed here as there is no way to jump here with a |
| 559 | dangling reference. */ |
| 560 | Py_XDECREF(*registry); |
| 561 | Py_XDECREF(*module); |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static PyObject * |
| 566 | get_category(PyObject *message, PyObject *category) |
| 567 | { |
| 568 | int rc; |
| 569 | |
| 570 | /* Get category. */ |
| 571 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 572 | if (rc == -1) |
| 573 | return NULL; |
| 574 | |
| 575 | if (rc == 1) |
| 576 | category = (PyObject*)message->ob_type; |
| 577 | else if (category == NULL) |
| 578 | category = PyExc_UserWarning; |
| 579 | |
| 580 | /* Validate category. */ |
| 581 | rc = PyObject_IsSubclass(category, PyExc_Warning); |
| 582 | if (rc == -1) |
| 583 | return NULL; |
| 584 | if (rc == 0) { |
| 585 | PyErr_SetString(PyExc_ValueError, |
| 586 | "category is not a subclass of Warning"); |
| 587 | return NULL; |
| 588 | } |
| 589 | |
| 590 | return category; |
| 591 | } |
| 592 | |
| 593 | static PyObject * |
| 594 | do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level) |
| 595 | { |
| 596 | PyObject *filename, *module, *registry, *res; |
| 597 | int lineno; |
| 598 | |
| 599 | if (!setup_context(stack_level, &filename, &lineno, &module, ®istry)) |
| 600 | return NULL; |
| 601 | |
| 602 | res = warn_explicit(category, message, filename, lineno, module, registry, |
| 603 | NULL); |
| 604 | Py_DECREF(filename); |
| 605 | Py_DECREF(registry); |
| 606 | Py_DECREF(module); |
| 607 | return res; |
| 608 | } |
| 609 | |
| 610 | static PyObject * |
| 611 | warnings_warn(PyObject *self, PyObject *args, PyObject *kwds) |
| 612 | { |
| 613 | static char *kw_list[] = { "message", "category", "stacklevel", 0 }; |
| 614 | PyObject *message, *category = NULL; |
| 615 | Py_ssize_t stack_level = 1; |
| 616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 618 | &message, &category, &stack_level)) |
| 619 | return NULL; |
| 620 | |
| 621 | category = get_category(message, category); |
| 622 | if (category == NULL) |
| 623 | return NULL; |
| 624 | return do_warn(message, category, stack_level); |
| 625 | } |
| 626 | |
| 627 | static PyObject * |
| 628 | warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) |
| 629 | { |
| 630 | static char *kwd_list[] = {"message", "category", "filename", "lineno", |
| 631 | "module", "registry", "module_globals", 0}; |
| 632 | PyObject *message; |
| 633 | PyObject *category; |
| 634 | PyObject *filename; |
| 635 | int lineno; |
| 636 | PyObject *module = NULL; |
| 637 | PyObject *registry = NULL; |
| 638 | PyObject *module_globals = NULL; |
| 639 | |
| 640 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOi|OOO:warn_explicit", |
| 641 | kwd_list, &message, &category, &filename, &lineno, &module, |
| 642 | ®istry, &module_globals)) |
| 643 | return NULL; |
| 644 | |
| 645 | if (module_globals) { |
| 646 | static PyObject *get_source_name = NULL; |
| 647 | static PyObject *splitlines_name = NULL; |
| 648 | PyObject *loader; |
| 649 | PyObject *module_name; |
| 650 | PyObject *source; |
| 651 | PyObject *source_list; |
| 652 | PyObject *source_line; |
| 653 | PyObject *returned; |
| 654 | |
| 655 | if (get_source_name == NULL) { |
| 656 | get_source_name = PyUnicode_InternFromString("get_source"); |
| 657 | if (!get_source_name) |
| 658 | return NULL; |
| 659 | } |
| 660 | if (splitlines_name == NULL) { |
| 661 | splitlines_name = PyUnicode_InternFromString("splitlines"); |
| 662 | if (!splitlines_name) |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | /* Check/get the requisite pieces needed for the loader. */ |
| 667 | loader = PyDict_GetItemString(module_globals, "__loader__"); |
| 668 | module_name = PyDict_GetItemString(module_globals, "__name__"); |
| 669 | |
| 670 | if (loader == NULL || module_name == NULL) |
| 671 | goto standard_call; |
| 672 | |
| 673 | /* Make sure the loader implements the optional get_source() method. */ |
| 674 | if (!PyObject_HasAttrString(loader, "get_source")) |
| 675 | goto standard_call; |
| 676 | /* Call get_source() to get the source code. */ |
| 677 | source = PyObject_CallMethodObjArgs(loader, get_source_name, |
| 678 | module_name, NULL); |
| 679 | if (!source) |
| 680 | return NULL; |
| 681 | else if (source == Py_None) { |
| 682 | Py_DECREF(Py_None); |
| 683 | goto standard_call; |
| 684 | } |
| 685 | |
| 686 | /* Split the source into lines. */ |
| 687 | source_list = PyObject_CallMethodObjArgs(source, splitlines_name, |
| 688 | NULL); |
| 689 | Py_DECREF(source); |
| 690 | if (!source_list) |
| 691 | return NULL; |
| 692 | |
| 693 | /* Get the source line. */ |
| 694 | source_line = PyList_GetItem(source_list, lineno-1); |
| 695 | if (!source_line) { |
| 696 | Py_DECREF(source_list); |
| 697 | return NULL; |
| 698 | } |
| 699 | |
| 700 | /* Handle the warning. */ |
| 701 | returned = warn_explicit(category, message, filename, lineno, module, |
| 702 | registry, source_line); |
| 703 | Py_DECREF(source_list); |
| 704 | return returned; |
| 705 | } |
| 706 | |
| 707 | standard_call: |
| 708 | return warn_explicit(category, message, filename, lineno, module, |
| 709 | registry, NULL); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | /* Function to issue a warning message; may raise an exception. */ |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 714 | |
| 715 | static int |
| 716 | warn_unicode(PyObject *category, PyObject *message, |
| 717 | Py_ssize_t stack_level) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 718 | { |
| 719 | PyObject *res; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 720 | |
| 721 | if (category == NULL) |
| 722 | category = PyExc_RuntimeWarning; |
| 723 | |
| 724 | res = do_warn(message, category, stack_level); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 725 | if (res == NULL) |
| 726 | return -1; |
| 727 | Py_DECREF(res); |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 732 | int |
| 733 | PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, |
| 734 | const char *format, ...) |
| 735 | { |
| 736 | int ret; |
| 737 | PyObject *message; |
| 738 | va_list vargs; |
| 739 | |
| 740 | #ifdef HAVE_STDARG_PROTOTYPES |
| 741 | va_start(vargs, format); |
| 742 | #else |
| 743 | va_start(vargs); |
| 744 | #endif |
| 745 | message = PyUnicode_FromFormatV(format, vargs); |
| 746 | if (message != NULL) { |
| 747 | ret = warn_unicode(category, message, stack_level); |
| 748 | Py_DECREF(message); |
| 749 | } |
| 750 | else |
| 751 | ret = -1; |
| 752 | va_end(vargs); |
| 753 | return ret; |
| 754 | } |
| 755 | |
| 756 | int |
| 757 | PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
| 758 | { |
| 759 | int ret; |
| 760 | PyObject *message = PyUnicode_FromString(text); |
| 761 | if (message == NULL) |
| 762 | return -1; |
| 763 | ret = warn_unicode(category, message, stack_level); |
| 764 | Py_DECREF(message); |
| 765 | return ret; |
| 766 | } |
| 767 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 768 | /* PyErr_Warn is only for backwards compatibility and will be removed. |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 769 | Use PyErr_WarnEx instead. */ |
| 770 | |
| 771 | #undef PyErr_Warn |
| 772 | |
| 773 | PyAPI_FUNC(int) |
| 774 | PyErr_Warn(PyObject *category, char *text) |
| 775 | { |
| 776 | return PyErr_WarnEx(category, text, 1); |
| 777 | } |
| 778 | |
| 779 | /* Warning with explicit origin */ |
| 780 | int |
| 781 | PyErr_WarnExplicit(PyObject *category, const char *text, |
| 782 | const char *filename_str, int lineno, |
| 783 | const char *module_str, PyObject *registry) |
| 784 | { |
| 785 | PyObject *res; |
| 786 | PyObject *message = PyUnicode_FromString(text); |
Victor Stinner | cb428f0 | 2010-12-27 20:10:36 +0000 | [diff] [blame] | 787 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 788 | PyObject *module = NULL; |
| 789 | int ret = -1; |
| 790 | |
| 791 | if (message == NULL || filename == NULL) |
| 792 | goto exit; |
| 793 | if (module_str != NULL) { |
| 794 | module = PyUnicode_FromString(module_str); |
| 795 | if (module == NULL) |
| 796 | goto exit; |
| 797 | } |
| 798 | |
| 799 | if (category == NULL) |
| 800 | category = PyExc_RuntimeWarning; |
| 801 | res = warn_explicit(category, message, filename, lineno, module, registry, |
| 802 | NULL); |
| 803 | if (res == NULL) |
| 804 | goto exit; |
| 805 | Py_DECREF(res); |
| 806 | ret = 0; |
| 807 | |
| 808 | exit: |
| 809 | Py_XDECREF(message); |
| 810 | Py_XDECREF(module); |
| 811 | Py_XDECREF(filename); |
| 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | PyDoc_STRVAR(warn_doc, |
| 817 | "Issue a warning, or maybe ignore it or raise an exception."); |
| 818 | |
| 819 | PyDoc_STRVAR(warn_explicit_doc, |
| 820 | "Low-level inferface to warnings functionality."); |
| 821 | |
| 822 | static PyMethodDef warnings_functions[] = { |
| 823 | {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS, |
| 824 | warn_doc}, |
| 825 | {"warn_explicit", (PyCFunction)warnings_warn_explicit, |
| 826 | METH_VARARGS | METH_KEYWORDS, warn_explicit_doc}, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 827 | /* XXX(brett.cannon): add showwarning? */ |
| 828 | /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | {NULL, NULL} /* sentinel */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 830 | }; |
| 831 | |
| 832 | |
| 833 | static PyObject * |
| 834 | create_filter(PyObject *category, const char *action) |
| 835 | { |
| 836 | static PyObject *ignore_str = NULL; |
| 837 | static PyObject *error_str = NULL; |
| 838 | static PyObject *default_str = NULL; |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 839 | static PyObject *always_str = NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 840 | PyObject *action_obj = NULL; |
| 841 | PyObject *lineno, *result; |
| 842 | |
| 843 | if (!strcmp(action, "ignore")) { |
| 844 | if (ignore_str == NULL) { |
| 845 | ignore_str = PyUnicode_InternFromString("ignore"); |
| 846 | if (ignore_str == NULL) |
| 847 | return NULL; |
| 848 | } |
| 849 | action_obj = ignore_str; |
| 850 | } |
| 851 | else if (!strcmp(action, "error")) { |
| 852 | if (error_str == NULL) { |
| 853 | error_str = PyUnicode_InternFromString("error"); |
| 854 | if (error_str == NULL) |
| 855 | return NULL; |
| 856 | } |
| 857 | action_obj = error_str; |
| 858 | } |
| 859 | else if (!strcmp(action, "default")) { |
| 860 | if (default_str == NULL) { |
| 861 | default_str = PyUnicode_InternFromString("default"); |
| 862 | if (default_str == NULL) |
| 863 | return NULL; |
| 864 | } |
| 865 | action_obj = default_str; |
| 866 | } |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 867 | else if (!strcmp(action, "always")) { |
| 868 | if (always_str == NULL) { |
| 869 | always_str = PyUnicode_InternFromString("always"); |
| 870 | if (always_str == NULL) |
| 871 | return NULL; |
| 872 | } |
| 873 | action_obj = always_str; |
| 874 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 875 | else { |
| 876 | Py_FatalError("unknown action"); |
| 877 | } |
| 878 | |
| 879 | /* This assumes the line number is zero for now. */ |
| 880 | lineno = PyLong_FromLong(0); |
| 881 | if (lineno == NULL) |
| 882 | return NULL; |
| 883 | result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno); |
| 884 | Py_DECREF(lineno); |
| 885 | return result; |
| 886 | } |
| 887 | |
| 888 | static PyObject * |
| 889 | init_filters(void) |
| 890 | { |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 891 | /* Don't silence DeprecationWarning if -3 was used. */ |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 892 | PyObject *filters = PyList_New(5); |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 893 | unsigned int pos = 0; /* Post-incremented in each use. */ |
| 894 | unsigned int x; |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 895 | const char *bytes_action, *resource_action; |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 896 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 897 | if (filters == NULL) |
| 898 | return NULL; |
| 899 | |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 900 | PyList_SET_ITEM(filters, pos++, |
| 901 | create_filter(PyExc_DeprecationWarning, "ignore")); |
| 902 | PyList_SET_ITEM(filters, pos++, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 903 | create_filter(PyExc_PendingDeprecationWarning, "ignore")); |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 904 | PyList_SET_ITEM(filters, pos++, |
| 905 | create_filter(PyExc_ImportWarning, "ignore")); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 906 | if (Py_BytesWarningFlag > 1) |
| 907 | bytes_action = "error"; |
| 908 | else if (Py_BytesWarningFlag) |
| 909 | bytes_action = "default"; |
| 910 | else |
| 911 | bytes_action = "ignore"; |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 912 | PyList_SET_ITEM(filters, pos++, create_filter(PyExc_BytesWarning, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 913 | bytes_action)); |
Georg Brandl | 08be72d | 2010-10-24 15:11:22 +0000 | [diff] [blame] | 914 | /* resource usage warnings are enabled by default in pydebug mode */ |
| 915 | #ifdef Py_DEBUG |
| 916 | resource_action = "always"; |
| 917 | #else |
| 918 | resource_action = "ignore"; |
| 919 | #endif |
| 920 | PyList_SET_ITEM(filters, pos++, create_filter(PyExc_ResourceWarning, |
| 921 | resource_action)); |
Benjamin Peterson | 7ab4b8d | 2010-06-28 00:01:59 +0000 | [diff] [blame] | 922 | for (x = 0; x < pos; x += 1) { |
| 923 | if (PyList_GET_ITEM(filters, x) == NULL) { |
| 924 | Py_DECREF(filters); |
| 925 | return NULL; |
| 926 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | return filters; |
| 930 | } |
| 931 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 932 | static struct PyModuleDef warningsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | PyModuleDef_HEAD_INIT, |
| 934 | MODULE_NAME, |
| 935 | warnings__doc__, |
| 936 | 0, |
| 937 | warnings_functions, |
| 938 | NULL, |
| 939 | NULL, |
| 940 | NULL, |
| 941 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 942 | }; |
| 943 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 944 | |
| 945 | PyMODINIT_FUNC |
| 946 | _PyWarnings_Init(void) |
| 947 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 948 | PyObject *m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 949 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 950 | m = PyModule_Create(&warningsmodule); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 951 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 952 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 953 | |
| 954 | _filters = init_filters(); |
| 955 | if (_filters == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 956 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 957 | Py_INCREF(_filters); |
| 958 | if (PyModule_AddObject(m, "filters", _filters) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 959 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 960 | |
| 961 | _once_registry = PyDict_New(); |
| 962 | if (_once_registry == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 963 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 964 | Py_INCREF(_once_registry); |
Brett Cannon | ef0e6c3 | 2010-09-04 18:24:04 +0000 | [diff] [blame] | 965 | if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 966 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 967 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 968 | _default_action = PyUnicode_FromString("default"); |
| 969 | if (_default_action == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 970 | return NULL; |
Brett Cannon | ef0e6c3 | 2010-09-04 18:24:04 +0000 | [diff] [blame] | 971 | if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 972 | return NULL; |
| 973 | return m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 974 | } |