@@ -312,6 +312,18 @@ def test_message_matching(self):
312312 self .module .warn ("something completely different" )
313313 self .assertEqual (w , [])
314314
315+ def test_message_matching_regex (self ):
316+ with original_warnings .catch_warnings (record = True ,
317+ module = self .module ) as w :
318+ self .module .simplefilter ("ignore" , UserWarning )
319+ self .module .filterwarnings ("error" , ".*match" , UserWarning )
320+ self .assertRaises (UserWarning , self .module .warn , "match" )
321+ self .assertRaises (UserWarning , self .module .warn , "match prefix" )
322+ self .assertRaises (UserWarning , self .module .warn , "suffix match" )
323+ self .assertEqual (w , [])
324+ self .module .warn ("not a m4tch" )
325+ self .assertEqual (w , [])
326+
315327 def test_mutate_filter_list (self ):
316328 class X :
317329 def match (self , a ):
@@ -1202,6 +1214,18 @@ def test_single_warning(self):
12021214 PYTHONDEVMODE = "" )
12031215 self .assertEqual (stdout , b"['ignore::DeprecationWarning']" )
12041216
1217+ def test_string_literals (self ):
1218+ # Ensure message/module are treated as string literals
1219+ rc , stdout , stderr = assert_python_ok ("-c" ,
1220+ "import sys, warnings; "
1221+ "sys.stdout.write(warnings.filters[0][1].pattern); "
1222+ "sys.stderr.write(warnings.filters[0][3].pattern)" ,
1223+ PYTHONWARNINGS = "ignore:.generic::yourmodule.submodule" ,
1224+ PYTHONDEVMODE = "" )
1225+ self .assertEqual (stdout , rb"\.generic" )
1226+ # '\Z' is added to the module name, so check start of pattern:
1227+ self .assertTrue (stderr .startswith (rb"yourmodule\.submodule" ))
1228+
12051229 def test_comma_separated_warnings (self ):
12061230 rc , stdout , stderr = assert_python_ok ("-c" ,
12071231 "import sys; sys.stdout.write(str(sys.warnoptions))" ,
0 commit comments