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