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