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) { |
| 88 | if (PyErr_Occurred()) { |
| 89 | return NULL; |
| 90 | } |
| 91 | return _default_action; |
| 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 | |
| 119 | if (!PyList_Check(_filters)) { |
| 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) |
| 205 | return NULL; |
| 206 | len = PyUnicode_GetSize(filename); |
| 207 | if (len < 0) |
| 208 | return NULL; |
| 209 | if (len >= 3 && |
| 210 | strncmp(mod_str + (len - 3), ".py", 3) == 0) { |
| 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 | { |
| 246 | PyObject *f_stderr; |
| 247 | PyObject *name; |
| 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? */ |
| 254 | return; |
| 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); |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +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 |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 285 | if (_Py_DisplaySourceLine(f_stderr, _PyUnicode_AsString(filename), |
| 286 | lineno, 2) < 0) |
| 287 | return; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 288 | PyErr_Clear(); |
| 289 | } |
| 290 | |
| 291 | static PyObject * |
| 292 | warn_explicit(PyObject *category, PyObject *message, |
| 293 | PyObject *filename, int lineno, |
| 294 | PyObject *module, PyObject *registry, PyObject *sourceline) |
| 295 | { |
| 296 | PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
| 297 | PyObject *item = Py_None; |
| 298 | const char *action; |
| 299 | int rc; |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 300 | |
| 301 | if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
| 302 | PyErr_SetString(PyExc_TypeError, "'registry' must be a dict"); |
| 303 | return NULL; |
| 304 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 305 | |
| 306 | /* Normalize module. */ |
| 307 | if (module == NULL) { |
| 308 | module = normalize_module(filename); |
| 309 | if (module == NULL) |
| 310 | return NULL; |
| 311 | } |
| 312 | else |
| 313 | Py_INCREF(module); |
| 314 | |
| 315 | /* Normalize message. */ |
| 316 | Py_INCREF(message); /* DECREF'ed in cleanup. */ |
| 317 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 318 | if (rc == -1) { |
| 319 | goto cleanup; |
| 320 | } |
| 321 | if (rc == 1) { |
| 322 | text = PyObject_Str(message); |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 323 | if (text == NULL) |
| 324 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 325 | category = (PyObject*)message->ob_type; |
| 326 | } |
| 327 | else { |
| 328 | text = message; |
| 329 | message = PyObject_CallFunction(category, "O", message); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 330 | if (message == NULL) |
| 331 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | lineno_obj = PyLong_FromLong(lineno); |
| 335 | if (lineno_obj == NULL) |
| 336 | goto cleanup; |
| 337 | |
| 338 | /* Create key. */ |
| 339 | key = PyTuple_Pack(3, text, category, lineno_obj); |
| 340 | if (key == NULL) |
| 341 | goto cleanup; |
| 342 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 343 | if ((registry != NULL) && (registry != Py_None)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 344 | rc = already_warned(registry, key, 0); |
| 345 | if (rc == -1) |
| 346 | goto cleanup; |
| 347 | else if (rc == 1) |
| 348 | goto return_none; |
| 349 | /* Else this warning hasn't been generated before. */ |
| 350 | } |
| 351 | |
| 352 | action = get_filter(category, text, lineno, module, &item); |
| 353 | if (action == NULL) |
| 354 | goto cleanup; |
| 355 | |
| 356 | if (strcmp(action, "error") == 0) { |
| 357 | PyErr_SetObject(category, message); |
| 358 | goto cleanup; |
| 359 | } |
| 360 | |
| 361 | /* Store in the registry that we've been here, *except* when the action |
| 362 | is "always". */ |
| 363 | rc = 0; |
| 364 | if (strcmp(action, "always") != 0) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 365 | if (registry != NULL && registry != Py_None && |
| 366 | PyDict_SetItem(registry, key, Py_True) < 0) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 367 | goto cleanup; |
| 368 | else if (strcmp(action, "ignore") == 0) |
| 369 | goto return_none; |
| 370 | else if (strcmp(action, "once") == 0) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 371 | if (registry == NULL || registry == Py_None) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 372 | registry = get_once_registry(); |
| 373 | if (registry == NULL) |
| 374 | goto cleanup; |
| 375 | } |
| 376 | /* _once_registry[(text, category)] = 1 */ |
| 377 | rc = update_registry(registry, text, category, 0); |
| 378 | } |
| 379 | else if (strcmp(action, "module") == 0) { |
| 380 | /* registry[(text, category, 0)] = 1 */ |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 381 | if (registry != NULL && registry != Py_None) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 382 | rc = update_registry(registry, text, category, 0); |
| 383 | } |
| 384 | else if (strcmp(action, "default") != 0) { |
| 385 | PyObject *to_str = PyObject_Str(item); |
| 386 | const char *err_str = "???"; |
| 387 | |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 388 | if (to_str != NULL) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 389 | err_str = _PyUnicode_AsString(to_str); |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 390 | if (err_str == NULL) |
| 391 | goto cleanup; |
| 392 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 393 | PyErr_Format(PyExc_RuntimeError, |
| 394 | "Unrecognized action (%s) in warnings.filters:\n %s", |
| 395 | action, err_str); |
| 396 | Py_XDECREF(to_str); |
| 397 | goto cleanup; |
| 398 | } |
| 399 | } |
| 400 | |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 401 | if (rc == 1) /* Already warned for this module. */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 402 | goto return_none; |
| 403 | if (rc == 0) { |
| 404 | PyObject *show_fxn = get_warnings_attr("showwarning"); |
| 405 | if (show_fxn == NULL) { |
| 406 | if (PyErr_Occurred()) |
| 407 | goto cleanup; |
| 408 | show_warning(filename, lineno, text, category, sourceline); |
| 409 | } |
| 410 | else { |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 411 | PyObject *res; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 412 | |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 413 | if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) { |
| 414 | PyErr_SetString(PyExc_TypeError, |
| 415 | "warnings.showwarning() must be set to a " |
| 416 | "function or method"); |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 417 | Py_DECREF(show_fxn); |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 418 | goto cleanup; |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 419 | } |
Brett Cannon | ec92e18 | 2008-09-02 02:46:59 +0000 | [diff] [blame] | 420 | |
| 421 | res = PyObject_CallFunctionObjArgs(show_fxn, message, category, |
| 422 | filename, lineno_obj, |
| 423 | NULL); |
| 424 | Py_DECREF(show_fxn); |
| 425 | Py_XDECREF(res); |
| 426 | if (res == NULL) |
| 427 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | else /* if (rc == -1) */ |
| 431 | goto cleanup; |
| 432 | |
| 433 | return_none: |
| 434 | result = Py_None; |
| 435 | Py_INCREF(result); |
| 436 | |
| 437 | cleanup: |
| 438 | Py_XDECREF(key); |
| 439 | Py_XDECREF(text); |
| 440 | Py_XDECREF(lineno_obj); |
| 441 | Py_DECREF(module); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 442 | Py_XDECREF(message); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 443 | return result; /* Py_None or NULL. */ |
| 444 | } |
| 445 | |
| 446 | /* filename, module, and registry are new refs, globals is borrowed */ |
| 447 | /* Returns 0 on error (no new refs), 1 on success */ |
| 448 | static int |
| 449 | setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, |
| 450 | PyObject **module, PyObject **registry) |
| 451 | { |
| 452 | PyObject *globals; |
| 453 | |
| 454 | /* Setup globals and lineno. */ |
| 455 | PyFrameObject *f = PyThreadState_GET()->frame; |
Christian Heimes | 5d8da20 | 2008-05-06 13:58:24 +0000 | [diff] [blame] | 456 | while (--stack_level > 0 && f != NULL) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 457 | f = f->f_back; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 458 | |
| 459 | if (f == NULL) { |
| 460 | globals = PyThreadState_Get()->interp->sysdict; |
| 461 | *lineno = 1; |
| 462 | } |
| 463 | else { |
| 464 | globals = f->f_globals; |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 465 | *lineno = PyFrame_GetLineNumber(f); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | *module = NULL; |
| 469 | |
| 470 | /* Setup registry. */ |
| 471 | assert(globals != NULL); |
| 472 | assert(PyDict_Check(globals)); |
| 473 | *registry = PyDict_GetItemString(globals, "__warningregistry__"); |
| 474 | if (*registry == NULL) { |
| 475 | int rc; |
| 476 | |
| 477 | *registry = PyDict_New(); |
| 478 | if (*registry == NULL) |
| 479 | return 0; |
| 480 | |
| 481 | rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); |
| 482 | if (rc < 0) |
| 483 | goto handle_error; |
| 484 | } |
| 485 | else |
| 486 | Py_INCREF(*registry); |
| 487 | |
| 488 | /* Setup module. */ |
| 489 | *module = PyDict_GetItemString(globals, "__name__"); |
| 490 | if (*module == NULL) { |
| 491 | *module = PyUnicode_FromString("<string>"); |
| 492 | if (*module == NULL) |
| 493 | goto handle_error; |
| 494 | } |
| 495 | else |
| 496 | Py_INCREF(*module); |
| 497 | |
| 498 | /* Setup filename. */ |
| 499 | *filename = PyDict_GetItemString(globals, "__file__"); |
| 500 | if (*filename != NULL) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 501 | Py_ssize_t len = PyUnicode_GetSize(*filename); |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 502 | const char *file_str = _PyUnicode_AsString(*filename); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 503 | if (file_str == NULL || (len < 0 && PyErr_Occurred())) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 504 | goto handle_error; |
| 505 | |
| 506 | /* if filename.lower().endswith((".pyc", ".pyo")): */ |
| 507 | if (len >= 4 && |
| 508 | file_str[len-4] == '.' && |
| 509 | tolower(file_str[len-3]) == 'p' && |
| 510 | tolower(file_str[len-2]) == 'y' && |
| 511 | (tolower(file_str[len-1]) == 'c' || |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 512 | tolower(file_str[len-1]) == 'o')) |
| 513 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 514 | *filename = PyUnicode_FromStringAndSize(file_str, len-1); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 515 | if (*filename == NULL) |
| 516 | goto handle_error; |
| 517 | } |
| 518 | else |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 519 | Py_INCREF(*filename); |
| 520 | } |
| 521 | else { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 522 | const char *module_str = _PyUnicode_AsString(*module); |
Brett Cannon | 54bd41d | 2008-09-02 04:01:42 +0000 | [diff] [blame] | 523 | if (module_str == NULL) |
| 524 | goto handle_error; |
| 525 | if (strcmp(module_str, "__main__") == 0) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 526 | PyObject *argv = PySys_GetObject("argv"); |
| 527 | if (argv != NULL && PyList_Size(argv) > 0) { |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 528 | int is_true; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 529 | *filename = PyList_GetItem(argv, 0); |
| 530 | Py_INCREF(*filename); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 531 | /* If sys.argv[0] is false, then use '__main__'. */ |
| 532 | is_true = PyObject_IsTrue(*filename); |
| 533 | if (is_true < 0) { |
| 534 | Py_DECREF(*filename); |
| 535 | goto handle_error; |
| 536 | } |
| 537 | else if (!is_true) { |
| 538 | Py_DECREF(*filename); |
Benjamin Peterson | 9f4bf1d | 2008-05-04 23:22:13 +0000 | [diff] [blame] | 539 | *filename = PyUnicode_FromString("__main__"); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 540 | if (*filename == NULL) |
| 541 | goto handle_error; |
| 542 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 543 | } |
| 544 | else { |
| 545 | /* embedded interpreters don't have sys.argv, see bug #839151 */ |
| 546 | *filename = PyUnicode_FromString("__main__"); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 547 | if (*filename == NULL) |
| 548 | goto handle_error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | if (*filename == NULL) { |
| 552 | *filename = *module; |
| 553 | Py_INCREF(*filename); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return 1; |
| 558 | |
| 559 | handle_error: |
| 560 | /* filename not XDECREF'ed here as there is no way to jump here with a |
| 561 | dangling reference. */ |
| 562 | Py_XDECREF(*registry); |
| 563 | Py_XDECREF(*module); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static PyObject * |
| 568 | get_category(PyObject *message, PyObject *category) |
| 569 | { |
| 570 | int rc; |
| 571 | |
| 572 | /* Get category. */ |
| 573 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 574 | if (rc == -1) |
| 575 | return NULL; |
| 576 | |
| 577 | if (rc == 1) |
| 578 | category = (PyObject*)message->ob_type; |
| 579 | else if (category == NULL) |
| 580 | category = PyExc_UserWarning; |
| 581 | |
| 582 | /* Validate category. */ |
| 583 | rc = PyObject_IsSubclass(category, PyExc_Warning); |
| 584 | if (rc == -1) |
| 585 | return NULL; |
| 586 | if (rc == 0) { |
| 587 | PyErr_SetString(PyExc_ValueError, |
| 588 | "category is not a subclass of Warning"); |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | return category; |
| 593 | } |
| 594 | |
| 595 | static PyObject * |
| 596 | do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level) |
| 597 | { |
| 598 | PyObject *filename, *module, *registry, *res; |
| 599 | int lineno; |
| 600 | |
| 601 | if (!setup_context(stack_level, &filename, &lineno, &module, ®istry)) |
| 602 | return NULL; |
| 603 | |
| 604 | res = warn_explicit(category, message, filename, lineno, module, registry, |
| 605 | NULL); |
| 606 | Py_DECREF(filename); |
| 607 | Py_DECREF(registry); |
| 608 | Py_DECREF(module); |
| 609 | return res; |
| 610 | } |
| 611 | |
| 612 | static PyObject * |
| 613 | warnings_warn(PyObject *self, PyObject *args, PyObject *kwds) |
| 614 | { |
| 615 | static char *kw_list[] = { "message", "category", "stacklevel", 0 }; |
| 616 | PyObject *message, *category = NULL; |
| 617 | Py_ssize_t stack_level = 1; |
| 618 | |
| 619 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list, |
| 620 | &message, &category, &stack_level)) |
| 621 | return NULL; |
| 622 | |
| 623 | category = get_category(message, category); |
| 624 | if (category == NULL) |
| 625 | return NULL; |
| 626 | return do_warn(message, category, stack_level); |
| 627 | } |
| 628 | |
| 629 | static PyObject * |
| 630 | warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) |
| 631 | { |
| 632 | static char *kwd_list[] = {"message", "category", "filename", "lineno", |
| 633 | "module", "registry", "module_globals", 0}; |
| 634 | PyObject *message; |
| 635 | PyObject *category; |
| 636 | PyObject *filename; |
| 637 | int lineno; |
| 638 | PyObject *module = NULL; |
| 639 | PyObject *registry = NULL; |
| 640 | PyObject *module_globals = NULL; |
| 641 | |
| 642 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOi|OOO:warn_explicit", |
| 643 | kwd_list, &message, &category, &filename, &lineno, &module, |
| 644 | ®istry, &module_globals)) |
| 645 | return NULL; |
| 646 | |
| 647 | if (module_globals) { |
| 648 | static PyObject *get_source_name = NULL; |
| 649 | static PyObject *splitlines_name = NULL; |
| 650 | PyObject *loader; |
| 651 | PyObject *module_name; |
| 652 | PyObject *source; |
| 653 | PyObject *source_list; |
| 654 | PyObject *source_line; |
| 655 | PyObject *returned; |
| 656 | |
| 657 | if (get_source_name == NULL) { |
| 658 | get_source_name = PyUnicode_InternFromString("get_source"); |
| 659 | if (!get_source_name) |
| 660 | return NULL; |
| 661 | } |
| 662 | if (splitlines_name == NULL) { |
| 663 | splitlines_name = PyUnicode_InternFromString("splitlines"); |
| 664 | if (!splitlines_name) |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | /* Check/get the requisite pieces needed for the loader. */ |
| 669 | loader = PyDict_GetItemString(module_globals, "__loader__"); |
| 670 | module_name = PyDict_GetItemString(module_globals, "__name__"); |
| 671 | |
| 672 | if (loader == NULL || module_name == NULL) |
| 673 | goto standard_call; |
| 674 | |
| 675 | /* Make sure the loader implements the optional get_source() method. */ |
| 676 | if (!PyObject_HasAttrString(loader, "get_source")) |
| 677 | goto standard_call; |
| 678 | /* Call get_source() to get the source code. */ |
| 679 | source = PyObject_CallMethodObjArgs(loader, get_source_name, |
| 680 | module_name, NULL); |
| 681 | if (!source) |
| 682 | return NULL; |
| 683 | else if (source == Py_None) { |
| 684 | Py_DECREF(Py_None); |
| 685 | goto standard_call; |
| 686 | } |
| 687 | |
| 688 | /* Split the source into lines. */ |
| 689 | source_list = PyObject_CallMethodObjArgs(source, splitlines_name, |
| 690 | NULL); |
| 691 | Py_DECREF(source); |
| 692 | if (!source_list) |
| 693 | return NULL; |
| 694 | |
| 695 | /* Get the source line. */ |
| 696 | source_line = PyList_GetItem(source_list, lineno-1); |
| 697 | if (!source_line) { |
| 698 | Py_DECREF(source_list); |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | /* Handle the warning. */ |
| 703 | returned = warn_explicit(category, message, filename, lineno, module, |
| 704 | registry, source_line); |
| 705 | Py_DECREF(source_list); |
| 706 | return returned; |
| 707 | } |
| 708 | |
| 709 | standard_call: |
| 710 | return warn_explicit(category, message, filename, lineno, module, |
| 711 | registry, NULL); |
| 712 | } |
| 713 | |
| 714 | |
| 715 | /* Function to issue a warning message; may raise an exception. */ |
| 716 | int |
| 717 | PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
| 718 | { |
| 719 | PyObject *res; |
| 720 | PyObject *message = PyUnicode_FromString(text); |
| 721 | if (message == NULL) |
| 722 | return -1; |
| 723 | |
| 724 | if (category == NULL) |
| 725 | category = PyExc_RuntimeWarning; |
| 726 | |
| 727 | res = do_warn(message, category, stack_level); |
| 728 | Py_DECREF(message); |
| 729 | if (res == NULL) |
| 730 | return -1; |
| 731 | Py_DECREF(res); |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | /* PyErr_Warn is only for backwards compatability and will be removed. |
| 737 | Use PyErr_WarnEx instead. */ |
| 738 | |
| 739 | #undef PyErr_Warn |
| 740 | |
| 741 | PyAPI_FUNC(int) |
| 742 | PyErr_Warn(PyObject *category, char *text) |
| 743 | { |
| 744 | return PyErr_WarnEx(category, text, 1); |
| 745 | } |
| 746 | |
| 747 | /* Warning with explicit origin */ |
| 748 | int |
| 749 | PyErr_WarnExplicit(PyObject *category, const char *text, |
| 750 | const char *filename_str, int lineno, |
| 751 | const char *module_str, PyObject *registry) |
| 752 | { |
| 753 | PyObject *res; |
| 754 | PyObject *message = PyUnicode_FromString(text); |
| 755 | PyObject *filename = PyUnicode_FromString(filename_str); |
| 756 | PyObject *module = NULL; |
| 757 | int ret = -1; |
| 758 | |
| 759 | if (message == NULL || filename == NULL) |
| 760 | goto exit; |
| 761 | if (module_str != NULL) { |
| 762 | module = PyUnicode_FromString(module_str); |
| 763 | if (module == NULL) |
| 764 | goto exit; |
| 765 | } |
| 766 | |
| 767 | if (category == NULL) |
| 768 | category = PyExc_RuntimeWarning; |
| 769 | res = warn_explicit(category, message, filename, lineno, module, registry, |
| 770 | NULL); |
| 771 | if (res == NULL) |
| 772 | goto exit; |
| 773 | Py_DECREF(res); |
| 774 | ret = 0; |
| 775 | |
| 776 | exit: |
| 777 | Py_XDECREF(message); |
| 778 | Py_XDECREF(module); |
| 779 | Py_XDECREF(filename); |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | PyDoc_STRVAR(warn_doc, |
| 785 | "Issue a warning, or maybe ignore it or raise an exception."); |
| 786 | |
| 787 | PyDoc_STRVAR(warn_explicit_doc, |
| 788 | "Low-level inferface to warnings functionality."); |
| 789 | |
| 790 | static PyMethodDef warnings_functions[] = { |
| 791 | {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS, |
| 792 | warn_doc}, |
| 793 | {"warn_explicit", (PyCFunction)warnings_warn_explicit, |
| 794 | METH_VARARGS | METH_KEYWORDS, warn_explicit_doc}, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 795 | /* XXX(brett.cannon): add showwarning? */ |
| 796 | /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 797 | {NULL, NULL} /* sentinel */ |
| 798 | }; |
| 799 | |
| 800 | |
| 801 | static PyObject * |
| 802 | create_filter(PyObject *category, const char *action) |
| 803 | { |
| 804 | static PyObject *ignore_str = NULL; |
| 805 | static PyObject *error_str = NULL; |
| 806 | static PyObject *default_str = NULL; |
| 807 | PyObject *action_obj = NULL; |
| 808 | PyObject *lineno, *result; |
| 809 | |
| 810 | if (!strcmp(action, "ignore")) { |
| 811 | if (ignore_str == NULL) { |
| 812 | ignore_str = PyUnicode_InternFromString("ignore"); |
| 813 | if (ignore_str == NULL) |
| 814 | return NULL; |
| 815 | } |
| 816 | action_obj = ignore_str; |
| 817 | } |
| 818 | else if (!strcmp(action, "error")) { |
| 819 | if (error_str == NULL) { |
| 820 | error_str = PyUnicode_InternFromString("error"); |
| 821 | if (error_str == NULL) |
| 822 | return NULL; |
| 823 | } |
| 824 | action_obj = error_str; |
| 825 | } |
| 826 | else if (!strcmp(action, "default")) { |
| 827 | if (default_str == NULL) { |
| 828 | default_str = PyUnicode_InternFromString("default"); |
| 829 | if (default_str == NULL) |
| 830 | return NULL; |
| 831 | } |
| 832 | action_obj = default_str; |
| 833 | } |
| 834 | else { |
| 835 | Py_FatalError("unknown action"); |
| 836 | } |
| 837 | |
| 838 | /* This assumes the line number is zero for now. */ |
| 839 | lineno = PyLong_FromLong(0); |
| 840 | if (lineno == NULL) |
| 841 | return NULL; |
| 842 | result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno); |
| 843 | Py_DECREF(lineno); |
| 844 | return result; |
| 845 | } |
| 846 | |
| 847 | static PyObject * |
| 848 | init_filters(void) |
| 849 | { |
| 850 | PyObject *filters = PyList_New(3); |
| 851 | const char *bytes_action; |
| 852 | if (filters == NULL) |
| 853 | return NULL; |
| 854 | |
| 855 | PyList_SET_ITEM(filters, 0, |
| 856 | create_filter(PyExc_PendingDeprecationWarning, "ignore")); |
| 857 | PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, "ignore")); |
| 858 | if (Py_BytesWarningFlag > 1) |
| 859 | bytes_action = "error"; |
| 860 | else if (Py_BytesWarningFlag) |
| 861 | bytes_action = "default"; |
| 862 | else |
| 863 | bytes_action = "ignore"; |
| 864 | PyList_SET_ITEM(filters, 2, create_filter(PyExc_BytesWarning, |
| 865 | bytes_action)); |
| 866 | |
| 867 | if (PyList_GET_ITEM(filters, 0) == NULL || |
| 868 | PyList_GET_ITEM(filters, 1) == NULL || |
| 869 | PyList_GET_ITEM(filters, 2) == NULL) { |
| 870 | Py_DECREF(filters); |
| 871 | return NULL; |
| 872 | } |
| 873 | |
| 874 | return filters; |
| 875 | } |
| 876 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 877 | static struct PyModuleDef warningsmodule = { |
| 878 | PyModuleDef_HEAD_INIT, |
| 879 | MODULE_NAME, |
| 880 | warnings__doc__, |
| 881 | 0, |
| 882 | warnings_functions, |
| 883 | NULL, |
| 884 | NULL, |
| 885 | NULL, |
| 886 | NULL |
| 887 | }; |
| 888 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 889 | |
| 890 | PyMODINIT_FUNC |
| 891 | _PyWarnings_Init(void) |
| 892 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 893 | PyObject *m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 894 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 895 | m = PyModule_Create(&warningsmodule); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 896 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 897 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 898 | |
| 899 | _filters = init_filters(); |
| 900 | if (_filters == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 901 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 902 | Py_INCREF(_filters); |
| 903 | if (PyModule_AddObject(m, "filters", _filters) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 904 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 905 | |
| 906 | _once_registry = PyDict_New(); |
| 907 | if (_once_registry == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 908 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 909 | Py_INCREF(_once_registry); |
| 910 | if (PyModule_AddObject(m, "once_registry", _once_registry) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 911 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 912 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 913 | _default_action = PyUnicode_FromString("default"); |
| 914 | if (_default_action == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 915 | return NULL; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 916 | if (PyModule_AddObject(m, "default_action", _default_action) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 917 | return NULL; |
| 918 | return m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 919 | } |