@@ -441,78 +441,14 @@ def test_stacklevel_import(self):
441441 self .assertEqual (len (w ), 1 )
442442 self .assertEqual (w [0 ].filename , __file__ )
443443
444- def test_missing_filename_not_main (self ):
445- # If __file__ is not specified and __main__ is not the module name,
446- # then __file__ should be set to the module name.
447- filename = warning_tests .__file__
448- try :
449- del warning_tests .__file__
450- with warnings_state (self .module ):
451- with original_warnings .catch_warnings (record = True ,
452- module = self .module ) as w :
453- warning_tests .inner ("spam8" , stacklevel = 1 )
454- self .assertEqual (w [- 1 ].filename , warning_tests .__name__ )
455- finally :
456- warning_tests .__file__ = filename
457-
458- @unittest .skipUnless (hasattr (sys , 'argv' ), 'test needs sys.argv' )
459- def test_missing_filename_main_with_argv (self ):
460- # If __file__ is not specified and the caller is __main__ and sys.argv
461- # exists, then use sys.argv[0] as the file.
462- filename = warning_tests .__file__
463- module_name = warning_tests .__name__
464- try :
465- del warning_tests .__file__
466- warning_tests .__name__ = '__main__'
467- with warnings_state (self .module ):
468- with original_warnings .catch_warnings (record = True ,
469- module = self .module ) as w :
470- warning_tests .inner ('spam9' , stacklevel = 1 )
471- self .assertEqual (w [- 1 ].filename , sys .argv [0 ])
472- finally :
473- warning_tests .__file__ = filename
474- warning_tests .__name__ = module_name
475-
476- def test_missing_filename_main_without_argv (self ):
477- # If __file__ is not specified, the caller is __main__, and sys.argv
478- # is not set, then '__main__' is the file name.
479- filename = warning_tests .__file__
480- module_name = warning_tests .__name__
481- argv = sys .argv
482- try :
483- del warning_tests .__file__
484- warning_tests .__name__ = '__main__'
485- del sys .argv
486- with warnings_state (self .module ):
487- with original_warnings .catch_warnings (record = True ,
488- module = self .module ) as w :
489- warning_tests .inner ('spam10' , stacklevel = 1 )
490- self .assertEqual (w [- 1 ].filename , '__main__' )
491- finally :
492- warning_tests .__file__ = filename
493- warning_tests .__name__ = module_name
494- sys .argv = argv
495-
496- def test_missing_filename_main_with_argv_empty_string (self ):
497- # If __file__ is not specified, the caller is __main__, and sys.argv[0]
498- # is the empty string, then '__main__ is the file name.
499- # Tests issue 2743.
500- file_name = warning_tests .__file__
501- module_name = warning_tests .__name__
502- argv = sys .argv
503- try :
504- del warning_tests .__file__
505- warning_tests .__name__ = '__main__'
506- sys .argv = ['' ]
507- with warnings_state (self .module ):
508- with original_warnings .catch_warnings (record = True ,
509- module = self .module ) as w :
510- warning_tests .inner ('spam11' , stacklevel = 1 )
511- self .assertEqual (w [- 1 ].filename , '__main__' )
512- finally :
513- warning_tests .__file__ = file_name
514- warning_tests .__name__ = module_name
515- sys .argv = argv
444+ def test_exec_filename (self ):
445+ filename = "<warnings-test>"
446+ codeobj = compile (("import warnings\n "
447+ "warnings.warn('hello', UserWarning)" ),
448+ filename , "exec" )
449+ with original_warnings .catch_warnings (record = True ) as w :
450+ exec (codeobj )
451+ self .assertEqual (w [0 ].filename , filename )
516452
517453 def test_warn_explicit_non_ascii_filename (self ):
518454 with original_warnings .catch_warnings (record = True ,
@@ -1245,9 +1181,7 @@ def __del__(self):
12451181a=A()
12461182 """
12471183 rc , out , err = assert_python_ok ("-c" , code )
1248- # note: "__main__" filename is not correct, it should be the name
1249- # of the script
1250- self .assertEqual (err .decode (), '__main__:7: UserWarning: test' )
1184+ self .assertEqual (err .decode (), '<string>:7: UserWarning: test' )
12511185
12521186 def test_late_resource_warning (self ):
12531187 # Issue #21925: Emitting a ResourceWarning late during the Python
0 commit comments