@@ -39,13 +39,13 @@ def warnings_state(module):
39
39
original_warnings = warning_tests .warnings
40
40
original_filters = module .filters
41
41
try :
42
- module .filters = original_filters [:]
43
42
module .simplefilter ("once" )
44
43
warning_tests .warnings = module
45
44
yield
46
45
finally :
47
46
warning_tests .warnings = original_warnings
48
- module .filters = original_filters
47
+ assert module .filters is original_filters
48
+
49
49
50
50
51
51
class TestWarning (Warning ):
@@ -315,15 +315,18 @@ def test_message_matching(self):
315
315
def test_mutate_filter_list (self ):
316
316
class X :
317
317
def match (self , a ):
318
- L [:] = []
318
+ self . module . get_filters () [:] = []
319
319
320
+ filter_copy = list (self .module .filters )
320
321
L = [("default" ,X (),UserWarning ,X (),0 ) for i in range (2 )]
321
322
with original_warnings .catch_warnings (record = True ,
322
323
module = self .module ) as w :
323
- self .module .filters = L
324
+ self .module .get_filters ()[:] = L
324
325
self .module .warn_explicit (UserWarning ("b" ), None , "f.py" , 42 )
325
326
self .assertEqual (str (w [- 1 ].message ), "b" )
326
327
328
+ self .assertEqual (self .module .filters , filter_copy )
329
+
327
330
def test_filterwarnings_duplicate_filters (self ):
328
331
with original_warnings .catch_warnings (module = self .module ):
329
332
self .module .resetwarnings ()
@@ -700,7 +703,7 @@ def test_filter(self):
700
703
self .module .filterwarnings ("error" , "" , Warning , "" , 0 )
701
704
self .assertRaises (UserWarning , self .module .warn ,
702
705
'convert to error' )
703
- del self .module .filters
706
+ self .module .get_filters ()[:] = []
704
707
self .assertRaises (UserWarning , self .module .warn ,
705
708
'convert to error' )
706
709
@@ -776,7 +779,9 @@ def test_showwarning_missing(self):
776
779
text = 'del showwarning test'
777
780
with original_warnings .catch_warnings (module = self .module ):
778
781
self .module .filterwarnings ("always" , category = UserWarning )
779
- del self .module .showwarning
782
+ with self .assertRaises (AttributeError ):
783
+ del self .module .showwarning
784
+ self .assertTrue (self .module .showwarning )
780
785
with support .captured_output ('stderr' ) as stream :
781
786
self .module .warn (text )
782
787
result = stream .getvalue ()
@@ -788,31 +793,24 @@ def test_showwarnmsg_missing(self):
788
793
with original_warnings .catch_warnings (module = self .module ):
789
794
self .module .filterwarnings ("always" , category = UserWarning )
790
795
791
- show = self .module ._showwarnmsg
792
- try :
796
+ with self .assertRaises (AttributeError ):
793
797
del self .module ._showwarnmsg
794
- with support .captured_output ('stderr' ) as stream :
795
- self .module .warn (text )
796
- result = stream .getvalue ()
797
- finally :
798
- self .module ._showwarnmsg = show
799
- self .assertIn (text , result )
798
+ self .assertTrue (self .module ._showwarnmsg )
800
799
801
- def test_showwarning_not_callable (self ):
802
- with original_warnings .catch_warnings (module = self .module ):
803
- self .module .filterwarnings ("always" , category = UserWarning )
804
- self .module .showwarning = print
805
- with support .captured_output ('stdout' ):
806
- self .module .warn ('Warning!' )
807
- self .module .showwarning = 23
808
- self .assertRaises (TypeError , self .module .warn , "Warning!" )
800
+ with support .captured_output ('stderr' ) as stream :
801
+ self .module .warn (text )
802
+ result = stream .getvalue ()
803
+
804
+ self .assertIn (text , result )
809
805
810
806
def test_show_warning_output (self ):
811
807
# With showwarning() missing, make sure that output is okay.
812
808
text = 'test show_warning'
813
809
with original_warnings .catch_warnings (module = self .module ):
814
810
self .module .filterwarnings ("always" , category = UserWarning )
815
- del self .module .showwarning
811
+ with self .assertRaises (AttributeError ):
812
+ del self .module .showwarning
813
+ self .assertTrue (self .module .showwarning )
816
814
with support .captured_output ('stderr' ) as stream :
817
815
warning_tests .inner (text )
818
816
result = stream .getvalue ()
@@ -907,11 +905,11 @@ def test_issue31416(self):
907
905
# bad warnings.filters or warnings.defaultaction.
908
906
wmod = self .module
909
907
with original_warnings .catch_warnings (module = wmod ):
910
- wmod .filters = [(None , None , Warning , None , 0 )]
908
+ wmod .get_filters ()[:] = [(None , None , Warning , None , 0 )]
911
909
with self .assertRaises (TypeError ):
912
910
wmod .warn_explicit ('foo' , Warning , 'bar' , 1 )
913
911
914
- wmod .filters = []
912
+ wmod .get_filters ()[:] = []
915
913
with support .swap_attr (wmod , 'defaultaction' , None ), \
916
914
self .assertRaises (TypeError ):
917
915
wmod .warn_explicit ('foo' , Warning , 'bar' , 1 )
@@ -1051,14 +1049,20 @@ def test_catch_warnings_restore(self):
1051
1049
wmod = self .module
1052
1050
orig_filters = wmod .filters
1053
1051
orig_showwarning = wmod .showwarning
1054
- # Ensure both showwarning and filters are restored when recording
1052
+ # Ensure both showwarning and filters are not modified
1055
1053
with wmod .catch_warnings (module = wmod , record = True ):
1056
- wmod .filters = wmod .showwarning = object ()
1054
+ with self .assertRaises (AttributeError ):
1055
+ wmod .filters = object ()
1056
+ with self .assertRaises (AttributeError ):
1057
+ wmod .showwarning = object ()
1057
1058
self .assertIs (wmod .filters , orig_filters )
1058
1059
self .assertIs (wmod .showwarning , orig_showwarning )
1059
1060
# Same test, but with recording disabled
1060
1061
with wmod .catch_warnings (module = wmod , record = False ):
1061
- wmod .filters = wmod .showwarning = object ()
1062
+ with self .assertRaises (AttributeError ):
1063
+ wmod .filters = object ()
1064
+ with self .assertRaises (AttributeError ):
1065
+ wmod .showwarning = object ()
1062
1066
self .assertIs (wmod .filters , orig_filters )
1063
1067
self .assertIs (wmod .showwarning , orig_showwarning )
1064
1068
@@ -1104,14 +1108,14 @@ def test_catch_warnings_defaults(self):
1104
1108
with wmod .catch_warnings (module = wmod ) as w :
1105
1109
self .assertIsNone (w )
1106
1110
self .assertIs (wmod .showwarning , orig_showwarning )
1107
- self .assertIsNot (wmod .filters , orig_filters )
1111
+ self .assertIs (wmod .filters , orig_filters )
1108
1112
self .assertIs (wmod .filters , orig_filters )
1109
1113
if wmod is sys .modules ['warnings' ]:
1110
1114
# Ensure the default module is this one
1111
1115
with wmod .catch_warnings () as w :
1112
1116
self .assertIsNone (w )
1113
1117
self .assertIs (wmod .showwarning , orig_showwarning )
1114
- self .assertIsNot (wmod .filters , orig_filters )
1118
+ self .assertIs (wmod .filters , orig_filters )
1115
1119
self .assertIs (wmod .filters , orig_filters )
1116
1120
1117
1121
def test_record_override_showwarning_before (self ):
@@ -1125,15 +1129,11 @@ def my_logger(message, category, filename, lineno, file=None, line=None):
1125
1129
nonlocal my_log
1126
1130
my_log .append (message )
1127
1131
1128
- # Override warnings.showwarning() before calling catch_warnings()
1129
- with support .swap_attr (wmod , 'showwarning' , my_logger ):
1130
- with wmod .catch_warnings (module = wmod , record = True ) as log :
1131
- self .assertIsNot (wmod .showwarning , my_logger )
1132
-
1133
- wmod .simplefilter ("always" )
1134
- wmod .warn (text )
1132
+ with wmod .catch_warnings (module = wmod , record = True ) as log :
1133
+ self .assertIsNot (wmod .showwarning , my_logger )
1135
1134
1136
- self .assertIs (wmod .showwarning , my_logger )
1135
+ wmod .simplefilter ("always" )
1136
+ wmod .warn (text )
1137
1137
1138
1138
self .assertEqual (len (log ), 1 , log )
1139
1139
self .assertEqual (log [0 ].message .args [0 ], text )
@@ -1147,17 +1147,17 @@ def test_record_override_showwarning_inside(self):
1147
1147
my_log = []
1148
1148
1149
1149
def my_logger (message , category , filename , lineno , file = None , line = None ):
1150
- nonlocal my_log
1151
1150
my_log .append (message )
1152
1151
1153
1152
with wmod .catch_warnings (module = wmod , record = True ) as log :
1154
1153
wmod .simplefilter ("always" )
1155
- wmod .showwarning = my_logger
1154
+ with self .assertRaises (AttributeError ):
1155
+ wmod .showwarning = my_logger
1156
1156
wmod .warn (text )
1157
1157
1158
- self .assertEqual (len (my_log ), 1 , my_log )
1159
- self .assertEqual (my_log [0 ].args [0 ], text )
1160
- self .assertEqual (log , [])
1158
+ self .assertEqual (len (log ), 1 , log )
1159
+ self .assertEqual (log [0 ]. message .args [0 ], text )
1160
+ self .assertEqual (my_log , [])
1161
1161
1162
1162
def test_check_warnings (self ):
1163
1163
# Explicit tests for the test.support convenience wrapper
@@ -1350,6 +1350,7 @@ def __del__(self):
1350
1350
warn("test")
1351
1351
1352
1352
a=A()
1353
+ a = None
1353
1354
"""
1354
1355
rc , out , err = assert_python_ok ("-c" , code )
1355
1356
self .assertEqual (err .decode ().rstrip (),
0 commit comments