Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 2 | #include "pycore_initconfig.h" |
Victor Stinner | 4a3fe08 | 2020-04-14 14:26:24 +0200 | [diff] [blame] | 3 | #include "pycore_interp.h" // PyInterpreterState.warnings |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 4 | #include "pycore_pyerrors.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 5 | #include "pycore_pystate.h" // _PyThreadState_GET() |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 6 | #include "frameobject.h" // PyFrame_GetBack() |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 7 | #include "clinic/_warnings.c.h" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 8 | |
| 9 | #define MODULE_NAME "_warnings" |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 10 | |
| 11 | PyDoc_STRVAR(warnings__doc__, |
| 12 | MODULE_NAME " provides basic warning filtering support.\n" |
| 13 | "It is a helper module to speed up interpreter start-up."); |
| 14 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 15 | _Py_IDENTIFIER(stderr); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 16 | #ifndef Py_DEBUG |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 17 | _Py_IDENTIFIER(default); |
Victor Stinner | b98f171 | 2017-11-23 17:13:44 +0100 | [diff] [blame] | 18 | _Py_IDENTIFIER(ignore); |
Victor Stinner | 747f48e | 2017-12-12 22:59:48 +0100 | [diff] [blame] | 19 | #endif |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 20 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 21 | |
| 22 | /*************************************************************************/ |
| 23 | |
| 24 | typedef struct _warnings_runtime_state WarningsState; |
| 25 | |
| 26 | /* Forward declaration of the _warnings module definition. */ |
| 27 | static struct PyModuleDef warningsmodule; |
| 28 | |
Hai Shi | 46874c2 | 2020-01-30 17:20:25 -0600 | [diff] [blame] | 29 | _Py_IDENTIFIER(__name__); |
| 30 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 31 | /* Given a module object, get its per-module state. */ |
| 32 | static WarningsState * |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 33 | warnings_get_state(void) |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 34 | { |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 35 | PyThreadState *tstate = _PyThreadState_GET(); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 36 | if (tstate == NULL) { |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 37 | _PyErr_SetString(tstate, PyExc_RuntimeError, |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 38 | "warnings_get_state: could not identify " |
Victor Stinner | 4d231bc | 2019-11-14 13:36:21 +0100 | [diff] [blame] | 39 | "current interpreter"); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
| 42 | return &tstate->interp->warnings; |
| 43 | } |
| 44 | |
| 45 | /* Clear the given warnings module state. */ |
| 46 | static void |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 47 | warnings_clear_state(WarningsState *st) |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 48 | { |
| 49 | Py_CLEAR(st->filters); |
| 50 | Py_CLEAR(st->once_registry); |
| 51 | Py_CLEAR(st->default_action); |
| 52 | } |
| 53 | |
| 54 | #ifndef Py_DEBUG |
| 55 | static PyObject * |
| 56 | create_filter(PyObject *category, _Py_Identifier *id, const char *modname) |
| 57 | { |
| 58 | PyObject *modname_obj = NULL; |
| 59 | PyObject *action_str = _PyUnicode_FromId(id); |
| 60 | if (action_str == NULL) { |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | /* Default to "no module name" for initial filter set */ |
| 65 | if (modname != NULL) { |
| 66 | modname_obj = PyUnicode_InternFromString(modname); |
| 67 | if (modname_obj == NULL) { |
| 68 | return NULL; |
| 69 | } |
| 70 | } else { |
| 71 | modname_obj = Py_None; |
| 72 | } |
| 73 | |
| 74 | /* This assumes the line number is zero for now. */ |
| 75 | return PyTuple_Pack(5, action_str, Py_None, |
| 76 | category, modname_obj, _PyLong_Zero); |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | static PyObject * |
| 81 | init_filters(void) |
| 82 | { |
| 83 | #ifdef Py_DEBUG |
| 84 | /* Py_DEBUG builds show all warnings by default */ |
| 85 | return PyList_New(0); |
| 86 | #else |
| 87 | /* Other builds ignore a number of warning categories by default */ |
| 88 | PyObject *filters = PyList_New(5); |
| 89 | if (filters == NULL) { |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | size_t pos = 0; /* Post-incremented in each use. */ |
| 94 | PyList_SET_ITEM(filters, pos++, |
| 95 | create_filter(PyExc_DeprecationWarning, &PyId_default, "__main__")); |
| 96 | PyList_SET_ITEM(filters, pos++, |
| 97 | create_filter(PyExc_DeprecationWarning, &PyId_ignore, NULL)); |
| 98 | PyList_SET_ITEM(filters, pos++, |
| 99 | create_filter(PyExc_PendingDeprecationWarning, &PyId_ignore, NULL)); |
| 100 | PyList_SET_ITEM(filters, pos++, |
| 101 | create_filter(PyExc_ImportWarning, &PyId_ignore, NULL)); |
| 102 | PyList_SET_ITEM(filters, pos++, |
| 103 | create_filter(PyExc_ResourceWarning, &PyId_ignore, NULL)); |
| 104 | |
| 105 | for (size_t x = 0; x < pos; x++) { |
| 106 | if (PyList_GET_ITEM(filters, x) == NULL) { |
| 107 | Py_DECREF(filters); |
| 108 | return NULL; |
| 109 | } |
| 110 | } |
| 111 | return filters; |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | /* Initialize the given warnings module state. */ |
| 116 | static int |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 117 | warnings_init_state(WarningsState *st) |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 118 | { |
| 119 | if (st->filters == NULL) { |
| 120 | st->filters = init_filters(); |
| 121 | if (st->filters == NULL) { |
| 122 | goto error; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (st->once_registry == NULL) { |
| 127 | st->once_registry = PyDict_New(); |
| 128 | if (st->once_registry == NULL) { |
| 129 | goto error; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (st->default_action == NULL) { |
| 134 | st->default_action = PyUnicode_FromString("default"); |
| 135 | if (st->default_action == NULL) { |
| 136 | goto error; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | st->filters_version = 0; |
| 141 | |
| 142 | return 0; |
| 143 | |
| 144 | error: |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 145 | warnings_clear_state(st); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /*************************************************************************/ |
| 151 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 152 | static int |
| 153 | check_matched(PyObject *obj, PyObject *arg) |
| 154 | { |
| 155 | PyObject *result; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 156 | _Py_IDENTIFIER(match); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 157 | int rc; |
| 158 | |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 159 | /* A 'None' filter always matches */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 160 | if (obj == Py_None) |
| 161 | return 1; |
Nick Coghlan | 9b99747 | 2018-01-08 12:45:02 +1000 | [diff] [blame] | 162 | |
| 163 | /* An internal plain text default filter must match exactly */ |
| 164 | if (PyUnicode_CheckExact(obj)) { |
| 165 | int cmp_result = PyUnicode_Compare(obj, arg); |
| 166 | if (cmp_result == -1 && PyErr_Occurred()) { |
| 167 | return -1; |
| 168 | } |
| 169 | return !cmp_result; |
| 170 | } |
| 171 | |
| 172 | /* Otherwise assume a regex filter and call its match() method */ |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 173 | result = _PyObject_CallMethodIdOneArg(obj, &PyId_match, arg); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 174 | if (result == NULL) |
| 175 | return -1; |
| 176 | |
| 177 | rc = PyObject_IsTrue(result); |
| 178 | Py_DECREF(result); |
| 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | Returns a new reference. |
| 184 | A NULL return value can mean false or an error. |
| 185 | */ |
| 186 | static PyObject * |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 187 | get_warnings_attr(_Py_Identifier *attr_id, int try_import) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 188 | { |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 189 | PyObject *warnings_str; |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 190 | PyObject *warnings_module, *obj; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 191 | _Py_IDENTIFIER(warnings); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 192 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 193 | warnings_str = _PyUnicode_FromId(&PyId_warnings); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 194 | if (warnings_str == NULL) { |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 195 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 198 | /* don't try to import after the start of the Python finallization */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 199 | if (try_import && !_Py_IsFinalizing()) { |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 200 | warnings_module = PyImport_Import(warnings_str); |
| 201 | if (warnings_module == NULL) { |
| 202 | /* Fallback to the C implementation if we cannot get |
| 203 | the Python implementation */ |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 204 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 205 | PyErr_Clear(); |
| 206 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 207 | return NULL; |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | else { |
Nathaniel J. Smith | dba976b | 2018-01-26 11:28:31 -0800 | [diff] [blame] | 211 | /* if we're so late into Python finalization that the module dict is |
| 212 | gone, then we can't even use PyImport_GetModule without triggering |
| 213 | an interpreter abort. |
| 214 | */ |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 215 | if (!_PyInterpreterState_GET()->modules) { |
Nathaniel J. Smith | dba976b | 2018-01-26 11:28:31 -0800 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 218 | warnings_module = PyImport_GetModule(warnings_str); |
Victor Stinner | 023654f | 2016-03-23 17:48:22 +0100 | [diff] [blame] | 219 | if (warnings_module == NULL) |
| 220 | return NULL; |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 223 | (void)_PyObject_LookupAttrId(warnings_module, attr_id, &obj); |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 224 | Py_DECREF(warnings_module); |
| 225 | return obj; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 229 | static PyObject * |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 230 | get_once_registry(WarningsState *st) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 231 | { |
| 232 | PyObject *registry; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 233 | _Py_IDENTIFIER(onceregistry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 234 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 235 | registry = get_warnings_attr(&PyId_onceregistry, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 236 | if (registry == NULL) { |
| 237 | if (PyErr_Occurred()) |
| 238 | return NULL; |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 239 | assert(st->once_registry); |
| 240 | return st->once_registry; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 241 | } |
Oren Milman | 252033d | 2017-09-11 09:28:39 +0300 | [diff] [blame] | 242 | if (!PyDict_Check(registry)) { |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 243 | PyErr_Format(PyExc_TypeError, |
| 244 | MODULE_NAME ".onceregistry must be a dict, " |
| 245 | "not '%.200s'", |
| 246 | Py_TYPE(registry)->tp_name); |
Oren Milman | 252033d | 2017-09-11 09:28:39 +0300 | [diff] [blame] | 247 | Py_DECREF(registry); |
| 248 | return NULL; |
| 249 | } |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 250 | Py_SETREF(st->once_registry, registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 251 | return registry; |
| 252 | } |
| 253 | |
| 254 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 255 | static PyObject * |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 256 | get_default_action(WarningsState *st) |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 257 | { |
| 258 | PyObject *default_action; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 259 | _Py_IDENTIFIER(defaultaction); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 260 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 261 | default_action = get_warnings_attr(&PyId_defaultaction, 0); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 262 | if (default_action == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | if (PyErr_Occurred()) { |
| 264 | return NULL; |
| 265 | } |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 266 | assert(st->default_action); |
| 267 | return st->default_action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 268 | } |
Oren Milman | 9d984fd | 2017-09-12 00:18:09 +0300 | [diff] [blame] | 269 | if (!PyUnicode_Check(default_action)) { |
| 270 | PyErr_Format(PyExc_TypeError, |
| 271 | MODULE_NAME ".defaultaction must be a string, " |
| 272 | "not '%.200s'", |
| 273 | Py_TYPE(default_action)->tp_name); |
| 274 | Py_DECREF(default_action); |
| 275 | return NULL; |
| 276 | } |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 277 | Py_SETREF(st->default_action, default_action); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 278 | return default_action; |
| 279 | } |
| 280 | |
| 281 | |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 282 | /* The item is a new reference. */ |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 283 | static PyObject* |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 284 | get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, |
| 285 | PyObject *module, PyObject **item) |
| 286 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 287 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 288 | Py_ssize_t i; |
| 289 | PyObject *warnings_filters; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 290 | _Py_IDENTIFIER(filters); |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 291 | WarningsState *st = warnings_get_state(); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 292 | if (st == NULL) { |
| 293 | return NULL; |
| 294 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 295 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 296 | warnings_filters = get_warnings_attr(&PyId_filters, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 297 | if (warnings_filters == NULL) { |
| 298 | if (PyErr_Occurred()) |
| 299 | return NULL; |
| 300 | } |
| 301 | else { |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 302 | Py_SETREF(st->filters, warnings_filters); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 305 | PyObject *filters = st->filters; |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 306 | if (filters == NULL || !PyList_Check(filters)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 307 | PyErr_SetString(PyExc_ValueError, |
| 308 | MODULE_NAME ".filters must be a list"); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 312 | /* WarningsState.filters could change while we are iterating over it. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 313 | for (i = 0; i < PyList_GET_SIZE(filters); i++) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 314 | PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; |
| 315 | Py_ssize_t ln; |
| 316 | int is_subclass, good_msg, good_mod; |
| 317 | |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 318 | tmp_item = PyList_GET_ITEM(filters, i); |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 319 | if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 320 | PyErr_Format(PyExc_ValueError, |
| 321 | MODULE_NAME ".filters item %zd isn't a 5-tuple", i); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | /* Python code: action, msg, cat, mod, ln = item */ |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 326 | Py_INCREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 327 | action = PyTuple_GET_ITEM(tmp_item, 0); |
| 328 | msg = PyTuple_GET_ITEM(tmp_item, 1); |
| 329 | cat = PyTuple_GET_ITEM(tmp_item, 2); |
| 330 | mod = PyTuple_GET_ITEM(tmp_item, 3); |
| 331 | ln_obj = PyTuple_GET_ITEM(tmp_item, 4); |
| 332 | |
Oren Milman | 9d984fd | 2017-09-12 00:18:09 +0300 | [diff] [blame] | 333 | if (!PyUnicode_Check(action)) { |
| 334 | PyErr_Format(PyExc_TypeError, |
| 335 | "action must be a string, not '%.200s'", |
| 336 | Py_TYPE(action)->tp_name); |
| 337 | Py_DECREF(tmp_item); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 341 | good_msg = check_matched(msg, text); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 342 | if (good_msg == -1) { |
| 343 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 344 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 345 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 346 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 347 | good_mod = check_matched(mod, module); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 348 | if (good_mod == -1) { |
| 349 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 350 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 351 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 352 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 353 | is_subclass = PyObject_IsSubclass(category, cat); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 354 | if (is_subclass == -1) { |
| 355 | Py_DECREF(tmp_item); |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 356 | return NULL; |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 357 | } |
Victor Stinner | 3cd04aa | 2013-10-31 14:46:00 +0100 | [diff] [blame] | 358 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 359 | ln = PyLong_AsSsize_t(ln_obj); |
Benjamin Peterson | 8c59816 | 2015-05-03 11:28:46 -0400 | [diff] [blame] | 360 | if (ln == -1 && PyErr_Occurred()) { |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 361 | Py_DECREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 362 | return NULL; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 363 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 364 | |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 365 | if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) { |
| 366 | *item = tmp_item; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 367 | return action; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | Py_DECREF(tmp_item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 373 | action = get_default_action(st); |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 374 | if (action != NULL) { |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 375 | Py_INCREF(Py_None); |
| 376 | *item = Py_None; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 377 | return action; |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 378 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 379 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
| 382 | |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 383 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 384 | static int |
| 385 | already_warned(PyObject *registry, PyObject *key, int should_set) |
| 386 | { |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 387 | PyObject *version_obj, *already_warned; |
| 388 | _Py_IDENTIFIER(version); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 389 | |
| 390 | if (key == NULL) |
| 391 | return -1; |
| 392 | |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 393 | WarningsState *st = warnings_get_state(); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 394 | if (st == NULL) { |
| 395 | return -1; |
| 396 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 397 | version_obj = _PyDict_GetItemIdWithError(registry, &PyId_version); |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 398 | if (version_obj == NULL |
| 399 | || !PyLong_CheckExact(version_obj) |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 400 | || PyLong_AsLong(version_obj) != st->filters_version) |
Serhiy Storchaka | 8905fcc | 2018-12-11 08:38:03 +0200 | [diff] [blame] | 401 | { |
| 402 | if (PyErr_Occurred()) { |
| 403 | return -1; |
| 404 | } |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 405 | PyDict_Clear(registry); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 406 | version_obj = PyLong_FromLong(st->filters_version); |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 407 | if (version_obj == NULL) |
| 408 | return -1; |
| 409 | if (_PyDict_SetItemId(registry, &PyId_version, version_obj) < 0) { |
| 410 | Py_DECREF(version_obj); |
| 411 | return -1; |
| 412 | } |
| 413 | Py_DECREF(version_obj); |
| 414 | } |
| 415 | else { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 416 | already_warned = PyDict_GetItemWithError(registry, key); |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 417 | if (already_warned != NULL) { |
| 418 | int rc = PyObject_IsTrue(already_warned); |
| 419 | if (rc != 0) |
| 420 | return rc; |
| 421 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 422 | else if (PyErr_Occurred()) { |
| 423 | return -1; |
| 424 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* This warning wasn't found in the registry, set it. */ |
| 428 | if (should_set) |
| 429 | return PyDict_SetItem(registry, key, Py_True); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* New reference. */ |
| 434 | static PyObject * |
| 435 | normalize_module(PyObject *filename) |
| 436 | { |
| 437 | PyObject *module; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 438 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 439 | const void *data; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 440 | Py_ssize_t len; |
| 441 | |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 442 | len = PyUnicode_GetLength(filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 443 | if (len < 0) |
| 444 | return NULL; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 445 | |
| 446 | if (len == 0) |
| 447 | return PyUnicode_FromString("<unknown>"); |
| 448 | |
| 449 | kind = PyUnicode_KIND(filename); |
| 450 | data = PyUnicode_DATA(filename); |
| 451 | |
| 452 | /* if filename.endswith(".py"): */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 453 | if (len >= 3 && |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 454 | PyUnicode_READ(kind, data, len-3) == '.' && |
| 455 | PyUnicode_READ(kind, data, len-2) == 'p' && |
| 456 | PyUnicode_READ(kind, data, len-1) == 'y') |
| 457 | { |
Victor Stinner | 9e30aa5 | 2011-11-21 02:49:52 +0100 | [diff] [blame] | 458 | module = PyUnicode_Substring(filename, 0, len-3); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 459 | } |
| 460 | else { |
| 461 | module = filename; |
| 462 | Py_INCREF(module); |
| 463 | } |
| 464 | return module; |
| 465 | } |
| 466 | |
| 467 | static int |
| 468 | update_registry(PyObject *registry, PyObject *text, PyObject *category, |
| 469 | int add_zero) |
| 470 | { |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 471 | PyObject *altkey; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 472 | int rc; |
| 473 | |
Serhiy Storchaka | ba85d69 | 2017-03-30 09:09:41 +0300 | [diff] [blame] | 474 | if (add_zero) |
| 475 | altkey = PyTuple_Pack(3, text, category, _PyLong_Zero); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 476 | else |
| 477 | altkey = PyTuple_Pack(2, text, category); |
| 478 | |
| 479 | rc = already_warned(registry, altkey, 1); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 480 | Py_XDECREF(altkey); |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | static void |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 485 | show_warning(PyObject *filename, int lineno, PyObject *text, |
| 486 | PyObject *category, PyObject *sourceline) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | PyObject *f_stderr; |
| 489 | PyObject *name; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 490 | char lineno_str[128]; |
| 491 | |
| 492 | PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); |
| 493 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 494 | name = _PyObject_GetAttrId(category, &PyId___name__); |
Hai Shi | 57c7810 | 2020-03-14 21:40:58 +0800 | [diff] [blame] | 495 | if (name == NULL) { |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 496 | goto error; |
Hai Shi | 57c7810 | 2020-03-14 21:40:58 +0800 | [diff] [blame] | 497 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 498 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 499 | f_stderr = _PySys_GetObjectId(&PyId_stderr); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 500 | if (f_stderr == NULL) { |
| 501 | fprintf(stderr, "lost sys.stderr\n"); |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 502 | goto error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* Print "filename:lineno: category: text\n" */ |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 506 | if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0) |
| 507 | goto error; |
| 508 | if (PyFile_WriteString(lineno_str, f_stderr) < 0) |
| 509 | goto error; |
| 510 | if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0) |
| 511 | goto error; |
| 512 | if (PyFile_WriteString(": ", f_stderr) < 0) |
| 513 | goto error; |
| 514 | if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0) |
| 515 | goto error; |
| 516 | if (PyFile_WriteString("\n", f_stderr) < 0) |
| 517 | goto error; |
| 518 | Py_CLEAR(name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 519 | |
| 520 | /* Print " source_line\n" */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 521 | if (sourceline) { |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 522 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 523 | const void *data; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 524 | Py_ssize_t i, len; |
| 525 | Py_UCS4 ch; |
| 526 | PyObject *truncated; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 527 | |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 528 | if (PyUnicode_READY(sourceline) < 1) |
| 529 | goto error; |
| 530 | |
| 531 | kind = PyUnicode_KIND(sourceline); |
| 532 | data = PyUnicode_DATA(sourceline); |
| 533 | len = PyUnicode_GET_LENGTH(sourceline); |
| 534 | for (i=0; i<len; i++) { |
| 535 | ch = PyUnicode_READ(kind, data, i); |
| 536 | if (ch != ' ' && ch != '\t' && ch != '\014') |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | truncated = PyUnicode_Substring(sourceline, i, len); |
| 541 | if (truncated == NULL) |
| 542 | goto error; |
| 543 | |
| 544 | PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW); |
| 545 | Py_DECREF(truncated); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 546 | PyFile_WriteString("\n", f_stderr); |
| 547 | } |
Victor Stinner | 78e2c98 | 2013-07-16 01:54:37 +0200 | [diff] [blame] | 548 | else { |
| 549 | _Py_DisplaySourceLine(f_stderr, filename, lineno, 2); |
| 550 | } |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 551 | |
| 552 | error: |
Victor Stinner | ae233ea | 2013-10-31 14:51:38 +0100 | [diff] [blame] | 553 | Py_XDECREF(name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 554 | PyErr_Clear(); |
| 555 | } |
| 556 | |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 557 | static int |
| 558 | call_show_warning(PyObject *category, PyObject *text, PyObject *message, |
| 559 | PyObject *filename, int lineno, PyObject *lineno_obj, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 560 | PyObject *sourceline, PyObject *source) |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 561 | { |
| 562 | PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 563 | _Py_IDENTIFIER(_showwarnmsg); |
| 564 | _Py_IDENTIFIER(WarningMessage); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 565 | |
Victor Stinner | e98445a | 2016-03-23 00:54:48 +0100 | [diff] [blame] | 566 | /* If the source parameter is set, try to get the Python implementation. |
| 567 | The Python implementation is able to log the traceback where the source |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 568 | was allocated, whereas the C implementation doesn't. */ |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 569 | show_fn = get_warnings_attr(&PyId__showwarnmsg, source != NULL); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 570 | if (show_fn == NULL) { |
| 571 | if (PyErr_Occurred()) |
| 572 | return -1; |
| 573 | show_warning(filename, lineno, text, category, sourceline); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | if (!PyCallable_Check(show_fn)) { |
| 578 | PyErr_SetString(PyExc_TypeError, |
| 579 | "warnings._showwarnmsg() must be set to a callable"); |
| 580 | goto error; |
| 581 | } |
| 582 | |
Victor Stinner | 8265627 | 2017-11-22 23:51:42 +0100 | [diff] [blame] | 583 | warnmsg_cls = get_warnings_attr(&PyId_WarningMessage, 0); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 584 | if (warnmsg_cls == NULL) { |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 585 | if (!PyErr_Occurred()) { |
| 586 | PyErr_SetString(PyExc_RuntimeError, |
| 587 | "unable to get warnings.WarningMessage"); |
| 588 | } |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 589 | goto error; |
| 590 | } |
| 591 | |
| 592 | msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 593 | filename, lineno_obj, Py_None, Py_None, source, |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 594 | NULL); |
| 595 | Py_DECREF(warnmsg_cls); |
| 596 | if (msg == NULL) |
| 597 | goto error; |
| 598 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 599 | res = PyObject_CallOneArg(show_fn, msg); |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 600 | Py_DECREF(show_fn); |
| 601 | Py_DECREF(msg); |
| 602 | |
| 603 | if (res == NULL) |
| 604 | return -1; |
| 605 | |
| 606 | Py_DECREF(res); |
| 607 | return 0; |
| 608 | |
| 609 | error: |
| 610 | Py_XDECREF(show_fn); |
| 611 | return -1; |
| 612 | } |
| 613 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 614 | static PyObject * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | warn_explicit(PyObject *category, PyObject *message, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 616 | PyObject *filename, int lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 617 | PyObject *module, PyObject *registry, PyObject *sourceline, |
| 618 | PyObject *source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 619 | { |
| 620 | PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 621 | PyObject *item = NULL; |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 622 | PyObject *action; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 623 | int rc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | |
Victor Stinner | dcdd05b | 2013-11-01 00:55:30 +0100 | [diff] [blame] | 625 | /* module can be None if a warning is emitted late during Python shutdown. |
| 626 | In this case, the Python warnings module was probably unloaded, filters |
| 627 | are no more available to choose as action. It is safer to ignore the |
| 628 | warning and do nothing. */ |
| 629 | if (module == Py_None) |
| 630 | Py_RETURN_NONE; |
| 631 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 632 | if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
Oren Milman | 252033d | 2017-09-11 09:28:39 +0300 | [diff] [blame] | 633 | PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None"); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 634 | return NULL; |
| 635 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 636 | |
| 637 | /* Normalize module. */ |
| 638 | if (module == NULL) { |
| 639 | module = normalize_module(filename); |
| 640 | if (module == NULL) |
| 641 | return NULL; |
| 642 | } |
| 643 | else |
| 644 | Py_INCREF(module); |
| 645 | |
| 646 | /* Normalize message. */ |
| 647 | Py_INCREF(message); /* DECREF'ed in cleanup. */ |
| 648 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 649 | if (rc == -1) { |
| 650 | goto cleanup; |
| 651 | } |
| 652 | if (rc == 1) { |
| 653 | text = PyObject_Str(message); |
Hirokazu Yamamoto | 1c0c003 | 2009-07-17 06:55:42 +0000 | [diff] [blame] | 654 | if (text == NULL) |
| 655 | goto cleanup; |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 656 | category = (PyObject*)Py_TYPE(message); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 657 | } |
| 658 | else { |
| 659 | text = message; |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 660 | message = PyObject_CallOneArg(category, message); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 661 | if (message == NULL) |
| 662 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | lineno_obj = PyLong_FromLong(lineno); |
| 666 | if (lineno_obj == NULL) |
| 667 | goto cleanup; |
| 668 | |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 669 | if (source == Py_None) { |
| 670 | source = NULL; |
| 671 | } |
| 672 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 673 | /* Create key. */ |
| 674 | key = PyTuple_Pack(3, text, category, lineno_obj); |
| 675 | if (key == NULL) |
| 676 | goto cleanup; |
| 677 | |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 678 | if ((registry != NULL) && (registry != Py_None)) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 679 | rc = already_warned(registry, key, 0); |
| 680 | if (rc == -1) |
| 681 | goto cleanup; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | else if (rc == 1) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 683 | goto return_none; |
| 684 | /* Else this warning hasn't been generated before. */ |
| 685 | } |
| 686 | |
| 687 | action = get_filter(category, text, lineno, module, &item); |
| 688 | if (action == NULL) |
| 689 | goto cleanup; |
| 690 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 691 | if (_PyUnicode_EqualToASCIIString(action, "error")) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 692 | PyErr_SetObject(category, message); |
| 693 | goto cleanup; |
| 694 | } |
| 695 | |
Victor Stinner | c975878 | 2017-11-27 16:57:07 +0100 | [diff] [blame] | 696 | if (_PyUnicode_EqualToASCIIString(action, "ignore")) { |
| 697 | goto return_none; |
| 698 | } |
| 699 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 700 | /* Store in the registry that we've been here, *except* when the action |
| 701 | is "always". */ |
| 702 | rc = 0; |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 703 | if (!_PyUnicode_EqualToASCIIString(action, "always")) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 704 | if (registry != NULL && registry != Py_None && |
Victor Stinner | c975878 | 2017-11-27 16:57:07 +0100 | [diff] [blame] | 705 | PyDict_SetItem(registry, key, Py_True) < 0) |
| 706 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 707 | goto cleanup; |
Victor Stinner | c975878 | 2017-11-27 16:57:07 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | if (_PyUnicode_EqualToASCIIString(action, "once")) { |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 711 | if (registry == NULL || registry == Py_None) { |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 712 | WarningsState *st = warnings_get_state(); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 713 | if (st == NULL) { |
| 714 | goto cleanup; |
| 715 | } |
| 716 | registry = get_once_registry(st); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 717 | if (registry == NULL) |
| 718 | goto cleanup; |
| 719 | } |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 720 | /* WarningsState.once_registry[(text, category)] = 1 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 722 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 723 | else if (_PyUnicode_EqualToASCIIString(action, "module")) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 724 | /* registry[(text, category, 0)] = 1 */ |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 725 | if (registry != NULL && registry != Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | rc = update_registry(registry, text, category, 0); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 727 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 728 | else if (!_PyUnicode_EqualToASCIIString(action, "default")) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 729 | PyErr_Format(PyExc_RuntimeError, |
Victor Stinner | a4c704b | 2013-10-29 23:43:41 +0100 | [diff] [blame] | 730 | "Unrecognized action (%R) in warnings.filters:\n %R", |
| 731 | action, item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 732 | goto cleanup; |
| 733 | } |
| 734 | } |
| 735 | |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 736 | if (rc == 1) /* Already warned for this module. */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 737 | goto return_none; |
| 738 | if (rc == 0) { |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 739 | if (call_show_warning(category, text, message, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 740 | lineno_obj, sourceline, source) < 0) |
Victor Stinner | 1231a46 | 2016-03-19 00:47:17 +0100 | [diff] [blame] | 741 | goto cleanup; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 742 | } |
| 743 | else /* if (rc == -1) */ |
| 744 | goto cleanup; |
| 745 | |
| 746 | return_none: |
| 747 | result = Py_None; |
| 748 | Py_INCREF(result); |
| 749 | |
| 750 | cleanup: |
Benjamin Peterson | deff2b7 | 2015-05-03 11:23:37 -0400 | [diff] [blame] | 751 | Py_XDECREF(item); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 752 | Py_XDECREF(key); |
| 753 | Py_XDECREF(text); |
| 754 | Py_XDECREF(lineno_obj); |
| 755 | Py_DECREF(module); |
Brett Cannon | db73491 | 2008-06-27 00:52:15 +0000 | [diff] [blame] | 756 | Py_XDECREF(message); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 757 | return result; /* Py_None or NULL. */ |
| 758 | } |
| 759 | |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 760 | static int |
| 761 | is_internal_frame(PyFrameObject *frame) |
| 762 | { |
| 763 | static PyObject *importlib_string = NULL; |
| 764 | static PyObject *bootstrap_string = NULL; |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 765 | int contains; |
| 766 | |
| 767 | if (importlib_string == NULL) { |
| 768 | importlib_string = PyUnicode_FromString("importlib"); |
| 769 | if (importlib_string == NULL) { |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | bootstrap_string = PyUnicode_FromString("_bootstrap"); |
| 774 | if (bootstrap_string == NULL) { |
| 775 | Py_DECREF(importlib_string); |
| 776 | return 0; |
| 777 | } |
| 778 | Py_INCREF(importlib_string); |
| 779 | Py_INCREF(bootstrap_string); |
| 780 | } |
| 781 | |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 782 | if (frame == NULL) { |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 785 | |
| 786 | PyCodeObject *code = PyFrame_GetCode(frame); |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 787 | PyObject *filename = code->co_filename; |
Victor Stinner | 8852ad4 | 2020-04-29 01:28:13 +0200 | [diff] [blame] | 788 | Py_DECREF(code); |
| 789 | |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 790 | if (filename == NULL) { |
| 791 | return 0; |
| 792 | } |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 793 | if (!PyUnicode_Check(filename)) { |
| 794 | return 0; |
| 795 | } |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 796 | |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 797 | contains = PyUnicode_Contains(filename, importlib_string); |
| 798 | if (contains < 0) { |
| 799 | return 0; |
| 800 | } |
| 801 | else if (contains > 0) { |
| 802 | contains = PyUnicode_Contains(filename, bootstrap_string); |
| 803 | if (contains < 0) { |
| 804 | return 0; |
| 805 | } |
| 806 | else if (contains > 0) { |
| 807 | return 1; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static PyFrameObject * |
| 815 | next_external_frame(PyFrameObject *frame) |
| 816 | { |
| 817 | do { |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 818 | PyFrameObject *back = PyFrame_GetBack(frame); |
| 819 | Py_DECREF(frame); |
| 820 | frame = back; |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 821 | } while (frame != NULL && is_internal_frame(frame)); |
| 822 | |
| 823 | return frame; |
| 824 | } |
| 825 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 826 | /* filename, module, and registry are new refs, globals is borrowed */ |
| 827 | /* Returns 0 on error (no new refs), 1 on success */ |
| 828 | static int |
| 829 | setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, |
| 830 | PyObject **module, PyObject **registry) |
| 831 | { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 832 | _Py_IDENTIFIER(__warningregistry__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 833 | PyObject *globals; |
| 834 | |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 835 | /* Setup globals, filename and lineno. */ |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 836 | PyThreadState *tstate = _PyThreadState_GET(); |
| 837 | PyFrameObject *f = PyThreadState_GetFrame(tstate); |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 838 | // Stack level comparisons to Python code is off by one as there is no |
| 839 | // warnings-related stack level to avoid. |
| 840 | if (stack_level <= 0 || is_internal_frame(f)) { |
| 841 | while (--stack_level > 0 && f != NULL) { |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 842 | PyFrameObject *back = PyFrame_GetBack(f); |
| 843 | Py_DECREF(f); |
| 844 | f = back; |
Larry Hastings | 714e493 | 2015-09-06 00:39:37 -0700 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | else { |
| 848 | while (--stack_level > 0 && f != NULL) { |
| 849 | f = next_external_frame(f); |
| 850 | } |
| 851 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 852 | |
| 853 | if (f == NULL) { |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 854 | globals = _PyInterpreterState_GET()->sysdict; |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 855 | *filename = PyUnicode_FromString("sys"); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 856 | *lineno = 1; |
| 857 | } |
| 858 | else { |
| 859 | globals = f->f_globals; |
Victor Stinner | 8852ad4 | 2020-04-29 01:28:13 +0200 | [diff] [blame] | 860 | PyCodeObject *code = PyFrame_GetCode(f); |
| 861 | *filename = code->co_filename; |
| 862 | Py_DECREF(code); |
Thomas Kluyver | 11a8966 | 2018-06-08 21:28:37 +0200 | [diff] [blame] | 863 | Py_INCREF(*filename); |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 864 | *lineno = PyFrame_GetLineNumber(f); |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 865 | Py_DECREF(f); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | *module = NULL; |
| 869 | |
| 870 | /* Setup registry. */ |
| 871 | assert(globals != NULL); |
| 872 | assert(PyDict_Check(globals)); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 873 | *registry = _PyDict_GetItemIdWithError(globals, &PyId___warningregistry__); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 874 | if (*registry == NULL) { |
| 875 | int rc; |
| 876 | |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 877 | if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | 2d2f855 | 2020-03-02 22:05:08 +0200 | [diff] [blame] | 878 | goto handle_error; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 879 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 880 | *registry = PyDict_New(); |
| 881 | if (*registry == NULL) |
Serhiy Storchaka | 2d2f855 | 2020-03-02 22:05:08 +0200 | [diff] [blame] | 882 | goto handle_error; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 883 | |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 884 | rc = _PyDict_SetItemId(globals, &PyId___warningregistry__, *registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 885 | if (rc < 0) |
| 886 | goto handle_error; |
| 887 | } |
| 888 | else |
| 889 | Py_INCREF(*registry); |
| 890 | |
| 891 | /* Setup module. */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 892 | *module = _PyDict_GetItemIdWithError(globals, &PyId___name__); |
Oren Milman | 5d3e800 | 2017-09-24 21:28:42 +0300 | [diff] [blame] | 893 | if (*module == Py_None || (*module != NULL && PyUnicode_Check(*module))) { |
| 894 | Py_INCREF(*module); |
| 895 | } |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 896 | else if (_PyErr_Occurred(tstate)) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 897 | goto handle_error; |
| 898 | } |
Oren Milman | 5d3e800 | 2017-09-24 21:28:42 +0300 | [diff] [blame] | 899 | else { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 900 | *module = PyUnicode_FromString("<string>"); |
| 901 | if (*module == NULL) |
| 902 | goto handle_error; |
| 903 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 904 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 905 | return 1; |
| 906 | |
| 907 | handle_error: |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 908 | Py_XDECREF(*registry); |
| 909 | Py_XDECREF(*module); |
Serhiy Storchaka | ae75a29 | 2020-03-03 19:43:29 +0200 | [diff] [blame] | 910 | Py_DECREF(*filename); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static PyObject * |
| 915 | get_category(PyObject *message, PyObject *category) |
| 916 | { |
| 917 | int rc; |
| 918 | |
| 919 | /* Get category. */ |
| 920 | rc = PyObject_IsInstance(message, PyExc_Warning); |
| 921 | if (rc == -1) |
| 922 | return NULL; |
| 923 | |
| 924 | if (rc == 1) |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 925 | category = (PyObject*)Py_TYPE(message); |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 926 | else if (category == NULL || category == Py_None) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 927 | category = PyExc_UserWarning; |
| 928 | |
| 929 | /* Validate category. */ |
| 930 | rc = PyObject_IsSubclass(category, PyExc_Warning); |
Berker Peksag | d8089e0 | 2014-07-11 19:50:25 +0300 | [diff] [blame] | 931 | /* category is not a subclass of PyExc_Warning or |
| 932 | PyObject_IsSubclass raised an error */ |
| 933 | if (rc == -1 || rc == 0) { |
| 934 | PyErr_Format(PyExc_TypeError, |
| 935 | "category must be a Warning subclass, not '%s'", |
| 936 | Py_TYPE(category)->tp_name); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 937 | return NULL; |
| 938 | } |
| 939 | |
| 940 | return category; |
| 941 | } |
| 942 | |
| 943 | static PyObject * |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 944 | do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level, |
| 945 | PyObject *source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 946 | { |
| 947 | PyObject *filename, *module, *registry, *res; |
| 948 | int lineno; |
| 949 | |
| 950 | if (!setup_context(stack_level, &filename, &lineno, &module, ®istry)) |
| 951 | return NULL; |
| 952 | |
Victor Stinner | dcdd05b | 2013-11-01 00:55:30 +0100 | [diff] [blame] | 953 | res = warn_explicit(category, message, filename, lineno, module, registry, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 954 | NULL, source); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 955 | Py_DECREF(filename); |
| 956 | Py_DECREF(registry); |
| 957 | Py_DECREF(module); |
| 958 | return res; |
| 959 | } |
| 960 | |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 961 | /*[clinic input] |
| 962 | warn as warnings_warn |
| 963 | |
| 964 | message: object |
| 965 | category: object = None |
| 966 | stacklevel: Py_ssize_t = 1 |
| 967 | source: object = None |
| 968 | |
| 969 | Issue a warning, or maybe ignore it or raise an exception. |
| 970 | [clinic start generated code]*/ |
| 971 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 972 | static PyObject * |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 973 | warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category, |
| 974 | Py_ssize_t stacklevel, PyObject *source) |
| 975 | /*[clinic end generated code: output=31ed5ab7d8d760b2 input=bfdf5cf99f6c4edd]*/ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 976 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 977 | category = get_category(message, category); |
| 978 | if (category == NULL) |
| 979 | return NULL; |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 980 | return do_warn(message, category, stacklevel, source); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static PyObject * |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 984 | get_source_line(PyObject *module_globals, int lineno) |
| 985 | { |
| 986 | _Py_IDENTIFIER(get_source); |
| 987 | _Py_IDENTIFIER(__loader__); |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 988 | PyObject *loader; |
| 989 | PyObject *module_name; |
| 990 | PyObject *get_source; |
| 991 | PyObject *source; |
| 992 | PyObject *source_list; |
| 993 | PyObject *source_line; |
| 994 | |
| 995 | /* Check/get the requisite pieces needed for the loader. */ |
| 996 | loader = _PyDict_GetItemIdWithError(module_globals, &PyId___loader__); |
| 997 | if (loader == NULL) { |
| 998 | return NULL; |
| 999 | } |
| 1000 | Py_INCREF(loader); |
| 1001 | module_name = _PyDict_GetItemIdWithError(module_globals, &PyId___name__); |
| 1002 | if (!module_name) { |
| 1003 | Py_DECREF(loader); |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | Py_INCREF(module_name); |
| 1007 | |
| 1008 | /* Make sure the loader implements the optional get_source() method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1009 | (void)_PyObject_LookupAttrId(loader, &PyId_get_source, &get_source); |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 1010 | Py_DECREF(loader); |
| 1011 | if (!get_source) { |
| 1012 | Py_DECREF(module_name); |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 1013 | return NULL; |
| 1014 | } |
| 1015 | /* Call get_source() to get the source code. */ |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1016 | source = PyObject_CallOneArg(get_source, module_name); |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 1017 | Py_DECREF(get_source); |
| 1018 | Py_DECREF(module_name); |
| 1019 | if (!source) { |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | if (source == Py_None) { |
| 1023 | Py_DECREF(source); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | /* Split the source into lines. */ |
| 1028 | source_list = PyUnicode_Splitlines(source, 0); |
| 1029 | Py_DECREF(source); |
| 1030 | if (!source_list) { |
| 1031 | return NULL; |
| 1032 | } |
| 1033 | |
| 1034 | /* Get the source line. */ |
| 1035 | source_line = PyList_GetItem(source_list, lineno-1); |
| 1036 | Py_XINCREF(source_line); |
| 1037 | Py_DECREF(source_list); |
| 1038 | return source_line; |
| 1039 | } |
| 1040 | |
| 1041 | static PyObject * |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1042 | warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) |
| 1043 | { |
| 1044 | static char *kwd_list[] = {"message", "category", "filename", "lineno", |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1045 | "module", "registry", "module_globals", |
| 1046 | "source", 0}; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1047 | PyObject *message; |
| 1048 | PyObject *category; |
| 1049 | PyObject *filename; |
| 1050 | int lineno; |
| 1051 | PyObject *module = NULL; |
| 1052 | PyObject *registry = NULL; |
| 1053 | PyObject *module_globals = NULL; |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1054 | PyObject *sourceobj = NULL; |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 1055 | PyObject *source_line = NULL; |
| 1056 | PyObject *returned; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1057 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1058 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOUi|OOOO:warn_explicit", |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1059 | kwd_list, &message, &category, &filename, &lineno, &module, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1060 | ®istry, &module_globals, &sourceobj)) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1061 | return NULL; |
| 1062 | |
Victor Stinner | b056562 | 2018-05-15 20:42:12 +0200 | [diff] [blame] | 1063 | if (module_globals && module_globals != Py_None) { |
| 1064 | if (!PyDict_Check(module_globals)) { |
| 1065 | PyErr_Format(PyExc_TypeError, |
| 1066 | "module_globals must be a dict, not '%.200s'", |
| 1067 | Py_TYPE(module_globals)->tp_name); |
| 1068 | return NULL; |
| 1069 | } |
| 1070 | |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 1071 | source_line = get_source_line(module_globals, lineno); |
| 1072 | if (source_line == NULL && PyErr_Occurred()) { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1073 | return NULL; |
| 1074 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1075 | } |
Serhiy Storchaka | d4f8480 | 2017-11-11 15:19:47 +0200 | [diff] [blame] | 1076 | returned = warn_explicit(category, message, filename, lineno, module, |
| 1077 | registry, source_line, sourceobj); |
| 1078 | Py_XDECREF(source_line); |
| 1079 | return returned; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1082 | static PyObject * |
| 1083 | warnings_filters_mutated(PyObject *self, PyObject *args) |
| 1084 | { |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 1085 | WarningsState *st = warnings_get_state(); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1086 | if (st == NULL) { |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | st->filters_version++; |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1090 | Py_RETURN_NONE; |
| 1091 | } |
| 1092 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1093 | |
| 1094 | /* Function to issue a warning message; may raise an exception. */ |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1095 | |
| 1096 | static int |
| 1097 | warn_unicode(PyObject *category, PyObject *message, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1098 | Py_ssize_t stack_level, PyObject *source) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1099 | { |
| 1100 | PyObject *res; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1101 | |
| 1102 | if (category == NULL) |
| 1103 | category = PyExc_RuntimeWarning; |
| 1104 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1105 | res = do_warn(message, category, stack_level, source); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1106 | if (res == NULL) |
| 1107 | return -1; |
| 1108 | Py_DECREF(res); |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1113 | static int |
| 1114 | _PyErr_WarnFormatV(PyObject *source, |
| 1115 | PyObject *category, Py_ssize_t stack_level, |
| 1116 | const char *format, va_list vargs) |
| 1117 | { |
| 1118 | PyObject *message; |
| 1119 | int res; |
| 1120 | |
| 1121 | message = PyUnicode_FromFormatV(format, vargs); |
| 1122 | if (message == NULL) |
| 1123 | return -1; |
| 1124 | |
| 1125 | res = warn_unicode(category, message, stack_level, source); |
| 1126 | Py_DECREF(message); |
| 1127 | return res; |
| 1128 | } |
| 1129 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1130 | int |
| 1131 | PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, |
| 1132 | const char *format, ...) |
| 1133 | { |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1134 | int res; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1135 | va_list vargs; |
| 1136 | |
| 1137 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1138 | va_start(vargs, format); |
| 1139 | #else |
| 1140 | va_start(vargs); |
| 1141 | #endif |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1142 | res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs); |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1143 | va_end(vargs); |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1144 | return res; |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Victor Stinner | 8d84adc | 2020-03-31 17:25:12 +0200 | [diff] [blame] | 1147 | static int |
| 1148 | _PyErr_WarnFormat(PyObject *source, PyObject *category, Py_ssize_t stack_level, |
| 1149 | const char *format, ...) |
| 1150 | { |
| 1151 | int res; |
| 1152 | va_list vargs; |
| 1153 | |
| 1154 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1155 | va_start(vargs, format); |
| 1156 | #else |
| 1157 | va_start(vargs); |
| 1158 | #endif |
| 1159 | res = _PyErr_WarnFormatV(source, category, stack_level, format, vargs); |
| 1160 | va_end(vargs); |
| 1161 | return res; |
| 1162 | } |
| 1163 | |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1164 | int |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1165 | PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, |
| 1166 | const char *format, ...) |
| 1167 | { |
| 1168 | int res; |
| 1169 | va_list vargs; |
| 1170 | |
| 1171 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1172 | va_start(vargs, format); |
| 1173 | #else |
| 1174 | va_start(vargs); |
| 1175 | #endif |
| 1176 | res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning, |
| 1177 | stack_level, format, vargs); |
| 1178 | va_end(vargs); |
| 1179 | return res; |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | int |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1184 | PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
| 1185 | { |
| 1186 | int ret; |
| 1187 | PyObject *message = PyUnicode_FromString(text); |
| 1188 | if (message == NULL) |
| 1189 | return -1; |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1190 | ret = warn_unicode(category, message, stack_level, NULL); |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 1191 | Py_DECREF(message); |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1195 | /* PyErr_Warn is only for backwards compatibility and will be removed. |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1196 | Use PyErr_WarnEx instead. */ |
| 1197 | |
| 1198 | #undef PyErr_Warn |
| 1199 | |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 1200 | int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1201 | PyErr_Warn(PyObject *category, const char *text) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1202 | { |
| 1203 | return PyErr_WarnEx(category, text, 1); |
| 1204 | } |
| 1205 | |
| 1206 | /* Warning with explicit origin */ |
| 1207 | int |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1208 | PyErr_WarnExplicitObject(PyObject *category, PyObject *message, |
| 1209 | PyObject *filename, int lineno, |
| 1210 | PyObject *module, PyObject *registry) |
| 1211 | { |
| 1212 | PyObject *res; |
| 1213 | if (category == NULL) |
| 1214 | category = PyExc_RuntimeWarning; |
| 1215 | res = warn_explicit(category, message, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1216 | module, registry, NULL, NULL); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1217 | if (res == NULL) |
| 1218 | return -1; |
| 1219 | Py_DECREF(res); |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | int |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1224 | PyErr_WarnExplicit(PyObject *category, const char *text, |
| 1225 | const char *filename_str, int lineno, |
| 1226 | const char *module_str, PyObject *registry) |
| 1227 | { |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1228 | PyObject *message = PyUnicode_FromString(text); |
Victor Stinner | cb428f0 | 2010-12-27 20:10:36 +0000 | [diff] [blame] | 1229 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1230 | PyObject *module = NULL; |
| 1231 | int ret = -1; |
| 1232 | |
| 1233 | if (message == NULL || filename == NULL) |
| 1234 | goto exit; |
| 1235 | if (module_str != NULL) { |
| 1236 | module = PyUnicode_FromString(module_str); |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1237 | if (module == NULL) |
| 1238 | goto exit; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1241 | ret = PyErr_WarnExplicitObject(category, message, filename, lineno, |
| 1242 | module, registry); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1243 | |
| 1244 | exit: |
| 1245 | Py_XDECREF(message); |
| 1246 | Py_XDECREF(module); |
| 1247 | Py_XDECREF(filename); |
| 1248 | return ret; |
| 1249 | } |
| 1250 | |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1251 | int |
| 1252 | PyErr_WarnExplicitFormat(PyObject *category, |
| 1253 | const char *filename_str, int lineno, |
| 1254 | const char *module_str, PyObject *registry, |
| 1255 | const char *format, ...) |
| 1256 | { |
| 1257 | PyObject *message; |
| 1258 | PyObject *module = NULL; |
| 1259 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
| 1260 | int ret = -1; |
| 1261 | va_list vargs; |
| 1262 | |
| 1263 | if (filename == NULL) |
| 1264 | goto exit; |
| 1265 | if (module_str != NULL) { |
| 1266 | module = PyUnicode_FromString(module_str); |
| 1267 | if (module == NULL) |
| 1268 | goto exit; |
| 1269 | } |
| 1270 | |
| 1271 | #ifdef HAVE_STDARG_PROTOTYPES |
| 1272 | va_start(vargs, format); |
| 1273 | #else |
| 1274 | va_start(vargs); |
| 1275 | #endif |
| 1276 | message = PyUnicode_FromFormatV(format, vargs); |
| 1277 | if (message != NULL) { |
| 1278 | PyObject *res; |
| 1279 | res = warn_explicit(category, message, filename, lineno, |
Victor Stinner | 914cde8 | 2016-03-19 01:03:51 +0100 | [diff] [blame] | 1280 | module, registry, NULL, NULL); |
Antoine Pitrou | 070cb3c | 2013-05-08 13:23:25 +0200 | [diff] [blame] | 1281 | Py_DECREF(message); |
| 1282 | if (res != NULL) { |
| 1283 | Py_DECREF(res); |
| 1284 | ret = 0; |
| 1285 | } |
| 1286 | } |
| 1287 | va_end(vargs); |
| 1288 | exit: |
| 1289 | Py_XDECREF(module); |
| 1290 | Py_XDECREF(filename); |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1294 | void |
| 1295 | _PyErr_WarnUnawaitedCoroutine(PyObject *coro) |
| 1296 | { |
| 1297 | /* First, we attempt to funnel the warning through |
| 1298 | warnings._warn_unawaited_coroutine. |
| 1299 | |
| 1300 | This could raise an exception, due to: |
| 1301 | - a bug |
| 1302 | - some kind of shutdown-related brokenness |
| 1303 | - succeeding, but with an "error" warning filter installed, so the |
| 1304 | warning is converted into a RuntimeWarning exception |
| 1305 | |
| 1306 | In the first two cases, we want to print the error (so we know what it |
| 1307 | is!), and then print a warning directly as a fallback. In the last |
| 1308 | case, we want to print the error (since it's the warning!), but *not* |
| 1309 | do a fallback. And after we print the error we can't check for what |
| 1310 | type of error it was (because PyErr_WriteUnraisable clears it), so we |
| 1311 | need a flag to keep track. |
| 1312 | |
| 1313 | Since this is called from __del__ context, it's careful to never raise |
| 1314 | an exception. |
| 1315 | */ |
| 1316 | _Py_IDENTIFIER(_warn_unawaited_coroutine); |
| 1317 | int warned = 0; |
| 1318 | PyObject *fn = get_warnings_attr(&PyId__warn_unawaited_coroutine, 1); |
| 1319 | if (fn) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 1320 | PyObject *res = PyObject_CallOneArg(fn, coro); |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1321 | Py_DECREF(fn); |
| 1322 | if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) { |
| 1323 | warned = 1; |
| 1324 | } |
| 1325 | Py_XDECREF(res); |
| 1326 | } |
| 1327 | |
| 1328 | if (PyErr_Occurred()) { |
| 1329 | PyErr_WriteUnraisable(coro); |
| 1330 | } |
| 1331 | if (!warned) { |
Victor Stinner | 8d84adc | 2020-03-31 17:25:12 +0200 | [diff] [blame] | 1332 | if (_PyErr_WarnFormat(coro, PyExc_RuntimeWarning, 1, |
| 1333 | "coroutine '%S' was never awaited", |
| 1334 | ((PyCoroObject *)coro)->cr_qualname) < 0) |
Yury Selivanov | 3510334 | 2018-01-21 20:47:04 -0500 | [diff] [blame] | 1335 | { |
Nathaniel J. Smith | fc2f407 | 2018-01-21 06:44:07 -0800 | [diff] [blame] | 1336 | PyErr_WriteUnraisable(coro); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1340 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1341 | PyDoc_STRVAR(warn_explicit_doc, |
Hansraj Das | 5dfbb4d | 2019-10-08 14:26:07 +0530 | [diff] [blame] | 1342 | "Low-level interface to warnings functionality."); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1343 | |
| 1344 | static PyMethodDef warnings_functions[] = { |
Victor Stinner | 22f1875 | 2016-12-09 18:08:18 +0100 | [diff] [blame] | 1345 | WARNINGS_WARN_METHODDEF |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 1346 | {"warn_explicit", (PyCFunction)(void(*)(void))warnings_warn_explicit, |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1347 | METH_VARARGS | METH_KEYWORDS, warn_explicit_doc}, |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1348 | {"_filters_mutated", (PyCFunction)warnings_filters_mutated, METH_NOARGS, |
| 1349 | NULL}, |
Christian Heimes | 1a8501c | 2008-10-02 19:56:01 +0000 | [diff] [blame] | 1350 | /* XXX(brett.cannon): add showwarning? */ |
| 1351 | /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | {NULL, NULL} /* sentinel */ |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1353 | }; |
| 1354 | |
| 1355 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1356 | static struct PyModuleDef warningsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | PyModuleDef_HEAD_INIT, |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1358 | MODULE_NAME, /* m_name */ |
| 1359 | warnings__doc__, /* m_doc */ |
| 1360 | 0, /* m_size */ |
| 1361 | warnings_functions, /* m_methods */ |
| 1362 | NULL, /* m_reload */ |
| 1363 | NULL, /* m_traverse */ |
| 1364 | NULL, /* m_clear */ |
| 1365 | NULL /* m_free */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1366 | }; |
| 1367 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1368 | |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 1369 | PyStatus |
| 1370 | _PyWarnings_InitState(PyThreadState *tstate) |
| 1371 | { |
| 1372 | if (warnings_init_state(&tstate->interp->warnings) < 0) { |
| 1373 | return _PyStatus_ERR("can't initialize warnings"); |
| 1374 | } |
| 1375 | return _PyStatus_OK(); |
| 1376 | } |
| 1377 | |
| 1378 | |
Victor Stinner | 5d86246 | 2017-12-19 11:35:58 +0100 | [diff] [blame] | 1379 | PyMODINIT_FUNC |
| 1380 | _PyWarnings_Init(void) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1381 | { |
Brett Cannon | 0759dd6 | 2009-04-01 18:13:07 +0000 | [diff] [blame] | 1382 | PyObject *m; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1383 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1384 | m = PyModule_Create(&warningsmodule); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1385 | if (m == NULL) { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1386 | return NULL; |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1387 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1388 | |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 1389 | WarningsState *st = warnings_get_state(); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1390 | if (st == NULL) { |
| 1391 | goto error; |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1392 | } |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 1393 | if (warnings_init_state(st) < 0) { |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1394 | goto error; |
Antoine Pitrou | aa5c5c6 | 2012-01-18 21:45:15 +0100 | [diff] [blame] | 1395 | } |
Antoine Pitrou | cb0a006 | 2014-09-18 02:40:46 +0200 | [diff] [blame] | 1396 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1397 | Py_INCREF(st->filters); |
| 1398 | if (PyModule_AddObject(m, "filters", st->filters) < 0) { |
| 1399 | goto error; |
| 1400 | } |
| 1401 | |
| 1402 | Py_INCREF(st->once_registry); |
| 1403 | if (PyModule_AddObject(m, "_onceregistry", st->once_registry) < 0) { |
| 1404 | goto error; |
| 1405 | } |
| 1406 | |
| 1407 | Py_INCREF(st->default_action); |
| 1408 | if (PyModule_AddObject(m, "_defaultaction", st->default_action) < 0) { |
| 1409 | goto error; |
| 1410 | } |
| 1411 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1412 | return m; |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1413 | |
| 1414 | error: |
| 1415 | if (st != NULL) { |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 1416 | warnings_clear_state(st); |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1417 | } |
| 1418 | Py_DECREF(m); |
| 1419 | return NULL; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1420 | } |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1421 | |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1422 | // We need this to ensure that warnings still work until late in finalization. |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1423 | void |
Eric Snow | 86ea581 | 2019-05-10 13:29:55 -0400 | [diff] [blame] | 1424 | _PyWarnings_Fini(PyInterpreterState *interp) |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1425 | { |
Victor Stinner | 66b7973 | 2020-03-02 15:02:18 +0100 | [diff] [blame] | 1426 | warnings_clear_state(&interp->warnings); |
Victor Stinner | 87d23a0 | 2019-04-26 05:49:26 +0200 | [diff] [blame] | 1427 | } |