@@ -241,6 +241,83 @@ 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+ self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module.PY' , 42 )
280+ self .assertEqual (len (w ), 3 )
281+ with self .assertRaises (UserWarning ):
282+ self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module/__init__.py' , 42 )
283+ with self .assertRaises (UserWarning ):
284+ self .module .warn_explicit ('msg' , UserWarning , r'/path/to/package/module.pyw' , 42 )
285+ with self .assertRaises (UserWarning ):
286+ self .module .warn_explicit ('msg' , UserWarning , r'\path\to\package\module' , 42 )
287+
288+ with self .module .catch_warnings (record = True ) as w :
289+ self .module .simplefilter ('error' )
290+ self .module .filterwarnings ('always' , module = r'/path/to/package/__init__\z' )
291+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/__init__.py' , 42 )
292+ self .assertEqual (len (w ), 1 )
293+ self .module .warn_explicit ('msg' , UserWarning , '/path/to/package/__init__' , 42 )
294+ self .assertEqual (len (w ), 2 )
295+
296+ if MS_WINDOWS :
297+ with self .module .catch_warnings (record = True ) as w :
298+ self .module .simplefilter ('error' )
299+ self .module .filterwarnings ('always' , module = r'C:\\path\\to\\package\\module\z' )
300+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module' , 42 )
301+ self .assertEqual (len (w ), 1 )
302+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.py' , 42 )
303+ self .assertEqual (len (w ), 2 )
304+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.PY' , 42 )
305+ self .assertEqual (len (w ), 3 )
306+ with self .assertRaises (UserWarning ):
307+ self .module .warn_explicit ('msg' , UserWarning , r'C:\path\to\package\module.pyw' , 42 )
308+ with self .assertRaises (UserWarning ):
309+ self .module .warn_explicit ('msg' , UserWarning , r'C:\PATH\TO\PACKAGE\MODULE' , 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\__init__.py' , 42 )
314+
315+ with self .module .catch_warnings (record = True ) as w :
316+ self .module .simplefilter ('error' )
317+ self .module .filterwarnings ('always' , module = r'<unknown>\z' )
318+ self .module .warn_explicit ('msg' , UserWarning , '' , 42 )
319+ self .assertEqual (len (w ), 1 )
320+
244321 def test_module_globals (self ):
245322 with self .module .catch_warnings (record = True ) as w :
246323 self .module .simplefilter ("always" , UserWarning )
0 commit comments