MCPcopy Create free account
hub / github.com/python/cpython / warnings_state

Function warnings_state

Lib/test/test_warnings/__init__.py:37–64  ·  view source on GitHub ↗

Use a specific warnings implementation in warning_tests.

(module)

Source from the content-addressed store, hash-verified

35
36@contextmanager
37def warnings_state(module):
38 """Use a specific warnings implementation in warning_tests."""
39 global __warningregistry__
40 for to_clear in (sys, warning_tests):
41 try:
42 to_clear.__warningregistry__.clear()
43 except AttributeError:
44 pass
45 try:
46 __warningregistry__.clear()
47 except NameError:
48 pass
49 original_warnings = warning_tests.warnings
50 if module._use_context:
51 saved_context, context = module._new_context()
52 else:
53 original_filters = module.filters
54 module.filters = original_filters[:]
55 try:
56 module.simplefilter("once")
57 warning_tests.warnings = module
58 yield
59 finally:
60 warning_tests.warnings = original_warnings
61 if module._use_context:
62 module._set_context(saved_context)
63 else:
64 module.filters = original_filters
65
66
67class TestWarning(Warning):

Calls 1

clearMethod · 0.45

Tested by 6

test_filenameMethod · 0.68
test_stacklevelMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…