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