@@ -241,6 +241,85 @@ def test_once(self):
241241                                    42 )
242242            self .assertEqual (len (w ), 0 )
243243
244+     def  test_filter_module (self ):
245+         MS_WINDOWS  =  (sys .platform  ==  'win32' )
246+         with  self .module .catch_warnings (record = True ) as  w :
247+             self .module .simplefilter ('error' )
248+             self .module .filterwarnings ('always' , module = r'package\.module\z' )
249+             self .module .warn_explicit ('msg' , UserWarning , 'filename' , 42 ,
250+                                       module = 'package.module' )
251+             self .assertEqual (len (w ), 1 )
252+             with  self .assertRaises (UserWarning ):
253+                 self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module' , 42 )
254+             with  self .assertRaises (UserWarning ):
255+                 self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module.py' , 42 )
256+ 
257+         with  self .module .catch_warnings (record = True ) as  w :
258+             self .module .simplefilter ('error' )
259+             self .module .filterwarnings ('always' , module = 'package' )
260+             self .module .warn_explicit ('msg' , UserWarning , 'filename' , 42 ,
261+                                       module = 'package.module' )
262+             self .assertEqual (len (w ), 1 )
263+             with  self .assertRaises (UserWarning ):
264+                 self .module .warn_explicit ('msg' , UserWarning , 'filename' , 42 ,
265+                                           module = 'other.package.module' )
266+             with  self .assertRaises (UserWarning ):
267+                 self .module .warn_explicit ('msg' , UserWarning , '/path/to/otherpackage/module.py' , 42 )
268+ 
269+         with  self .module .catch_warnings (record = True ) as  w :
270+             self .module .simplefilter ('error' )
271+             self .module .filterwarnings ('always' , module = r'/path/to/package/module\z' )
272+             self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module' , 42 )
273+             self .assertEqual (len (w ), 1 )
274+             self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/module.py' , 42 )
275+             self .assertEqual (len (w ), 2 )
276+             with  self .assertRaises (UserWarning ):
277+                 self .module .warn_explicit ('msg' , UserWarning , '/PATH/TO/PACKAGE/MODULE' , 42 )
278+             if  MS_WINDOWS :
279+                 if  self .module  is  py_warnings :
280+                     self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module.PY' , 42 )
281+                     self .assertEqual (len (w ), 3 )
282+                 with  self .assertRaises (UserWarning ):
283+                     self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module/__init__.py' , 42 )
284+                 with  self .assertRaises (UserWarning ):
285+                     self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module.pyw' , 42 )
286+                 with  self .assertRaises (UserWarning ):
287+                     self .module .warn_explicit ('msg' , UserWarning , r'\path\to\package\module' , 42 )
288+ 
289+         with  self .module .catch_warnings (record = True ) as  w :
290+             self .module .simplefilter ('error' )
291+             self .module .filterwarnings ('always' , module = r'/path/to/package/__init__\z' )
292+             self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/__init__.py' , 42 )
293+             self .assertEqual (len (w ), 1 )
294+             self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/__init__' , 42 )
295+             self .assertEqual (len (w ), 2 )
296+ 
297+         if  MS_WINDOWS :
298+             with  self .module .catch_warnings (record = True ) as  w :
299+                 self .module .simplefilter ('error' )
300+                 self .module .filterwarnings ('always' , module = r'C:\\path\\to\\package\\module\z' )
301+                 self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module' , 42 )
302+                 self .assertEqual (len (w ), 1 )
303+                 self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.py' , 42 )
304+                 self .assertEqual (len (w ), 2 )
305+                 if  self .module  is  py_warnings :
306+                     self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.PY' , 42 )
307+                     self .assertEqual (len (w ), 3 )
308+                 with  self .assertRaises (UserWarning ):
309+                     self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.pyw' , 42 )
310+                 with  self .assertRaises (UserWarning ):
311+                     self .module .warn_explicit ('msg' , UserWarning , r'C:\PATH\TO\PACKAGE\MODULE' , 42 )
312+                 with  self .assertRaises (UserWarning ):
313+                     self .module .warn_explicit ('msg' , UserWarning , r'C:/path/to/package/module' , 42 )
314+                 with  self .assertRaises (UserWarning ):
315+                     self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module\__init__.py' , 42 )
316+ 
317+         with  self .module .catch_warnings (record = True ) as  w :
318+             self .module .simplefilter ('error' )
319+             self .module .filterwarnings ('always' , module = r'<unknown>\z' )
320+             self .module .warn_explicit ('msg' , UserWarning , '' , 42 )
321+             self .assertEqual (len (w ), 1 )
322+ 
244323    def  test_module_globals (self ):
245324        with  self .module .catch_warnings (record = True ) as  w :
246325            self .module .simplefilter ("always" , UserWarning )
0 commit comments