@@ -241,6 +241,77 @@ def test_once(self):
241241 42 )
242242 self .assertEqual (len (w ), 0 )
243243
244+ def test_filter_module (self ):
245+ with self .module .catch_warnings (record = True ) as w :
246+ self .module .simplefilter ('error' )
247+ self .module .filterwarnings ('always' , module = r'package\.module\z' )
248+ self .module .warn_explicit ('msg' , UserWarning , 'filename' , 42 ,
249+ module = 'package.module' )
250+ self .assertEqual (len (w ), 1 )
251+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module' , 42 )
252+ self .assertEqual (len (w ), 2 )
253+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module.py' , 42 )
254+ self .assertEqual (len (w ), 3 )
255+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module/__init__.py' , 42 )
256+ self .assertEqual (len (w ), 4 )
257+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module/__init__' , 42 )
258+ self .assertEqual (len (w ), 5 )
259+ if sys .platform == 'win32' :
260+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.PY' , 42 )
261+ self .assertEqual (len (w ), 6 )
262+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module\__init__.PY' , 42 )
263+ self .assertEqual (len (w ), 6 )
264+
265+ with self .module .catch_warnings (record = True ) as w :
266+ self .module .simplefilter ('error' )
267+ self .module .filterwarnings ('always' , module = 'package' )
268+ self .module .warn_explicit ('msg' , UserWarning , 'filename' , 42 ,
269+ module = 'package.module' )
270+ self .assertEqual (len (w ), 1 )
271+ with self .assertRaises (UserWarning ):
272+ self .module .warn_explicit ('msg' , UserWarning , 'filename' , 42 ,
273+ module = 'other.package.module' )
274+ with self .assertRaises (UserWarning ):
275+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/otherpackage/module.py' , 42 )
276+
277+ with self .module .catch_warnings (record = True ) as w :
278+ self .module .simplefilter ('error' )
279+ self .module .filterwarnings ('always' , module = r'/path/to/package/module\z' )
280+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module' , 42 )
281+ self .assertEqual (len (w ), 1 )
282+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module.py' , 42 )
283+ self .assertEqual (len (w ), 2 )
284+ with self .assertRaises (UserWarning ):
285+ self .module .warn_explicit ('msg' , UserWarning , '/PATH/TO/PACKAGE/MODULE' , 42 )
286+ if sys .platform == 'win32' :
287+ self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module.PY' , 42 )
288+ self .assertEqual (len (w ), 3 )
289+ with self .assertRaises (UserWarning ):
290+ self .module .warn_explicit ('msg' , UserWarning , r'\path\to\package\module' , 42 )
291+
292+ if sys .platform == 'win32' :
293+ with self .module .catch_warnings (record = True ) as w :
294+ self .module .simplefilter ('error' )
295+ self .module .filterwarnings ('always' , module = r'C:\\path\\to\\package\\module\z' )
296+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module' , 42 )
297+ self .assertEqual (len (w ), 1 )
298+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.py' , 42 )
299+ self .assertEqual (len (w ), 2 )
300+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.PY' , 42 )
301+ self .assertEqual (len (w ), 3 )
302+ with self .assertRaises (UserWarning ):
303+ self .module .warn_explicit ('msg' , UserWarning , r'C:\PATH\TO\PACKAGE\MODULE' , 42 )
304+ with self .assertRaises (UserWarning ):
305+ self .module .warn_explicit ('msg' , UserWarning , r'C:/path/to/package/module' , 42 )
306+ with self .assertRaises (UserWarning ):
307+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module\__init__' , 42 )
308+
309+ with self .module .catch_warnings (record = True ) as w :
310+ self .module .simplefilter ('error' )
311+ self .module .filterwarnings ('always' , module = r'<unknown>\z' )
312+ self .module .warn_explicit ('msg' , UserWarning , '' , 42 )
313+ self .assertEqual (len (w ), 1 )
314+
244315 def test_module_globals (self ):
245316 with self .module .catch_warnings (record = True ) as w :
246317 self .module .simplefilter ("always" , UserWarning )
@@ -320,7 +391,7 @@ def test_message_matching(self):
320391
321392 def test_mutate_filter_list (self ):
322393 class X :
323- def match (self , a ):
394+ def match (self , a , start = 0 ):
324395 L [:] = []
325396
326397 L = [("default" ,X (),UserWarning ,X (),0 ) for i in range (2 )]
0 commit comments