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