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