/src/cpython/Python/_warnings.c
Line | Count | Source |
1 | | #include "Python.h" |
2 | | #include "pycore_critical_section.h" // _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED() |
3 | | #include "pycore_frame.h" // PyFrameObject |
4 | | #include "pycore_genobject.h" // PyAsyncGenObject |
5 | | #include "pycore_import.h" // _PyImport_GetModules() |
6 | | #include "pycore_interpframe.h" // _PyFrame_GetCode() |
7 | | #include "pycore_long.h" // _PyLong_GetZero() |
8 | | #include "pycore_object.h" // _Py_XGetRef() |
9 | | #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_SSIZE_ACQUIRE() |
10 | | #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() |
11 | | #include "pycore_pystate.h" // _PyThreadState_GET() |
12 | | #include "pycore_traceback.h" // _Py_DisplaySourceLine() |
13 | | #include "pycore_tuple.h" // _PyTuple_FromPair |
14 | | #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString() |
15 | | #include "pycore_warnings.h" // _PyWarnings_AfterFork() |
16 | | |
17 | | #include <stdbool.h> |
18 | | #include "clinic/_warnings.c.h" |
19 | | |
20 | | |
21 | 0 | #define MODULE_NAME "_warnings" |
22 | | |
23 | | PyDoc_STRVAR(warnings__doc__, |
24 | | MODULE_NAME " provides basic warning filtering support.\n" |
25 | | "It is a helper module to speed up interpreter start-up."); |
26 | | |
27 | | |
28 | | /*************************************************************************/ |
29 | | |
30 | | typedef struct _warnings_runtime_state WarningsState; |
31 | | |
32 | | static inline int |
33 | | check_interp(PyInterpreterState *interp) |
34 | 1.72M | { |
35 | 1.72M | if (interp == NULL) { |
36 | 0 | PyErr_SetString(PyExc_RuntimeError, |
37 | 0 | "warnings_get_state: could not identify " |
38 | 0 | "current interpreter"); |
39 | 0 | return 0; |
40 | 0 | } |
41 | 1.72M | return 1; |
42 | 1.72M | } |
43 | | |
44 | | static inline PyInterpreterState * |
45 | | get_current_interp(void) |
46 | 3.75k | { |
47 | 3.75k | PyInterpreterState *interp = _PyInterpreterState_GET(); |
48 | 3.75k | return check_interp(interp) ? interp : NULL; |
49 | 3.75k | } |
50 | | |
51 | | static inline PyThreadState * |
52 | | get_current_tstate(void) |
53 | 1.72M | { |
54 | 1.72M | PyThreadState *tstate = _PyThreadState_GET(); |
55 | 1.72M | if (tstate == NULL) { |
56 | 0 | (void)check_interp(NULL); |
57 | 0 | return NULL; |
58 | 0 | } |
59 | 1.72M | return check_interp(tstate->interp) ? tstate : NULL; |
60 | 1.72M | } |
61 | | |
62 | | /* Given a module object, get its per-module state. */ |
63 | | static WarningsState * |
64 | | warnings_get_state(PyInterpreterState *interp) |
65 | 1.61M | { |
66 | 1.61M | return &interp->warnings; |
67 | 1.61M | } |
68 | | |
69 | | /* Clear the given warnings module state. */ |
70 | | static void |
71 | | warnings_clear_state(WarningsState *st) |
72 | 0 | { |
73 | 0 | Py_CLEAR(st->filters); |
74 | 0 | Py_CLEAR(st->once_registry); |
75 | 0 | Py_CLEAR(st->default_action); |
76 | 0 | Py_CLEAR(st->context); |
77 | 0 | } |
78 | | |
79 | | #ifndef Py_DEBUG |
80 | | static PyObject * |
81 | | create_filter(PyObject *category, PyObject *action_str, const char *modname) |
82 | 185 | { |
83 | 185 | PyObject *modname_obj = NULL; |
84 | | |
85 | | /* Default to "no module name" for initial filter set */ |
86 | 185 | if (modname != NULL) { |
87 | 37 | modname_obj = PyUnicode_InternFromString(modname); |
88 | 37 | if (modname_obj == NULL) { |
89 | 0 | return NULL; |
90 | 0 | } |
91 | 148 | } else { |
92 | 148 | modname_obj = Py_NewRef(Py_None); |
93 | 148 | } |
94 | | |
95 | | /* This assumes the line number is zero for now. */ |
96 | 185 | PyObject *filter = PyTuple_Pack(5, action_str, Py_None, |
97 | 185 | category, modname_obj, _PyLong_GetZero()); |
98 | 185 | Py_DECREF(modname_obj); |
99 | 185 | return filter; |
100 | 185 | } |
101 | | #endif |
102 | | |
103 | | static PyObject * |
104 | | init_filters(PyInterpreterState *interp) |
105 | 37 | { |
106 | | #ifdef Py_DEBUG |
107 | | /* Py_DEBUG builds show all warnings by default */ |
108 | | return PyList_New(0); |
109 | | #else |
110 | | /* Other builds ignore a number of warning categories by default */ |
111 | 37 | PyObject *filters = PyList_New(5); |
112 | 37 | if (filters == NULL) { |
113 | 0 | return NULL; |
114 | 0 | } |
115 | | |
116 | 37 | size_t pos = 0; /* Post-incremented in each use. */ |
117 | 37 | #define ADD(TYPE, ACTION, MODNAME) \ |
118 | 185 | PyList_SET_ITEM(filters, pos++, \ |
119 | 185 | create_filter(TYPE, &_Py_ID(ACTION), MODNAME)); |
120 | 37 | ADD(PyExc_DeprecationWarning, default, "__main__"); |
121 | 37 | ADD(PyExc_DeprecationWarning, ignore, NULL); |
122 | 37 | ADD(PyExc_PendingDeprecationWarning, ignore, NULL); |
123 | 37 | ADD(PyExc_ImportWarning, ignore, NULL); |
124 | 37 | ADD(PyExc_ResourceWarning, ignore, NULL); |
125 | 37 | #undef ADD |
126 | | |
127 | 222 | for (size_t x = 0; x < pos; x++) { |
128 | 185 | if (PyList_GET_ITEM(filters, x) == NULL) { |
129 | 0 | Py_DECREF(filters); |
130 | 0 | return NULL; |
131 | 0 | } |
132 | 185 | } |
133 | 37 | return filters; |
134 | 37 | #endif |
135 | 37 | } |
136 | | |
137 | | /* Initialize the given warnings module state. */ |
138 | | int |
139 | | _PyWarnings_InitState(PyInterpreterState *interp) |
140 | 37 | { |
141 | 37 | WarningsState *st = &interp->warnings; |
142 | | |
143 | 37 | if (st->filters == NULL) { |
144 | 37 | st->filters = init_filters(interp); |
145 | 37 | if (st->filters == NULL) { |
146 | 0 | return -1; |
147 | 0 | } |
148 | 37 | PyUnstable_EnableTryIncRef(st->filters); |
149 | 37 | } |
150 | | |
151 | 37 | if (st->once_registry == NULL) { |
152 | 37 | st->once_registry = PyDict_New(); |
153 | 37 | if (st->once_registry == NULL) { |
154 | 0 | return -1; |
155 | 0 | } |
156 | 37 | PyUnstable_EnableTryIncRef(st->once_registry); |
157 | 37 | } |
158 | | |
159 | 37 | if (st->default_action == NULL) { |
160 | 37 | st->default_action = PyUnicode_FromString("default"); |
161 | 37 | if (st->default_action == NULL) { |
162 | 0 | return -1; |
163 | 0 | } |
164 | 37 | PyUnstable_EnableTryIncRef(st->default_action); |
165 | 37 | } |
166 | | |
167 | 37 | if (st->context == NULL) { |
168 | 37 | st->context = PyContextVar_New("_warnings_context", NULL); |
169 | 37 | if (st->context == NULL) { |
170 | 0 | return -1; |
171 | 0 | } |
172 | 37 | } |
173 | | |
174 | 37 | st->filters_version = 0; |
175 | 37 | return 0; |
176 | 37 | } |
177 | | |
178 | | |
179 | | /*************************************************************************/ |
180 | | |
181 | | static int |
182 | | check_matched(PyInterpreterState *interp, PyObject *obj, PyObject *arg, PyObject *arg2) |
183 | 1.80M | { |
184 | 1.80M | PyObject *result; |
185 | 1.80M | int rc; |
186 | | |
187 | | /* A 'None' filter always matches */ |
188 | 1.80M | if (obj == Py_None) |
189 | 1.62M | return 1; |
190 | | |
191 | | /* An internal plain text default filter must match exactly */ |
192 | 178k | if (PyUnicode_CheckExact(obj)) { |
193 | 178k | if (arg == NULL) { |
194 | 0 | return 0; |
195 | 0 | } |
196 | 178k | int cmp_result = PyUnicode_Compare(obj, arg); |
197 | 178k | if (cmp_result == -1 && PyErr_Occurred()) { |
198 | 0 | return -1; |
199 | 0 | } |
200 | 178k | return !cmp_result; |
201 | 178k | } |
202 | | |
203 | | /* Otherwise assume a regex filter and call its match() method */ |
204 | 0 | if (arg != NULL) { |
205 | 0 | result = PyObject_CallMethodOneArg(obj, &_Py_ID(match), arg); |
206 | 0 | } |
207 | 0 | else { |
208 | 0 | PyObject *match = PyImport_ImportModuleAttrString("_py_warnings", "_match_filename"); |
209 | 0 | if (match == NULL) { |
210 | 0 | return -1; |
211 | 0 | } |
212 | 0 | result = PyObject_CallFunctionObjArgs(match, obj, arg2, NULL); |
213 | 0 | Py_DECREF(match); |
214 | 0 | } |
215 | 0 | if (result == NULL) |
216 | 0 | return -1; |
217 | 0 | rc = PyObject_IsTrue(result); |
218 | 0 | Py_DECREF(result); |
219 | 0 | return rc; |
220 | 0 | } |
221 | | |
222 | | #define GET_WARNINGS_ATTR(interp, ATTR, try_import) \ |
223 | 722k | get_warnings_attr(interp, &_Py_ID(ATTR), try_import) |
224 | | |
225 | | /* |
226 | | Returns a new reference. |
227 | | A NULL return value can mean false or an error. |
228 | | */ |
229 | | static PyObject * |
230 | | get_warnings_attr(PyInterpreterState *interp, PyObject *attr, int try_import) |
231 | 722k | { |
232 | 722k | PyObject *warnings_module, *obj; |
233 | | |
234 | | /* don't try to import after the start of the Python finallization */ |
235 | 722k | if (try_import && !_Py_IsInterpreterFinalizing(interp)) { |
236 | 175k | warnings_module = PyImport_Import(&_Py_ID(warnings)); |
237 | 175k | if (warnings_module == NULL) { |
238 | | /* Fallback to the C implementation if we cannot get |
239 | | the Python implementation */ |
240 | 0 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
241 | 0 | PyErr_Clear(); |
242 | 0 | } |
243 | 0 | return NULL; |
244 | 0 | } |
245 | 175k | } |
246 | 546k | else { |
247 | | /* if we're so late into Python finalization that the module dict is |
248 | | gone, then we can't even use PyImport_GetModule without triggering |
249 | | an interpreter abort. |
250 | | */ |
251 | 546k | if (!_PyImport_GetModules(interp)) { |
252 | 0 | return NULL; |
253 | 0 | } |
254 | 546k | warnings_module = PyImport_GetModule(&_Py_ID(warnings)); |
255 | 546k | if (warnings_module == NULL) |
256 | 222 | return NULL; |
257 | 546k | } |
258 | | |
259 | 722k | (void)PyObject_GetOptionalAttr(warnings_module, attr, &obj); |
260 | 722k | Py_DECREF(warnings_module); |
261 | 722k | return obj; |
262 | 722k | } |
263 | | |
264 | | static inline void |
265 | | warnings_lock(PyInterpreterState *interp) |
266 | 1.24k | { |
267 | 1.24k | WarningsState *st = warnings_get_state(interp); |
268 | 1.24k | assert(st != NULL); |
269 | 1.24k | _PyRecursiveMutex_Lock(&st->lock); |
270 | 1.24k | } |
271 | | |
272 | | static inline int |
273 | | warnings_unlock(PyInterpreterState *interp) |
274 | 1.24k | { |
275 | 1.24k | WarningsState *st = warnings_get_state(interp); |
276 | 1.24k | assert(st != NULL); |
277 | 1.24k | return _PyRecursiveMutex_TryUnlock(&st->lock); |
278 | 1.24k | } |
279 | | |
280 | | static inline bool |
281 | | warnings_lock_held(WarningsState *st) |
282 | 1.24k | { |
283 | 1.24k | return PyMutex_IsLocked(&st->lock.mutex); |
284 | 1.24k | } |
285 | | |
286 | | #ifdef HAVE_FORK |
287 | | void |
288 | | _PyWarnings_BeforeFork(PyInterpreterState *interp) |
289 | 0 | { |
290 | 0 | WarningsState *st = warnings_get_state(interp); |
291 | 0 | st->lock_held_at_fork = _PyRecursiveMutex_IsLockedByCurrentThread(&st->lock); |
292 | 0 | } |
293 | | |
294 | | void |
295 | | _PyWarnings_AfterFork(PyInterpreterState *interp) |
296 | 0 | { |
297 | 0 | WarningsState *st = warnings_get_state(interp); |
298 | 0 | _PyRecursiveMutex_at_fork_reinit(&st->lock, st->lock_held_at_fork); |
299 | 0 | st->lock_held_at_fork = false; |
300 | 0 | } |
301 | | #endif |
302 | | |
303 | | static PyObject * |
304 | | get_warnings_context(PyInterpreterState *interp) |
305 | 195k | { |
306 | 195k | WarningsState *st = warnings_get_state(interp); |
307 | 195k | assert(PyContextVar_CheckExact(st->context)); |
308 | 195k | PyObject *ctx; |
309 | 195k | if (PyContextVar_Get(st->context, NULL, &ctx) < 0) { |
310 | 0 | return NULL; |
311 | 0 | } |
312 | 195k | if (ctx == NULL) { |
313 | 195k | Py_RETURN_NONE; |
314 | 195k | } |
315 | 0 | return ctx; |
316 | 195k | } |
317 | | |
318 | | static PyObject * |
319 | | get_warnings_context_filters(PyInterpreterState *interp) |
320 | 195k | { |
321 | 195k | PyObject *ctx = get_warnings_context(interp); |
322 | 195k | if (ctx == NULL) { |
323 | 0 | return NULL; |
324 | 0 | } |
325 | 195k | if (ctx == Py_None) { |
326 | 195k | Py_RETURN_NONE; |
327 | 195k | } |
328 | 0 | PyObject *context_filters = PyObject_GetAttr(ctx, &_Py_ID(_filters)); |
329 | 0 | Py_DECREF(ctx); |
330 | 0 | if (context_filters == NULL) { |
331 | 0 | return NULL; |
332 | 0 | } |
333 | 0 | if (!PyList_Check(context_filters)) { |
334 | 0 | PyErr_SetString(PyExc_ValueError, |
335 | 0 | "_filters of warnings._warnings_context must be a list"); |
336 | 0 | Py_DECREF(context_filters); |
337 | 0 | return NULL; |
338 | 0 | } |
339 | 0 | return context_filters; |
340 | 0 | } |
341 | | |
342 | | /* Store 'value' in the warnings state field '*ptr'. */ |
343 | | static void |
344 | | update_state_ref(PyObject **ptr, PyObject *value) |
345 | 370k | { |
346 | 370k | if (FT_ATOMIC_LOAD_PTR_ACQUIRE(*ptr) == value) { |
347 | 370k | return; |
348 | 370k | } |
349 | 0 | PyUnstable_EnableTryIncRef(value); |
350 | 0 | PyObject *old = _Py_atomic_exchange_ptr(ptr, Py_NewRef(value)); |
351 | 0 | Py_XDECREF(old); |
352 | 0 | } |
353 | | |
354 | | /* Return a new reference to the warnings state field '*ptr'. */ |
355 | | static PyObject * |
356 | | get_state_ref(PyObject **ptr) |
357 | 222 | { |
358 | | #ifdef Py_GIL_DISABLED |
359 | | return _Py_XGetRef(ptr); |
360 | | #else |
361 | 222 | return Py_XNewRef(*ptr); |
362 | 222 | #endif |
363 | 222 | } |
364 | | |
365 | | // Returns a new reference to the list. |
366 | | static PyObject * |
367 | | get_warnings_filters(PyInterpreterState *interp) |
368 | 195k | { |
369 | 195k | WarningsState *st = warnings_get_state(interp); |
370 | 195k | PyObject *warnings_filters = GET_WARNINGS_ATTR(interp, filters, 0); |
371 | 195k | if (warnings_filters == NULL) { |
372 | 222 | if (PyErr_Occurred()) |
373 | 0 | return NULL; |
374 | 222 | warnings_filters = get_state_ref(&st->filters); |
375 | 222 | } |
376 | 194k | else { |
377 | 194k | update_state_ref(&st->filters, warnings_filters); |
378 | 194k | } |
379 | | |
380 | 195k | PyObject *filters = warnings_filters; |
381 | 195k | if (filters == NULL || !PyList_Check(filters)) { |
382 | 0 | PyErr_SetString(PyExc_ValueError, |
383 | 0 | MODULE_NAME ".filters must be a list"); |
384 | 0 | Py_XDECREF(filters); |
385 | 0 | return NULL; |
386 | 0 | } |
387 | 195k | return filters; |
388 | 195k | } |
389 | | |
390 | | /*[clinic input] |
391 | | _acquire_lock as warnings_acquire_lock |
392 | | |
393 | | [clinic start generated code]*/ |
394 | | |
395 | | static PyObject * |
396 | | warnings_acquire_lock_impl(PyObject *module) |
397 | | /*[clinic end generated code: output=594313457d1bf8e1 input=46ec20e55acca52f]*/ |
398 | 1.24k | { |
399 | 1.24k | PyInterpreterState *interp = get_current_interp(); |
400 | 1.24k | if (interp == NULL) { |
401 | 0 | return NULL; |
402 | 0 | } |
403 | 1.24k | warnings_lock(interp); |
404 | 1.24k | Py_RETURN_NONE; |
405 | 1.24k | } |
406 | | |
407 | | /*[clinic input] |
408 | | _release_lock as warnings_release_lock |
409 | | |
410 | | [clinic start generated code]*/ |
411 | | |
412 | | static PyObject * |
413 | | warnings_release_lock_impl(PyObject *module) |
414 | | /*[clinic end generated code: output=d73d5a8789396750 input=ea01bb77870c5693]*/ |
415 | 1.24k | { |
416 | 1.24k | PyInterpreterState *interp = get_current_interp(); |
417 | 1.24k | if (interp == NULL) { |
418 | 0 | return NULL; |
419 | 0 | } |
420 | 1.24k | if (warnings_unlock(interp) < 0) { |
421 | 0 | PyErr_SetString(PyExc_RuntimeError, "cannot release un-acquired lock"); |
422 | 0 | return NULL; |
423 | 0 | } |
424 | 1.24k | Py_RETURN_NONE; |
425 | 1.24k | } |
426 | | |
427 | | // Returns a new reference. |
428 | | static PyObject * |
429 | | get_once_registry(PyInterpreterState *interp) |
430 | 0 | { |
431 | 0 | WarningsState *st = warnings_get_state(interp); |
432 | 0 | assert(st != NULL); |
433 | |
|
434 | 0 | PyObject *registry = GET_WARNINGS_ATTR(interp, onceregistry, 0); |
435 | 0 | if (registry == NULL) { |
436 | 0 | if (PyErr_Occurred()) |
437 | 0 | return NULL; |
438 | 0 | registry = get_state_ref(&st->once_registry); |
439 | 0 | assert(registry != NULL); |
440 | 0 | return registry; |
441 | 0 | } |
442 | 0 | if (!PyDict_Check(registry)) { |
443 | 0 | PyErr_Format(PyExc_TypeError, |
444 | 0 | MODULE_NAME ".onceregistry must be a dict, " |
445 | 0 | "not '%.200s'", |
446 | 0 | Py_TYPE(registry)->tp_name); |
447 | 0 | Py_DECREF(registry); |
448 | 0 | return NULL; |
449 | 0 | } |
450 | 0 | update_state_ref(&st->once_registry, registry); |
451 | 0 | return registry; |
452 | 0 | } |
453 | | |
454 | | |
455 | | // Returns a new reference. |
456 | | static PyObject * |
457 | | get_default_action(PyInterpreterState *interp) |
458 | 175k | { |
459 | 175k | WarningsState *st = warnings_get_state(interp); |
460 | 175k | assert(st != NULL); |
461 | | |
462 | 175k | PyObject *default_action = GET_WARNINGS_ATTR(interp, defaultaction, 0); |
463 | 175k | if (default_action == NULL) { |
464 | 0 | if (PyErr_Occurred()) { |
465 | 0 | return NULL; |
466 | 0 | } |
467 | 0 | default_action = get_state_ref(&st->default_action); |
468 | 0 | assert(default_action != NULL); |
469 | 0 | return default_action; |
470 | 0 | } |
471 | 175k | if (!PyUnicode_Check(default_action)) { |
472 | 0 | PyErr_Format(PyExc_TypeError, |
473 | 0 | MODULE_NAME ".defaultaction must be a string, " |
474 | 0 | "not '%.200s'", |
475 | 0 | Py_TYPE(default_action)->tp_name); |
476 | 0 | Py_DECREF(default_action); |
477 | 0 | return NULL; |
478 | 0 | } |
479 | 175k | update_state_ref(&st->default_action, default_action); |
480 | 175k | return default_action; |
481 | 175k | } |
482 | | |
483 | | /* Search filters list of match, returns false on error. If no match |
484 | | * then 'matched_action' is NULL. */ |
485 | | static bool |
486 | | filter_search(PyInterpreterState *interp, PyObject *category, |
487 | | PyObject *text, Py_ssize_t lineno, |
488 | | PyObject *module, PyObject *filename, char *list_name, PyObject *filters, |
489 | 195k | PyObject **item, PyObject **matched_action) { |
490 | 195k | bool result = true; |
491 | 195k | *matched_action = NULL; |
492 | | /* Avoid the filters list changing while we iterate over it. */ |
493 | 195k | Py_BEGIN_CRITICAL_SECTION(filters); |
494 | 1.07M | for (Py_ssize_t i = 0; i < PyList_GET_SIZE(filters); i++) { |
495 | 902k | PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; |
496 | 902k | Py_ssize_t ln; |
497 | 902k | int is_subclass, good_msg, good_mod; |
498 | | |
499 | 902k | tmp_item = PyList_GET_ITEM(filters, i); |
500 | 902k | if (!PyTuple_Check(tmp_item) || PyTuple_GET_SIZE(tmp_item) != 5) { |
501 | 0 | PyErr_Format(PyExc_ValueError, |
502 | 0 | "warnings.%s item %zd isn't a 5-tuple", list_name, i); |
503 | 0 | result = false; |
504 | 0 | break; |
505 | 0 | } |
506 | | |
507 | | /* Python code: action, msg, cat, mod, ln = item */ |
508 | 902k | Py_INCREF(tmp_item); |
509 | 902k | action = PyTuple_GET_ITEM(tmp_item, 0); |
510 | 902k | msg = PyTuple_GET_ITEM(tmp_item, 1); |
511 | 902k | cat = PyTuple_GET_ITEM(tmp_item, 2); |
512 | 902k | mod = PyTuple_GET_ITEM(tmp_item, 3); |
513 | 902k | ln_obj = PyTuple_GET_ITEM(tmp_item, 4); |
514 | | |
515 | 902k | if (!PyUnicode_Check(action)) { |
516 | 0 | PyErr_Format(PyExc_TypeError, |
517 | 0 | "action must be a string, not '%.200s'", |
518 | 0 | Py_TYPE(action)->tp_name); |
519 | 0 | Py_DECREF(tmp_item); |
520 | 0 | result = false; |
521 | 0 | break; |
522 | 0 | } |
523 | | |
524 | 902k | good_msg = check_matched(interp, msg, text, NULL); |
525 | 902k | if (good_msg == -1) { |
526 | 0 | Py_DECREF(tmp_item); |
527 | 0 | result = false; |
528 | 0 | break; |
529 | 0 | } |
530 | | |
531 | 902k | good_mod = check_matched(interp, mod, module, filename); |
532 | 902k | if (good_mod == -1) { |
533 | 0 | Py_DECREF(tmp_item); |
534 | 0 | result = false; |
535 | 0 | break; |
536 | 0 | } |
537 | | |
538 | 902k | is_subclass = PyObject_IsSubclass(category, cat); |
539 | 902k | if (is_subclass == -1) { |
540 | 0 | Py_DECREF(tmp_item); |
541 | 0 | result = false; |
542 | 0 | break; |
543 | 0 | } |
544 | | |
545 | 902k | ln = PyLong_AsSsize_t(ln_obj); |
546 | 902k | if (ln == -1 && PyErr_Occurred()) { |
547 | 0 | Py_DECREF(tmp_item); |
548 | 0 | result = false; |
549 | 0 | break; |
550 | 0 | } |
551 | | |
552 | 902k | if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) { |
553 | 19.2k | *item = tmp_item; |
554 | 19.2k | *matched_action = action; |
555 | 19.2k | result = true; |
556 | 19.2k | break; |
557 | 19.2k | } |
558 | | |
559 | 882k | Py_DECREF(tmp_item); |
560 | 882k | } |
561 | 195k | Py_END_CRITICAL_SECTION(); |
562 | 195k | return result; |
563 | 195k | } |
564 | | |
565 | | /* The returned action and the item are new references. */ |
566 | | static PyObject* |
567 | | get_filter(PyInterpreterState *interp, PyObject *category, |
568 | | PyObject *text, Py_ssize_t lineno, |
569 | | PyObject *module, PyObject *filename, PyObject **item) |
570 | 195k | { |
571 | | /* check _warning_context _filters list */ |
572 | 195k | PyObject *context_filters = get_warnings_context_filters(interp); |
573 | 195k | if (context_filters == NULL) { |
574 | 0 | return NULL; |
575 | 0 | } |
576 | 195k | bool use_global_filters = false; |
577 | 195k | if (context_filters == Py_None) { |
578 | 195k | use_global_filters = true; |
579 | 195k | } else { |
580 | 0 | PyObject *context_action = NULL; |
581 | 0 | if (!filter_search(interp, category, text, lineno, module, filename, "_warnings_context _filters", |
582 | 0 | context_filters, item, &context_action)) { |
583 | 0 | Py_DECREF(context_filters); |
584 | 0 | return NULL; |
585 | 0 | } |
586 | 0 | Py_DECREF(context_filters); |
587 | 0 | if (context_action != NULL) { |
588 | 0 | return Py_NewRef(context_action); |
589 | 0 | } |
590 | 0 | } |
591 | | |
592 | 195k | PyObject *action; |
593 | | |
594 | 195k | if (use_global_filters) { |
595 | | /* check warnings.filters list */ |
596 | 195k | PyObject *filters = get_warnings_filters(interp); |
597 | 195k | if (filters == NULL) { |
598 | 0 | return NULL; |
599 | 0 | } |
600 | 195k | bool ok = filter_search(interp, category, text, lineno, module, |
601 | 195k | filename, "filters", filters, item, &action); |
602 | 195k | Py_DECREF(filters); |
603 | 195k | if (!ok) { |
604 | 0 | return NULL; |
605 | 0 | } |
606 | 195k | if (action != NULL) { |
607 | 19.2k | return Py_NewRef(action); |
608 | 19.2k | } |
609 | 195k | } |
610 | | |
611 | 175k | action = get_default_action(interp); |
612 | 175k | if (action != NULL) { |
613 | 175k | *item = Py_NewRef(Py_None); |
614 | 175k | return action; |
615 | 175k | } |
616 | | |
617 | 0 | return NULL; |
618 | 175k | } |
619 | | |
620 | | |
621 | | /* Returns 1 if 'registry' is stamped with 'version', 0 if not, -1 on error. */ |
622 | | static int |
623 | | registry_version_matches(PyObject *registry, Py_ssize_t version) |
624 | 1.20M | { |
625 | 1.20M | PyObject *version_obj; |
626 | 1.20M | if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { |
627 | 0 | return -1; |
628 | 0 | } |
629 | 1.20M | int rc = 0; |
630 | 1.20M | if (version_obj != NULL && PyLong_CheckExact(version_obj)) { |
631 | 1.20M | int overflow; |
632 | 1.20M | long long v = PyLong_AsLongLongAndOverflow(version_obj, &overflow); |
633 | 1.20M | rc = (!overflow && v == version); |
634 | 1.20M | } |
635 | 1.20M | Py_XDECREF(version_obj); |
636 | 1.20M | return rc; |
637 | 1.20M | } |
638 | | |
639 | | static int |
640 | | set_registry_version(PyObject *registry, Py_ssize_t version) |
641 | 238 | { |
642 | 238 | PyObject *version_obj = PyLong_FromSsize_t(version); |
643 | 238 | if (version_obj == NULL) { |
644 | 0 | return -1; |
645 | 0 | } |
646 | 238 | int rc = PyDict_SetItem(registry, &_Py_ID(version), version_obj); |
647 | 238 | Py_DECREF(version_obj); |
648 | 238 | return rc; |
649 | 238 | } |
650 | | |
651 | | /* Make 'registry' usable for filters 'version', clearing it if it is stamped |
652 | | with a different version. Returns 1 if it can be used, 0 if 'version' is |
653 | | no longer current, -1 on error. */ |
654 | | static int |
655 | | check_registry_version(PyInterpreterState *interp, PyObject *registry, |
656 | | Py_ssize_t version) |
657 | 175k | { |
658 | 175k | _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(registry); |
659 | 175k | WarningsState *st = warnings_get_state(interp); |
660 | | |
661 | 175k | int rc = registry_version_matches(registry, version); |
662 | 175k | if (rc != 0) { |
663 | 175k | return rc; |
664 | 175k | } |
665 | 0 | if (version != FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version)) { |
666 | | // The caller's filter search is stale, don't record its result. |
667 | 0 | return 0; |
668 | 0 | } |
669 | | |
670 | 0 | PyDict_Clear(registry); |
671 | 0 | return set_registry_version(registry, version) < 0 ? -1 : 1; |
672 | 0 | } |
673 | | |
674 | | static int |
675 | | registry_contains(PyObject *registry, PyObject *key) |
676 | 1.03M | { |
677 | 1.03M | PyObject *value; |
678 | 1.03M | if (PyDict_GetItemRef(registry, key, &value) < 0) { |
679 | 0 | return -1; |
680 | 0 | } |
681 | 1.03M | if (value == NULL) { |
682 | 354k | return 0; |
683 | 354k | } |
684 | 675k | int rc = PyObject_IsTrue(value); |
685 | 675k | Py_DECREF(value); |
686 | 675k | return rc; |
687 | 1.03M | } |
688 | | |
689 | | /* Returns 1 if 'key' is recorded in 'registry' for the current filters version, |
690 | | 0 if not, -1 on error. Stores the version checked in '*pversion'. */ |
691 | | static int |
692 | | already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, |
693 | | Py_ssize_t *pversion) |
694 | 854k | { |
695 | 854k | int rc; |
696 | 854k | Py_BEGIN_CRITICAL_SECTION(registry); |
697 | 854k | WarningsState *st = warnings_get_state(interp); |
698 | 854k | Py_ssize_t version = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version); |
699 | 854k | *pversion = version; |
700 | 854k | rc = registry_version_matches(registry, version); |
701 | 854k | if (rc > 0) { |
702 | 854k | rc = registry_contains(registry, key); |
703 | 854k | } |
704 | 238 | else if (rc == 0) { |
705 | | /* Stamped with an old version, reset it. 'key' can't be present. */ |
706 | 238 | PyDict_Clear(registry); |
707 | 238 | if (set_registry_version(registry, version) < 0) { |
708 | 0 | rc = -1; |
709 | 0 | } |
710 | 238 | } |
711 | 854k | Py_END_CRITICAL_SECTION(); |
712 | 854k | return rc; |
713 | 854k | } |
714 | | |
715 | | /* Returns 1 if 'key' was already in 'registry', 0 if it was added, -1 on |
716 | | error. */ |
717 | | static int |
718 | | registry_add(PyObject *registry, PyObject *key) |
719 | 175k | { |
720 | 175k | int rc = registry_contains(registry, key); |
721 | 175k | if (rc == 0 && PyDict_SetItem(registry, key, Py_True) < 0) { |
722 | 0 | rc = -1; |
723 | 0 | } |
724 | 175k | return rc; |
725 | 175k | } |
726 | | |
727 | | /* Record 'key' and 'altkey' (either may be NULL) in 'registry' for filters |
728 | | 'version'. Returns 0 if the warning should be shown, 1 if it was already |
729 | | recorded, -1 on error. */ |
730 | | static int |
731 | | record_warning(PyInterpreterState *interp, PyObject *registry, |
732 | | PyObject *key, PyObject *altkey, Py_ssize_t version) |
733 | 175k | { |
734 | 175k | int rc; |
735 | 175k | Py_BEGIN_CRITICAL_SECTION(registry); |
736 | 175k | rc = check_registry_version(interp, registry, version); |
737 | 175k | if (rc > 0) { |
738 | 175k | rc = 0; |
739 | 175k | if (key != NULL) { |
740 | 175k | rc = registry_add(registry, key); |
741 | 175k | } |
742 | 175k | if (rc == 0 && altkey != NULL) { |
743 | 0 | rc = registry_add(registry, altkey); |
744 | 0 | } |
745 | 175k | if (rc >= 0) { |
746 | | /* The critical section may have been suspended while the keys |
747 | | were added, letting another thread clear and re-stamp the |
748 | | registry. If so, invalidate it. */ |
749 | 175k | int match = registry_version_matches(registry, version); |
750 | 175k | if (match < 0 || |
751 | 175k | (match == 0 && set_registry_version(registry, -1) < 0)) |
752 | 0 | { |
753 | 0 | rc = -1; |
754 | 0 | } |
755 | 175k | } |
756 | 175k | } |
757 | 175k | Py_END_CRITICAL_SECTION(); |
758 | 175k | return rc; |
759 | 175k | } |
760 | | |
761 | | static void |
762 | | show_warning(PyThreadState *tstate, PyObject *filename, int lineno, |
763 | | PyObject *text, PyObject *category, PyObject *sourceline) |
764 | 0 | { |
765 | 0 | PyObject *f_stderr = NULL; |
766 | 0 | PyObject *name; |
767 | 0 | char lineno_str[128]; |
768 | |
|
769 | 0 | PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); |
770 | |
|
771 | 0 | name = PyObject_GetAttr(category, &_Py_ID(__name__)); |
772 | 0 | if (name == NULL) { |
773 | 0 | goto error; |
774 | 0 | } |
775 | | |
776 | 0 | if (PySys_GetOptionalAttr(&_Py_ID(stderr), &f_stderr) <= 0) { |
777 | 0 | fprintf(stderr, "lost sys.stderr\n"); |
778 | 0 | goto error; |
779 | 0 | } |
780 | | |
781 | | /* Print "filename:lineno: category: text\n" */ |
782 | 0 | if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0) |
783 | 0 | goto error; |
784 | 0 | if (PyFile_WriteString(lineno_str, f_stderr) < 0) |
785 | 0 | goto error; |
786 | 0 | if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0) |
787 | 0 | goto error; |
788 | 0 | if (PyFile_WriteString(": ", f_stderr) < 0) |
789 | 0 | goto error; |
790 | 0 | if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0) |
791 | 0 | goto error; |
792 | 0 | if (PyFile_WriteString("\n", f_stderr) < 0) |
793 | 0 | goto error; |
794 | 0 | Py_CLEAR(name); |
795 | | |
796 | | /* Print " source_line\n" */ |
797 | 0 | if (sourceline) { |
798 | 0 | int kind; |
799 | 0 | const void *data; |
800 | 0 | Py_ssize_t i, len; |
801 | 0 | Py_UCS4 ch; |
802 | 0 | PyObject *truncated; |
803 | |
|
804 | 0 | kind = PyUnicode_KIND(sourceline); |
805 | 0 | data = PyUnicode_DATA(sourceline); |
806 | 0 | len = PyUnicode_GET_LENGTH(sourceline); |
807 | 0 | for (i=0; i<len; i++) { |
808 | 0 | ch = PyUnicode_READ(kind, data, i); |
809 | 0 | if (ch != ' ' && ch != '\t' && ch != '\014') |
810 | 0 | break; |
811 | 0 | } |
812 | |
|
813 | 0 | truncated = PyUnicode_Substring(sourceline, i, len); |
814 | 0 | if (truncated == NULL) |
815 | 0 | goto error; |
816 | | |
817 | 0 | PyFile_WriteObject(sourceline, f_stderr, Py_PRINT_RAW); |
818 | 0 | Py_DECREF(truncated); |
819 | 0 | PyFile_WriteString("\n", f_stderr); |
820 | 0 | } |
821 | 0 | else { |
822 | 0 | _Py_DisplaySourceLine(f_stderr, filename, lineno, 2, NULL, NULL); |
823 | 0 | } |
824 | | |
825 | 0 | error: |
826 | 0 | Py_XDECREF(f_stderr); |
827 | 0 | Py_XDECREF(name); |
828 | 0 | PyErr_Clear(); |
829 | 0 | } |
830 | | |
831 | | static int |
832 | | call_show_warning(PyThreadState *tstate, PyObject *category, |
833 | | PyObject *text, PyObject *message, |
834 | | PyObject *filename, int lineno, PyObject *lineno_obj, |
835 | | PyObject *sourceline, PyObject *source, PyObject *module) |
836 | 175k | { |
837 | 175k | PyObject *show_fn, *msg, *res, *warnmsg_cls = NULL; |
838 | 175k | PyInterpreterState *interp = tstate->interp; |
839 | | |
840 | | /* The Python implementation is able to log the traceback where the source |
841 | | was allocated, whereas the C implementation doesn't. */ |
842 | 175k | show_fn = GET_WARNINGS_ATTR(interp, _showwarnmsg, 1); |
843 | 175k | if (show_fn == NULL) { |
844 | 0 | if (PyErr_Occurred()) |
845 | 0 | return -1; |
846 | 0 | show_warning(tstate, filename, lineno, text, category, sourceline); |
847 | 0 | return 0; |
848 | 0 | } |
849 | | |
850 | 175k | if (!PyCallable_Check(show_fn)) { |
851 | 0 | PyErr_SetString(PyExc_TypeError, |
852 | 0 | "warnings._showwarnmsg() must be set to a callable"); |
853 | 0 | goto error; |
854 | 0 | } |
855 | | |
856 | 175k | warnmsg_cls = GET_WARNINGS_ATTR(interp, WarningMessage, 0); |
857 | 175k | if (warnmsg_cls == NULL) { |
858 | 0 | if (!PyErr_Occurred()) { |
859 | 0 | PyErr_SetString(PyExc_RuntimeError, |
860 | 0 | "unable to get warnings.WarningMessage"); |
861 | 0 | } |
862 | 0 | goto error; |
863 | 0 | } |
864 | | |
865 | 175k | msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category, |
866 | 175k | filename, lineno_obj, Py_None, |
867 | 175k | sourceline ? sourceline : Py_None, |
868 | 175k | source ? source : Py_None, module, |
869 | 175k | NULL); |
870 | 175k | Py_DECREF(warnmsg_cls); |
871 | 175k | if (msg == NULL) |
872 | 0 | goto error; |
873 | | |
874 | 175k | res = PyObject_CallOneArg(show_fn, msg); |
875 | 175k | Py_DECREF(show_fn); |
876 | 175k | Py_DECREF(msg); |
877 | | |
878 | 175k | if (res == NULL) |
879 | 0 | return -1; |
880 | | |
881 | 175k | Py_DECREF(res); |
882 | 175k | return 0; |
883 | | |
884 | 0 | error: |
885 | 0 | Py_XDECREF(show_fn); |
886 | 0 | return -1; |
887 | 175k | } |
888 | | |
889 | | static PyObject * |
890 | | warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message, |
891 | | PyObject *filename, int lineno, |
892 | | PyObject *module, PyObject *registry, PyObject *sourceline, |
893 | | PyObject *source) |
894 | 870k | { |
895 | 870k | PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
896 | 870k | PyObject *item = NULL, *action = NULL; |
897 | 870k | PyObject *altkey = NULL, *once_registry = NULL; |
898 | 870k | int rc; |
899 | 870k | PyInterpreterState *interp = tstate->interp; |
900 | | |
901 | | /* module can be None if a warning is emitted late during Python shutdown. |
902 | | In this case, the Python warnings module was probably unloaded, filters |
903 | | are no more available to choose as action. It is safer to ignore the |
904 | | warning and do nothing. */ |
905 | 870k | if (module == Py_None) |
906 | 0 | Py_RETURN_NONE; |
907 | | |
908 | 870k | if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
909 | 0 | PyErr_SetString(PyExc_TypeError, "'registry' must be a dict or None"); |
910 | 0 | return NULL; |
911 | 0 | } |
912 | | |
913 | | /* Normalize message. */ |
914 | 870k | Py_INCREF(message); /* DECREF'ed in cleanup. */ |
915 | 870k | if (PyObject_TypeCheck(message, (PyTypeObject *)PyExc_Warning)) { |
916 | 0 | text = PyObject_Str(message); |
917 | 0 | if (text == NULL) |
918 | 0 | goto cleanup; |
919 | 0 | category = (PyObject*)Py_TYPE(message); |
920 | 0 | } |
921 | 870k | else { |
922 | 870k | text = message; |
923 | 870k | message = PyObject_CallOneArg(category, message); |
924 | 870k | if (message == NULL) |
925 | 0 | goto cleanup; |
926 | 870k | } |
927 | | |
928 | 870k | lineno_obj = PyLong_FromLong(lineno); |
929 | 870k | if (lineno_obj == NULL) |
930 | 0 | goto cleanup; |
931 | | |
932 | 870k | if (source == Py_None) { |
933 | 854k | source = NULL; |
934 | 854k | } |
935 | | |
936 | | /* Create key. */ |
937 | 870k | key = PyTuple_Pack(3, text, category, lineno_obj); |
938 | 870k | if (key == NULL) |
939 | 0 | goto cleanup; |
940 | | |
941 | | /* The warnings lock is not held. 'version' ties the registry updates |
942 | | to the filters searched; a result from filters that changed since is |
943 | | never recorded. */ |
944 | 870k | Py_ssize_t version; |
945 | 870k | if ((registry != NULL) && (registry != Py_None)) { |
946 | 854k | rc = already_warned(interp, registry, key, &version); |
947 | 854k | if (rc == -1) |
948 | 0 | goto cleanup; |
949 | 854k | else if (rc == 1) |
950 | 675k | goto return_none; |
951 | | /* Else this warning hasn't been generated before. */ |
952 | 854k | } |
953 | 16.0k | else { |
954 | 16.0k | registry = NULL; |
955 | 16.0k | WarningsState *st = warnings_get_state(interp); |
956 | 16.0k | version = FT_ATOMIC_LOAD_SSIZE_ACQUIRE(st->filters_version); |
957 | 16.0k | } |
958 | | |
959 | 195k | action = get_filter(interp, category, text, lineno, module, filename, &item); |
960 | 195k | if (action == NULL) |
961 | 0 | goto cleanup; |
962 | | |
963 | 195k | if (_PyUnicode_EqualToASCIIString(action, "error")) { |
964 | 0 | PyErr_SetObject(category, message); |
965 | 0 | goto cleanup; |
966 | 0 | } |
967 | | |
968 | 195k | if (_PyUnicode_EqualToASCIIString(action, "ignore")) { |
969 | 19.2k | goto return_none; |
970 | 19.2k | } |
971 | | |
972 | | /* Store in the registry that we've been here, *except* when the action |
973 | | is "always" or "all". */ |
974 | 175k | if (!_PyUnicode_EqualToASCIIString(action, "always") && !_PyUnicode_EqualToASCIIString(action, "all")) { |
975 | 175k | bool is_once = _PyUnicode_EqualToASCIIString(action, "once"); |
976 | 175k | bool is_module = _PyUnicode_EqualToASCIIString(action, "module"); |
977 | 175k | if (!is_once && !is_module && |
978 | 175k | !_PyUnicode_EqualToASCIIString(action, "default")) |
979 | 0 | { |
980 | 0 | PyErr_Format(PyExc_RuntimeError, |
981 | 0 | "Unrecognized action (%R) in warnings.filters:\n %R", |
982 | 0 | action, item); |
983 | 0 | goto cleanup; |
984 | 0 | } |
985 | | |
986 | 175k | if (is_once && registry == NULL) { |
987 | | /* WarningsState.once_registry[(text, category)] = 1 */ |
988 | 0 | once_registry = get_once_registry(interp); |
989 | 0 | if (once_registry == NULL) |
990 | 0 | goto cleanup; |
991 | 0 | altkey = _PyTuple_FromPair(text, category); |
992 | 0 | if (altkey == NULL) |
993 | 0 | goto cleanup; |
994 | 0 | rc = record_warning(interp, once_registry, NULL, altkey, version); |
995 | 0 | } |
996 | 175k | else if (registry != NULL) { |
997 | | /* registry[key] = 1, and for "once" and "module" also |
998 | | registry[(text, category)] = 1 */ |
999 | 175k | if (is_once || is_module) { |
1000 | 0 | altkey = _PyTuple_FromPair(text, category); |
1001 | 0 | if (altkey == NULL) |
1002 | 0 | goto cleanup; |
1003 | 0 | } |
1004 | 175k | rc = record_warning(interp, registry, key, altkey, version); |
1005 | 175k | } |
1006 | 0 | else { |
1007 | 0 | rc = 0; |
1008 | 0 | } |
1009 | 175k | if (rc == -1) |
1010 | 0 | goto cleanup; |
1011 | 175k | if (rc == 1) /* Already warned for this module. */ |
1012 | 0 | goto return_none; |
1013 | 175k | } |
1014 | | |
1015 | 175k | if (call_show_warning(tstate, category, text, message, filename, |
1016 | 175k | lineno, lineno_obj, sourceline, source, module) < 0) |
1017 | 0 | goto cleanup; |
1018 | | |
1019 | 870k | return_none: |
1020 | 870k | result = Py_NewRef(Py_None); |
1021 | | |
1022 | 870k | cleanup: |
1023 | 870k | Py_XDECREF(once_registry); |
1024 | 870k | Py_XDECREF(altkey); |
1025 | 870k | Py_XDECREF(action); |
1026 | 870k | Py_XDECREF(item); |
1027 | 870k | Py_XDECREF(key); |
1028 | 870k | Py_XDECREF(text); |
1029 | 870k | Py_XDECREF(lineno_obj); |
1030 | 870k | Py_XDECREF(message); |
1031 | 870k | return result; /* Py_None or NULL. */ |
1032 | 870k | } |
1033 | | |
1034 | | static PyObject * |
1035 | | get_frame_filename(PyFrameObject *frame) |
1036 | 23.6M | { |
1037 | 23.6M | PyCodeObject *code = PyFrame_GetCode(frame); |
1038 | 23.6M | PyObject *filename = code->co_filename; |
1039 | 23.6M | Py_DECREF(code); |
1040 | 23.6M | return filename; |
1041 | 23.6M | } |
1042 | | |
1043 | | static bool |
1044 | | is_internal_filename(PyObject *filename) |
1045 | 23.6M | { |
1046 | 23.6M | if (!PyUnicode_Check(filename)) { |
1047 | 0 | return false; |
1048 | 0 | } |
1049 | | |
1050 | 23.6M | int contains = PyUnicode_Contains(filename, &_Py_ID(importlib)); |
1051 | 23.6M | if (contains < 0) { |
1052 | 0 | return false; |
1053 | 0 | } |
1054 | 23.6M | else if (contains > 0) { |
1055 | 0 | contains = PyUnicode_Contains(filename, &_Py_ID(_bootstrap)); |
1056 | 0 | if (contains < 0) { |
1057 | 0 | return false; |
1058 | 0 | } |
1059 | 0 | else if (contains > 0) { |
1060 | 0 | return true; |
1061 | 0 | } |
1062 | 0 | } |
1063 | | |
1064 | 23.6M | return false; |
1065 | 23.6M | } |
1066 | | |
1067 | | static bool |
1068 | | is_filename_to_skip(PyObject *filename, PyTupleObject *skip_file_prefixes) |
1069 | 22.8M | { |
1070 | 22.8M | if (skip_file_prefixes) { |
1071 | 588 | if (!PyUnicode_Check(filename)) { |
1072 | 0 | return false; |
1073 | 0 | } |
1074 | | |
1075 | 588 | Py_ssize_t prefixes = PyTuple_GET_SIZE(skip_file_prefixes); |
1076 | 784 | for (Py_ssize_t idx = 0; idx < prefixes; ++idx) |
1077 | 588 | { |
1078 | 588 | PyObject *prefix = PyTuple_GET_ITEM(skip_file_prefixes, idx); |
1079 | 588 | Py_ssize_t found = PyUnicode_Tailmatch(filename, prefix, |
1080 | 588 | 0, PY_SSIZE_T_MAX, -1); |
1081 | 588 | if (found == 1) { |
1082 | 392 | return true; |
1083 | 392 | } |
1084 | 196 | if (found < 0) { |
1085 | 0 | return false; |
1086 | 0 | } |
1087 | 196 | } |
1088 | 588 | } |
1089 | 22.8M | return false; |
1090 | 22.8M | } |
1091 | | |
1092 | | static bool |
1093 | | is_internal_frame(PyFrameObject *frame) |
1094 | 854k | { |
1095 | 854k | if (frame == NULL) { |
1096 | 0 | return false; |
1097 | 0 | } |
1098 | | |
1099 | 854k | PyObject *filename = get_frame_filename(frame); |
1100 | 854k | if (filename == NULL) { |
1101 | 0 | return false; |
1102 | 0 | } |
1103 | | |
1104 | 854k | return is_internal_filename(filename); |
1105 | 854k | } |
1106 | | |
1107 | | static PyFrameObject * |
1108 | | next_external_frame(PyFrameObject *frame, PyTupleObject *skip_file_prefixes) |
1109 | 22.8M | { |
1110 | 22.8M | PyObject *frame_filename; |
1111 | 22.8M | do { |
1112 | 22.8M | PyFrameObject *back = PyFrame_GetBack(frame); |
1113 | 22.8M | Py_SETREF(frame, back); |
1114 | 22.8M | } while (frame != NULL && (frame_filename = get_frame_filename(frame)) && |
1115 | 22.8M | (is_internal_filename(frame_filename) || |
1116 | 22.8M | is_filename_to_skip(frame_filename, skip_file_prefixes))); |
1117 | | |
1118 | 22.8M | return frame; |
1119 | 22.8M | } |
1120 | | |
1121 | | /* filename, module, and registry are new refs, globals is borrowed */ |
1122 | | /* skip_file_prefixes is either NULL or a tuple of strs. */ |
1123 | | /* Returns 0 on error (no new refs), 1 on success */ |
1124 | | static int |
1125 | | setup_context(Py_ssize_t stack_level, |
1126 | | PyTupleObject *skip_file_prefixes, |
1127 | | PyObject **filename, int *lineno, |
1128 | | PyObject **module, PyObject **registry) |
1129 | 854k | { |
1130 | 854k | PyObject *globals; |
1131 | | |
1132 | | /* Setup globals, filename and lineno. */ |
1133 | 854k | PyThreadState *tstate = get_current_tstate(); |
1134 | 854k | if (tstate == NULL) { |
1135 | 0 | return 0; |
1136 | 0 | } |
1137 | 854k | if (skip_file_prefixes) { |
1138 | | /* Type check our data structure up front. Later code that uses it |
1139 | | * isn't structured to report errors. */ |
1140 | 196 | Py_ssize_t prefixes = PyTuple_GET_SIZE(skip_file_prefixes); |
1141 | 392 | for (Py_ssize_t idx = 0; idx < prefixes; ++idx) |
1142 | 196 | { |
1143 | 196 | PyObject *prefix = PyTuple_GET_ITEM(skip_file_prefixes, idx); |
1144 | 196 | if (!PyUnicode_Check(prefix)) { |
1145 | 0 | PyErr_Format(PyExc_TypeError, |
1146 | 0 | "Found non-str '%s' in skip_file_prefixes.", |
1147 | 0 | Py_TYPE(prefix)->tp_name); |
1148 | 0 | return 0; |
1149 | 0 | } |
1150 | 196 | } |
1151 | 196 | } |
1152 | 854k | PyInterpreterState *interp = tstate->interp; |
1153 | 854k | PyFrameObject *f = PyThreadState_GetFrame(tstate); |
1154 | | // Stack level comparisons to Python code is off by one as there is no |
1155 | | // warnings-related stack level to avoid. |
1156 | 854k | if (stack_level <= 0 || is_internal_frame(f)) { |
1157 | 0 | while (--stack_level > 0 && f != NULL) { |
1158 | 0 | PyFrameObject *back = PyFrame_GetBack(f); |
1159 | 0 | Py_SETREF(f, back); |
1160 | 0 | } |
1161 | 0 | } |
1162 | 854k | else { |
1163 | 23.6M | while (--stack_level > 0 && f != NULL) { |
1164 | 22.8M | f = next_external_frame(f, skip_file_prefixes); |
1165 | 22.8M | } |
1166 | 854k | } |
1167 | | |
1168 | 854k | if (f == NULL) { |
1169 | 0 | globals = interp->sysdict; |
1170 | 0 | *filename = PyUnicode_FromString("<sys>"); |
1171 | 0 | if (*filename == NULL) { |
1172 | 0 | return 0; |
1173 | 0 | } |
1174 | 0 | *lineno = 0; |
1175 | 0 | } |
1176 | 854k | else { |
1177 | 854k | globals = f->f_frame->f_globals; |
1178 | 854k | *filename = Py_NewRef(_PyFrame_GetCode(f->f_frame)->co_filename); |
1179 | 854k | *lineno = PyFrame_GetLineNumber(f); |
1180 | 854k | Py_DECREF(f); |
1181 | 854k | } |
1182 | | |
1183 | 854k | *module = NULL; |
1184 | | |
1185 | | /* Setup registry. */ |
1186 | 854k | assert(globals != NULL); |
1187 | 854k | assert(PyAnyDict_Check(globals)); |
1188 | 854k | int rc = PyDict_GetItemRef(globals, &_Py_ID(__warningregistry__), |
1189 | 854k | registry); |
1190 | 854k | if (rc < 0) { |
1191 | 0 | goto handle_error; |
1192 | 0 | } |
1193 | 854k | if (*registry == NULL) { |
1194 | 8 | *registry = PyDict_New(); |
1195 | 8 | if (*registry == NULL) |
1196 | 0 | goto handle_error; |
1197 | | |
1198 | 8 | rc = PyDict_SetItem(globals, &_Py_ID(__warningregistry__), *registry); |
1199 | 8 | if (rc < 0) |
1200 | 0 | goto handle_error; |
1201 | 8 | } |
1202 | | |
1203 | | /* Setup module. */ |
1204 | 854k | rc = PyDict_GetItemRef(globals, &_Py_ID(__name__), module); |
1205 | 854k | if (rc < 0) { |
1206 | 0 | goto handle_error; |
1207 | 0 | } |
1208 | 854k | if (rc > 0) { |
1209 | 854k | if (Py_IsNone(*module) || PyUnicode_Check(*module)) { |
1210 | 854k | return 1; |
1211 | 854k | } |
1212 | 0 | Py_DECREF(*module); |
1213 | 0 | } |
1214 | 0 | *module = PyUnicode_FromString("<string>"); |
1215 | 0 | if (*module == NULL) { |
1216 | 0 | goto handle_error; |
1217 | 0 | } |
1218 | | |
1219 | 0 | return 1; |
1220 | | |
1221 | 0 | handle_error: |
1222 | 0 | Py_XDECREF(*registry); |
1223 | 0 | Py_XDECREF(*module); |
1224 | 0 | Py_DECREF(*filename); |
1225 | 0 | return 0; |
1226 | 0 | } |
1227 | | |
1228 | | static PyObject * |
1229 | | get_category(PyObject *message, PyObject *category) |
1230 | 854k | { |
1231 | 854k | if (PyObject_TypeCheck(message, (PyTypeObject *)PyExc_Warning)) { |
1232 | | /* Ignore the category argument. */ |
1233 | 0 | return (PyObject*)Py_TYPE(message); |
1234 | 0 | } |
1235 | 854k | if (category == NULL || category == Py_None) { |
1236 | 311 | return PyExc_UserWarning; |
1237 | 311 | } |
1238 | | |
1239 | | /* Validate category. */ |
1240 | 854k | if (!PyType_Check(category)) { |
1241 | 0 | PyErr_Format(PyExc_TypeError, |
1242 | 0 | "category must be a Warning subclass, not '%T'", |
1243 | 0 | category); |
1244 | 0 | return NULL; |
1245 | 0 | } |
1246 | 854k | if (!PyType_IsSubtype((PyTypeObject *)category, (PyTypeObject *)PyExc_Warning)) { |
1247 | 0 | PyErr_Format(PyExc_TypeError, |
1248 | 0 | "category must be a Warning subclass, not class '%N'", |
1249 | 0 | (PyTypeObject *)category); |
1250 | 0 | return NULL; |
1251 | 0 | } |
1252 | | |
1253 | 854k | return category; |
1254 | 854k | } |
1255 | | |
1256 | | static PyObject * |
1257 | | do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level, |
1258 | | PyObject *source, PyTupleObject *skip_file_prefixes) |
1259 | 854k | { |
1260 | 854k | PyObject *filename, *module, *registry, *res; |
1261 | 854k | int lineno; |
1262 | | |
1263 | 854k | PyThreadState *tstate = get_current_tstate(); |
1264 | 854k | if (tstate == NULL) { |
1265 | 0 | return NULL; |
1266 | 0 | } |
1267 | | |
1268 | 854k | if (!setup_context(stack_level, skip_file_prefixes, |
1269 | 854k | &filename, &lineno, &module, ®istry)) |
1270 | 0 | return NULL; |
1271 | | |
1272 | 854k | res = warn_explicit(tstate, category, message, filename, lineno, module, registry, |
1273 | 854k | NULL, source); |
1274 | 854k | Py_DECREF(filename); |
1275 | 854k | Py_DECREF(registry); |
1276 | 854k | Py_DECREF(module); |
1277 | 854k | return res; |
1278 | 854k | } |
1279 | | |
1280 | | /*[clinic input] |
1281 | | warn as warnings_warn |
1282 | | |
1283 | | message: object |
1284 | | Text of the warning message. |
1285 | | category: object = None |
1286 | | The Warning category subclass. Defaults to UserWarning. |
1287 | | stacklevel: Py_ssize_t = 1 |
1288 | | How far up the call stack to make this warning appear. A value of 2 for |
1289 | | example attributes the warning to the caller of the code calling warn(). |
1290 | | source: object = None |
1291 | | If supplied, the destroyed object which emitted a ResourceWarning |
1292 | | * |
1293 | | skip_file_prefixes: object(type='PyTupleObject *', subclass_of='&PyTuple_Type', c_default='NULL') = () |
1294 | | An optional tuple of module filename prefixes indicating frames to skip |
1295 | | during stacklevel computations for stack frame attribution. |
1296 | | |
1297 | | Issue a warning, or maybe ignore it or raise an exception. |
1298 | | [clinic start generated code]*/ |
1299 | | |
1300 | | static PyObject * |
1301 | | warnings_warn_impl(PyObject *module, PyObject *message, PyObject *category, |
1302 | | Py_ssize_t stacklevel, PyObject *source, |
1303 | | PyTupleObject *skip_file_prefixes) |
1304 | | /*[clinic end generated code: output=a68e0f6906c65f80 input=2b52e8b20f508f51]*/ |
1305 | 854k | { |
1306 | 854k | category = get_category(message, category); |
1307 | 854k | if (category == NULL) |
1308 | 0 | return NULL; |
1309 | 854k | if (skip_file_prefixes) { |
1310 | 196 | if (PyTuple_GET_SIZE(skip_file_prefixes) > 0) { |
1311 | 196 | if (stacklevel < 2) { |
1312 | 196 | stacklevel = 2; |
1313 | 196 | } |
1314 | 196 | } else { |
1315 | 0 | skip_file_prefixes = NULL; |
1316 | 0 | } |
1317 | 196 | } |
1318 | 854k | return do_warn(message, category, stacklevel, source, skip_file_prefixes); |
1319 | 854k | } |
1320 | | |
1321 | | static PyObject * |
1322 | | get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno) |
1323 | 0 | { |
1324 | 0 | PyObject *loader; |
1325 | 0 | PyObject *module_name; |
1326 | 0 | PyObject *get_source; |
1327 | 0 | PyObject *source; |
1328 | 0 | PyObject *source_list; |
1329 | 0 | PyObject *source_line; |
1330 | | |
1331 | | /* stolen from import.c */ |
1332 | 0 | loader = _PyImport_BlessMyLoader(interp, module_globals); |
1333 | 0 | if (loader == NULL) { |
1334 | 0 | return NULL; |
1335 | 0 | } |
1336 | | |
1337 | | /* Prefer __spec__.name: __name__ is "__main__" for the module executed |
1338 | | as a script, but the loader can only handle its own module name. */ |
1339 | 0 | PyObject *spec; |
1340 | 0 | if (PyDict_GetItemRef(module_globals, &_Py_ID(__spec__), &spec) < 0) { |
1341 | 0 | Py_DECREF(loader); |
1342 | 0 | return NULL; |
1343 | 0 | } |
1344 | 0 | module_name = NULL; |
1345 | 0 | if (spec != NULL) { |
1346 | 0 | int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(name), &module_name); |
1347 | 0 | Py_DECREF(spec); |
1348 | 0 | if (rc < 0) { |
1349 | 0 | Py_DECREF(loader); |
1350 | 0 | return NULL; |
1351 | 0 | } |
1352 | 0 | if (module_name == Py_None) { |
1353 | 0 | Py_CLEAR(module_name); |
1354 | 0 | } |
1355 | 0 | } |
1356 | 0 | if (module_name == NULL) { |
1357 | 0 | int rc = PyDict_GetItemRef(module_globals, &_Py_ID(__name__), |
1358 | 0 | &module_name); |
1359 | 0 | if (rc <= 0) { // not found or error |
1360 | 0 | Py_DECREF(loader); |
1361 | 0 | return NULL; |
1362 | 0 | } |
1363 | 0 | } |
1364 | | |
1365 | | /* Make sure the loader implements the optional get_source() method. */ |
1366 | 0 | (void)PyObject_GetOptionalAttr(loader, &_Py_ID(get_source), &get_source); |
1367 | 0 | Py_DECREF(loader); |
1368 | 0 | if (!get_source) { |
1369 | 0 | Py_DECREF(module_name); |
1370 | 0 | return NULL; |
1371 | 0 | } |
1372 | | /* Call get_source() to get the source code. */ |
1373 | 0 | source = PyObject_CallOneArg(get_source, module_name); |
1374 | 0 | Py_DECREF(get_source); |
1375 | 0 | Py_DECREF(module_name); |
1376 | 0 | if (!source) { |
1377 | | /* The source line is optional: the loader can be unable to provide |
1378 | | the source of the module, for example if it is not its loader. */ |
1379 | 0 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
1380 | 0 | PyErr_Clear(); |
1381 | 0 | } |
1382 | 0 | return NULL; |
1383 | 0 | } |
1384 | 0 | if (source == Py_None) { |
1385 | 0 | Py_DECREF(source); |
1386 | 0 | return NULL; |
1387 | 0 | } |
1388 | | |
1389 | | /* Split the source into lines. */ |
1390 | 0 | source_list = PyUnicode_Splitlines(source, 0); |
1391 | 0 | Py_DECREF(source); |
1392 | 0 | if (!source_list) { |
1393 | 0 | return NULL; |
1394 | 0 | } |
1395 | | |
1396 | | /* Get the source line. */ |
1397 | 0 | if (lineno < 1 || lineno > PyList_GET_SIZE(source_list)) { |
1398 | 0 | Py_DECREF(source_list); |
1399 | 0 | return NULL; |
1400 | 0 | } |
1401 | 0 | source_line = Py_NewRef(PyList_GET_ITEM(source_list, lineno-1)); |
1402 | 0 | Py_DECREF(source_list); |
1403 | 0 | return source_line; |
1404 | 0 | } |
1405 | | |
1406 | | /*[clinic input] |
1407 | | warn_explicit as warnings_warn_explicit |
1408 | | |
1409 | | message: object |
1410 | | category: object |
1411 | | filename: unicode |
1412 | | lineno: int |
1413 | | module as mod: object = NULL |
1414 | | registry: object = None |
1415 | | module_globals: object = None |
1416 | | source as sourceobj: object = None |
1417 | | |
1418 | | Issue a warning, or maybe ignore it or raise an exception. |
1419 | | [clinic start generated code]*/ |
1420 | | |
1421 | | static PyObject * |
1422 | | warnings_warn_explicit_impl(PyObject *module, PyObject *message, |
1423 | | PyObject *category, PyObject *filename, |
1424 | | int lineno, PyObject *mod, PyObject *registry, |
1425 | | PyObject *module_globals, PyObject *sourceobj) |
1426 | | /*[clinic end generated code: output=c49c62b15a49a186 input=df6eeb8b45e712f1]*/ |
1427 | 0 | { |
1428 | 0 | PyObject *source_line = NULL; |
1429 | 0 | PyObject *returned; |
1430 | |
|
1431 | 0 | PyThreadState *tstate = get_current_tstate(); |
1432 | 0 | if (tstate == NULL) { |
1433 | 0 | return NULL; |
1434 | 0 | } |
1435 | | |
1436 | 0 | if (module_globals && module_globals != Py_None) { |
1437 | 0 | if (!PyAnyDict_Check(module_globals)) { |
1438 | 0 | PyErr_Format(PyExc_TypeError, |
1439 | 0 | "module_globals must be a dict or a frozendict, " |
1440 | 0 | "not %T", |
1441 | 0 | module_globals); |
1442 | 0 | return NULL; |
1443 | 0 | } |
1444 | | |
1445 | 0 | source_line = get_source_line(tstate->interp, module_globals, lineno); |
1446 | 0 | if (source_line == NULL && PyErr_Occurred()) { |
1447 | 0 | return NULL; |
1448 | 0 | } |
1449 | 0 | } |
1450 | | |
1451 | 0 | returned = warn_explicit(tstate, category, message, filename, lineno, |
1452 | 0 | mod, registry, source_line, sourceobj); |
1453 | 0 | Py_XDECREF(source_line); |
1454 | 0 | return returned; |
1455 | 0 | } |
1456 | | |
1457 | | /*[clinic input] |
1458 | | _filters_mutated_lock_held as warnings_filters_mutated_lock_held |
1459 | | |
1460 | | [clinic start generated code]*/ |
1461 | | |
1462 | | static PyObject * |
1463 | | warnings_filters_mutated_lock_held_impl(PyObject *module) |
1464 | | /*[clinic end generated code: output=df5c84f044e856ec input=34208bf03d70e432]*/ |
1465 | 1.24k | { |
1466 | 1.24k | PyInterpreterState *interp = get_current_interp(); |
1467 | 1.24k | if (interp == NULL) { |
1468 | 0 | return NULL; |
1469 | 0 | } |
1470 | | |
1471 | 1.24k | WarningsState *st = warnings_get_state(interp); |
1472 | 1.24k | assert(st != NULL); |
1473 | | |
1474 | | // Note that the lock must be held by the caller. |
1475 | 1.24k | if (!warnings_lock_held(st)) { |
1476 | 0 | PyErr_SetString(PyExc_RuntimeError, "warnings lock is not held"); |
1477 | 0 | return NULL; |
1478 | 0 | } |
1479 | | |
1480 | 1.24k | FT_ATOMIC_ADD_SSIZE(st->filters_version, 1); |
1481 | | |
1482 | 1.24k | Py_RETURN_NONE; |
1483 | 1.24k | } |
1484 | | |
1485 | | /* Function to issue a warning message; may raise an exception. */ |
1486 | | |
1487 | | static int |
1488 | | warn_unicode(PyObject *category, PyObject *message, |
1489 | | Py_ssize_t stack_level, PyObject *source) |
1490 | 222 | { |
1491 | 222 | PyObject *res; |
1492 | | |
1493 | 222 | if (category == NULL) |
1494 | 0 | category = PyExc_RuntimeWarning; |
1495 | | |
1496 | 222 | res = do_warn(message, category, stack_level, source, NULL); |
1497 | 222 | if (res == NULL) |
1498 | 0 | return -1; |
1499 | 222 | Py_DECREF(res); |
1500 | | |
1501 | 222 | return 0; |
1502 | 222 | } |
1503 | | |
1504 | | static int |
1505 | | _PyErr_WarnFormatV(PyObject *source, |
1506 | | PyObject *category, Py_ssize_t stack_level, |
1507 | | const char *format, va_list vargs) |
1508 | 222 | { |
1509 | 222 | PyObject *message; |
1510 | 222 | int res; |
1511 | | |
1512 | 222 | message = PyUnicode_FromFormatV(format, vargs); |
1513 | 222 | if (message == NULL) |
1514 | 0 | return -1; |
1515 | | |
1516 | 222 | res = warn_unicode(category, message, stack_level, source); |
1517 | 222 | Py_DECREF(message); |
1518 | 222 | return res; |
1519 | 222 | } |
1520 | | |
1521 | | int |
1522 | | PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, |
1523 | | const char *format, ...) |
1524 | 222 | { |
1525 | 222 | int res; |
1526 | 222 | va_list vargs; |
1527 | | |
1528 | 222 | va_start(vargs, format); |
1529 | 222 | res = _PyErr_WarnFormatV(NULL, category, stack_level, format, vargs); |
1530 | 222 | va_end(vargs); |
1531 | 222 | return res; |
1532 | 222 | } |
1533 | | |
1534 | | static int |
1535 | | _PyErr_WarnFormat(PyObject *source, PyObject *category, Py_ssize_t stack_level, |
1536 | | const char *format, ...) |
1537 | 0 | { |
1538 | 0 | int res; |
1539 | 0 | va_list vargs; |
1540 | |
|
1541 | 0 | va_start(vargs, format); |
1542 | 0 | res = _PyErr_WarnFormatV(source, category, stack_level, format, vargs); |
1543 | 0 | va_end(vargs); |
1544 | 0 | return res; |
1545 | 0 | } |
1546 | | |
1547 | | int |
1548 | | PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, |
1549 | | const char *format, ...) |
1550 | 0 | { |
1551 | 0 | int res; |
1552 | 0 | va_list vargs; |
1553 | |
|
1554 | 0 | va_start(vargs, format); |
1555 | 0 | res = _PyErr_WarnFormatV(source, PyExc_ResourceWarning, |
1556 | 0 | stack_level, format, vargs); |
1557 | 0 | va_end(vargs); |
1558 | 0 | return res; |
1559 | 0 | } |
1560 | | |
1561 | | |
1562 | | int |
1563 | | PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
1564 | 0 | { |
1565 | 0 | int ret; |
1566 | 0 | PyObject *message = PyUnicode_FromString(text); |
1567 | 0 | if (message == NULL) |
1568 | 0 | return -1; |
1569 | 0 | ret = warn_unicode(category, message, stack_level, NULL); |
1570 | 0 | Py_DECREF(message); |
1571 | 0 | return ret; |
1572 | 0 | } |
1573 | | |
1574 | | /* PyErr_Warn is only for backwards compatibility and will be removed. |
1575 | | Use PyErr_WarnEx instead. */ |
1576 | | |
1577 | | #undef PyErr_Warn |
1578 | | |
1579 | | int |
1580 | | PyErr_Warn(PyObject *category, const char *text) |
1581 | 0 | { |
1582 | 0 | return PyErr_WarnEx(category, text, 1); |
1583 | 0 | } |
1584 | | |
1585 | | /* Warning with explicit origin */ |
1586 | | int |
1587 | | PyErr_WarnExplicitObject(PyObject *category, PyObject *message, |
1588 | | PyObject *filename, int lineno, |
1589 | | PyObject *module, PyObject *registry) |
1590 | 16.0k | { |
1591 | 16.0k | PyObject *res; |
1592 | 16.0k | if (category == NULL) |
1593 | 0 | category = PyExc_RuntimeWarning; |
1594 | 16.0k | PyThreadState *tstate = get_current_tstate(); |
1595 | 16.0k | if (tstate == NULL) { |
1596 | 0 | return -1; |
1597 | 0 | } |
1598 | | |
1599 | 16.0k | res = warn_explicit(tstate, category, message, filename, lineno, |
1600 | 16.0k | module, registry, NULL, NULL); |
1601 | 16.0k | if (res == NULL) |
1602 | 0 | return -1; |
1603 | 16.0k | Py_DECREF(res); |
1604 | 16.0k | return 0; |
1605 | 16.0k | } |
1606 | | |
1607 | | int |
1608 | | PyErr_WarnExplicit(PyObject *category, const char *text, |
1609 | | const char *filename_str, int lineno, |
1610 | | const char *module_str, PyObject *registry) |
1611 | 0 | { |
1612 | 0 | PyObject *message = PyUnicode_FromString(text); |
1613 | 0 | if (message == NULL) { |
1614 | 0 | return -1; |
1615 | 0 | } |
1616 | 0 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
1617 | 0 | if (filename == NULL) { |
1618 | 0 | Py_DECREF(message); |
1619 | 0 | return -1; |
1620 | 0 | } |
1621 | 0 | PyObject *module = NULL; |
1622 | 0 | if (module_str != NULL) { |
1623 | 0 | module = PyUnicode_FromString(module_str); |
1624 | 0 | if (module == NULL) { |
1625 | 0 | Py_DECREF(filename); |
1626 | 0 | Py_DECREF(message); |
1627 | 0 | return -1; |
1628 | 0 | } |
1629 | 0 | } |
1630 | | |
1631 | 0 | int ret = PyErr_WarnExplicitObject(category, message, filename, lineno, |
1632 | 0 | module, registry); |
1633 | 0 | Py_XDECREF(module); |
1634 | 0 | Py_DECREF(filename); |
1635 | 0 | Py_DECREF(message); |
1636 | 0 | return ret; |
1637 | 0 | } |
1638 | | |
1639 | | int |
1640 | | PyErr_WarnExplicitFormat(PyObject *category, |
1641 | | const char *filename_str, int lineno, |
1642 | | const char *module_str, PyObject *registry, |
1643 | | const char *format, ...) |
1644 | 0 | { |
1645 | 0 | PyObject *message; |
1646 | 0 | PyObject *module = NULL; |
1647 | 0 | PyObject *filename = PyUnicode_DecodeFSDefault(filename_str); |
1648 | 0 | int ret = -1; |
1649 | 0 | va_list vargs; |
1650 | |
|
1651 | 0 | if (filename == NULL) |
1652 | 0 | goto exit; |
1653 | 0 | if (module_str != NULL) { |
1654 | 0 | module = PyUnicode_FromString(module_str); |
1655 | 0 | if (module == NULL) |
1656 | 0 | goto exit; |
1657 | 0 | } |
1658 | | |
1659 | 0 | va_start(vargs, format); |
1660 | 0 | message = PyUnicode_FromFormatV(format, vargs); |
1661 | 0 | if (message != NULL) { |
1662 | 0 | PyObject *res; |
1663 | 0 | PyThreadState *tstate = get_current_tstate(); |
1664 | 0 | if (tstate != NULL) { |
1665 | 0 | res = warn_explicit(tstate, category, message, filename, lineno, |
1666 | 0 | module, registry, NULL, NULL); |
1667 | 0 | Py_DECREF(message); |
1668 | 0 | if (res != NULL) { |
1669 | 0 | Py_DECREF(res); |
1670 | 0 | ret = 0; |
1671 | 0 | } |
1672 | 0 | } |
1673 | 0 | } |
1674 | 0 | va_end(vargs); |
1675 | 0 | exit: |
1676 | 0 | Py_XDECREF(module); |
1677 | 0 | Py_XDECREF(filename); |
1678 | 0 | return ret; |
1679 | 0 | } |
1680 | | |
1681 | | void |
1682 | | _PyErr_WarnUnawaitedAgenMethod(PyAsyncGenObject *agen, PyObject *method) |
1683 | 0 | { |
1684 | 0 | PyObject *exc = PyErr_GetRaisedException(); |
1685 | 0 | if (_PyErr_WarnFormat((PyObject *)agen, PyExc_RuntimeWarning, 1, |
1686 | 0 | "coroutine method %R of %R was never awaited", |
1687 | 0 | method, agen->ag_qualname) < 0) |
1688 | 0 | { |
1689 | 0 | PyErr_FormatUnraisable("Exception ignored while " |
1690 | 0 | "finalizing async generator %R", agen); |
1691 | 0 | } |
1692 | 0 | PyErr_SetRaisedException(exc); |
1693 | 0 | } |
1694 | | |
1695 | | |
1696 | | void |
1697 | | _PyErr_WarnUnawaitedCoroutine(PyObject *coro) |
1698 | 0 | { |
1699 | | /* First, we attempt to funnel the warning through |
1700 | | warnings._warn_unawaited_coroutine. |
1701 | | |
1702 | | This could raise an exception, due to: |
1703 | | - a bug |
1704 | | - some kind of shutdown-related brokenness |
1705 | | - succeeding, but with an "error" warning filter installed, so the |
1706 | | warning is converted into a RuntimeWarning exception |
1707 | | |
1708 | | In the first two cases, we want to print the error (so we know what it |
1709 | | is!), and then print a warning directly as a fallback. In the last |
1710 | | case, we want to print the error (since it's the warning!), but *not* |
1711 | | do a fallback. And after we print the error we can't check for what |
1712 | | type of error it was (because PyErr_WriteUnraisable clears it), so we |
1713 | | need a flag to keep track. |
1714 | | |
1715 | | Since this is called from __del__ context, it's careful to never raise |
1716 | | an exception. |
1717 | | */ |
1718 | 0 | int warned = 0; |
1719 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
1720 | 0 | assert(interp != NULL); |
1721 | 0 | PyObject *fn = GET_WARNINGS_ATTR(interp, _warn_unawaited_coroutine, 1); |
1722 | 0 | if (fn) { |
1723 | 0 | PyObject *res = PyObject_CallOneArg(fn, coro); |
1724 | 0 | Py_DECREF(fn); |
1725 | 0 | if (res || PyErr_ExceptionMatches(PyExc_RuntimeWarning)) { |
1726 | 0 | warned = 1; |
1727 | 0 | } |
1728 | 0 | Py_XDECREF(res); |
1729 | 0 | } |
1730 | |
|
1731 | 0 | if (PyErr_Occurred()) { |
1732 | 0 | PyErr_FormatUnraisable("Exception ignored while " |
1733 | 0 | "finalizing coroutine %R", coro); |
1734 | 0 | } |
1735 | |
|
1736 | 0 | if (!warned) { |
1737 | 0 | if (_PyErr_WarnFormat(coro, PyExc_RuntimeWarning, 1, |
1738 | 0 | "coroutine '%S' was never awaited", |
1739 | 0 | ((PyCoroObject *)coro)->cr_qualname) < 0) |
1740 | 0 | { |
1741 | 0 | PyErr_FormatUnraisable("Exception ignored while " |
1742 | 0 | "finalizing coroutine %R", coro); |
1743 | 0 | } |
1744 | 0 | } |
1745 | 0 | } |
1746 | | |
1747 | | static PyMethodDef warnings_functions[] = { |
1748 | | WARNINGS_WARN_METHODDEF |
1749 | | WARNINGS_WARN_EXPLICIT_METHODDEF |
1750 | | WARNINGS_FILTERS_MUTATED_LOCK_HELD_METHODDEF |
1751 | | WARNINGS_ACQUIRE_LOCK_METHODDEF |
1752 | | WARNINGS_RELEASE_LOCK_METHODDEF |
1753 | | /* XXX(brett.cannon): add showwarning? */ |
1754 | | /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
1755 | | {NULL, NULL} /* sentinel */ |
1756 | | }; |
1757 | | |
1758 | | |
1759 | | static int |
1760 | | warnings_module_exec(PyObject *module) |
1761 | 37 | { |
1762 | 37 | PyInterpreterState *interp = get_current_interp(); |
1763 | 37 | if (interp == NULL) { |
1764 | 0 | return -1; |
1765 | 0 | } |
1766 | 37 | WarningsState *st = warnings_get_state(interp); |
1767 | 37 | if (st == NULL) { |
1768 | 0 | return -1; |
1769 | 0 | } |
1770 | 37 | if (PyModule_AddObjectRef(module, "filters", st->filters) < 0) { |
1771 | 0 | return -1; |
1772 | 0 | } |
1773 | 37 | if (PyModule_AddObjectRef(module, "_onceregistry", st->once_registry) < 0) { |
1774 | 0 | return -1; |
1775 | 0 | } |
1776 | 37 | if (PyModule_AddObjectRef(module, "_defaultaction", st->default_action) < 0) { |
1777 | 0 | return -1; |
1778 | 0 | } |
1779 | 37 | if (PyModule_AddObjectRef(module, "_warnings_context", st->context) < 0) { |
1780 | 0 | return -1; |
1781 | 0 | } |
1782 | 37 | return 0; |
1783 | 37 | } |
1784 | | |
1785 | | |
1786 | | static PyModuleDef_Slot warnings_slots[] = { |
1787 | | _Py_ABI_SLOT, |
1788 | | {Py_mod_exec, warnings_module_exec}, |
1789 | | {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
1790 | | {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
1791 | | {0, NULL} |
1792 | | }; |
1793 | | |
1794 | | static struct PyModuleDef warnings_module = { |
1795 | | PyModuleDef_HEAD_INIT, |
1796 | | .m_name = MODULE_NAME, |
1797 | | .m_doc = warnings__doc__, |
1798 | | .m_size = 0, |
1799 | | .m_methods = warnings_functions, |
1800 | | .m_slots = warnings_slots, |
1801 | | }; |
1802 | | |
1803 | | |
1804 | | PyMODINIT_FUNC |
1805 | | _PyWarnings_Init(void) |
1806 | 37 | { |
1807 | 37 | return PyModuleDef_Init(&warnings_module); |
1808 | 37 | } |
1809 | | |
1810 | | // We need this to ensure that warnings still work until late in finalization. |
1811 | | void |
1812 | | _PyWarnings_Fini(PyInterpreterState *interp) |
1813 | 0 | { |
1814 | 0 | warnings_clear_state(&interp->warnings); |
1815 | 0 | } |