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