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