@@ -229,6 +229,77 @@ def test_stacklevel(self):
229229 warning_tests .inner ("spam7" , stacklevel = 9999 )
230230 self .assertEqual (os .path .basename (w .filename ), "sys" )
231231
232+ def test_missing_filename_not_main (self ):
233+ # If __file__ is not specified and __main__ is not the module name,
234+ # then __file__ should be set to the module name.
235+ filename = warning_tests .__file__
236+ try :
237+ del warning_tests .__file__
238+ with warnings_state (self .module ):
239+ with test_support .catch_warning (self .module ) as w :
240+ warning_tests .inner ("spam8" , stacklevel = 1 )
241+ self .assertEqual (w .filename , warning_tests .__name__ )
242+ finally :
243+ warning_tests .__file__ = filename
244+
245+ def test_missing_filename_main_with_argv (self ):
246+ # If __file__ is not specified and the caller is __main__ and sys.argv
247+ # exists, then use sys.argv[0] as the file.
248+ if not hasattr (sys , 'argv' ):
249+ return
250+ filename = warning_tests .__file__
251+ module_name = warning_tests .__name__
252+ try :
253+ del warning_tests .__file__
254+ warning_tests .__name__ = '__main__'
255+ with warnings_state (self .module ):
256+ with test_support .catch_warning (self .module ) as w :
257+ warning_tests .inner ('spam9' , stacklevel = 1 )
258+ self .assertEqual (w .filename , sys .argv [0 ])
259+ finally :
260+ warning_tests .__file__ = filename
261+ warning_tests .__name__ = module_name
262+
263+ def test_missing_filename_main_without_argv (self ):
264+ # If __file__ is not specified, the caller is __main__, and sys.argv
265+ # is not set, then '__main__' is the file name.
266+ filename = warning_tests .__file__
267+ module_name = warning_tests .__name__
268+ argv = sys .argv
269+ try :
270+ del warning_tests .__file__
271+ warning_tests .__name__ = '__main__'
272+ del sys .argv
273+ with warnings_state (self .module ):
274+ with test_support .catch_warning (self .module ) as w :
275+ warning_tests .inner ('spam10' , stacklevel = 1 )
276+ self .assertEqual (w .filename , '__main__' )
277+ finally :
278+ warning_tests .__file__ = filename
279+ warning_tests .__name__ = module_name
280+ sys .argv = argv
281+
282+ def test_missing_filename_main_with_argv_empty_string (self ):
283+ # If __file__ is not specified, the caller is __main__, and sys.argv[0]
284+ # is the empty string, then '__main__ is the file name.
285+ # Tests issue 2743.
286+ file_name = warning_tests .__file__
287+ module_name = warning_tests .__name__
288+ argv = sys .argv
289+ try :
290+ del warning_tests .__file__
291+ warning_tests .__name__ = '__main__'
292+ sys .argv = ['' ]
293+ with warnings_state (self .module ):
294+ with test_support .catch_warning (self .module ) as w :
295+ warning_tests .inner ('spam11' , stacklevel = 1 )
296+ self .assertEqual (w .filename , '__main__' )
297+ finally :
298+ warning_tests .__file__ = file_name
299+ warning_tests .__name__ = module_name
300+ sys .argv = argv
301+
302+
232303
233304class CWarnTests (BaseTest , WarnTests ):
234305 module = c_warnings
0 commit comments