77from test import support
88from test .support .script_helper import assert_python_ok , assert_python_failure
99
10- from test import warning_tests
10+ from test . test_warnings . data import stacklevel as warning_tests
1111
1212import warnings as original_warnings
1313
@@ -44,7 +44,6 @@ class BaseTest:
4444 """Basic bookkeeping required for testing."""
4545
4646 def setUp (self ):
47- self .old_unittest_module = unittest .case .warnings
4847 # The __warningregistry__ needs to be in a pristine state for tests
4948 # to work properly.
5049 if '__warningregistry__' in globals ():
@@ -56,15 +55,10 @@ def setUp(self):
5655 # The 'warnings' module must be explicitly set so that the proper
5756 # interaction between _warnings and 'warnings' can be controlled.
5857 sys .modules ['warnings' ] = self .module
59- # Ensure that unittest.TestCase.assertWarns() uses the same warnings
60- # module than warnings.catch_warnings(). Otherwise,
61- # warnings.catch_warnings() will be unable to remove the added filter.
62- unittest .case .warnings = self .module
6358 super (BaseTest , self ).setUp ()
6459
6560 def tearDown (self ):
6661 sys .modules ['warnings' ] = original_warnings
67- unittest .case .warnings = self .old_unittest_module
6862 super (BaseTest , self ).tearDown ()
6963
7064class PublicAPITests (BaseTest ):
@@ -194,11 +188,11 @@ def test_once(self):
194188 self .module .resetwarnings ()
195189 self .module .filterwarnings ("once" , category = UserWarning )
196190 message = UserWarning ("FilterTests.test_once" )
197- self .module .warn_explicit (message , UserWarning , "test_warnings .py" ,
191+ self .module .warn_explicit (message , UserWarning , "__init__ .py" ,
198192 42 )
199193 self .assertEqual (w [- 1 ].message , message )
200194 del w [:]
201- self .module .warn_explicit (message , UserWarning , "test_warnings .py" ,
195+ self .module .warn_explicit (message , UserWarning , "__init__ .py" ,
202196 13 )
203197 self .assertEqual (len (w ), 0 )
204198 self .module .warn_explicit (message , UserWarning , "test_warnings2.py" ,
@@ -304,10 +298,10 @@ def test_filename(self):
304298 module = self .module ) as w :
305299 warning_tests .inner ("spam1" )
306300 self .assertEqual (os .path .basename (w [- 1 ].filename ),
307- "warning_tests .py" )
301+ "stacklevel .py" )
308302 warning_tests .outer ("spam2" )
309303 self .assertEqual (os .path .basename (w [- 1 ].filename ),
310- "warning_tests .py" )
304+ "stacklevel .py" )
311305
312306 def test_stacklevel (self ):
313307 # Test stacklevel argument
@@ -317,25 +311,36 @@ def test_stacklevel(self):
317311 module = self .module ) as w :
318312 warning_tests .inner ("spam3" , stacklevel = 1 )
319313 self .assertEqual (os .path .basename (w [- 1 ].filename ),
320- "warning_tests .py" )
314+ "stacklevel .py" )
321315 warning_tests .outer ("spam4" , stacklevel = 1 )
322316 self .assertEqual (os .path .basename (w [- 1 ].filename ),
323- "warning_tests .py" )
317+ "stacklevel .py" )
324318
325319 warning_tests .inner ("spam5" , stacklevel = 2 )
326320 self .assertEqual (os .path .basename (w [- 1 ].filename ),
327- "test_warnings .py" )
321+ "__init__ .py" )
328322 warning_tests .outer ("spam6" , stacklevel = 2 )
329323 self .assertEqual (os .path .basename (w [- 1 ].filename ),
330- "warning_tests .py" )
324+ "stacklevel .py" )
331325 warning_tests .outer ("spam6.5" , stacklevel = 3 )
332326 self .assertEqual (os .path .basename (w [- 1 ].filename ),
333- "test_warnings .py" )
327+ "__init__ .py" )
334328
335329 warning_tests .inner ("spam7" , stacklevel = 9999 )
336330 self .assertEqual (os .path .basename (w [- 1 ].filename ),
337331 "sys" )
338332
333+ def test_stacklevel_import (self ):
334+ # Issue #24305: With stacklevel=2, module-level warnings should work.
335+ support .unload ('test.test_warnings.data.import_warning' )
336+ with warnings_state (self .module ):
337+ with original_warnings .catch_warnings (record = True ,
338+ module = self .module ) as w :
339+ self .module .simplefilter ('always' )
340+ import test .test_warnings .data .import_warning
341+ self .assertEqual (len (w ), 1 )
342+ self .assertEqual (w [0 ].filename , __file__ )
343+
339344 def test_missing_filename_not_main (self ):
340345 # If __file__ is not specified and __main__ is not the module name,
341346 # then __file__ should be set to the module name.
0 commit comments