Beispiel #1
0
 def test_filename(self):
     with warnings_state(self.module):
         with original_warnings.catch_warnings(record=True, module=self.module) as w:
             warning_tests.inner("spam1")
             self.assertEqual(os.path.basename(w[-1].filename), "stacklevel.py")
             warning_tests.outer("spam2")
             self.assertEqual(os.path.basename(w[-1].filename), "stacklevel.py")
Beispiel #2
0
 def test_filename(self):
     with warnings_state(self.module):
         with original_warnings.catch_warnings(record=True,
                 module=self.module) as w:
             warning_tests.inner("spam1")
             self.assertEqual(os.path.basename(w[-1].filename),
                                 "stacklevel.py")
             warning_tests.outer("spam2")
             self.assertEqual(os.path.basename(w[-1].filename),
                                 "stacklevel.py")
Beispiel #3
0
 def test_missing_filename_not_main(self):
     filename = warning_tests.__file__
     try:
         del warning_tests.__file__
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                                                   module=self.module) as w:
                 warning_tests.inner('spam8', stacklevel=1)
                 self.assertEqual(w[-1].filename, warning_tests.__name__)
     finally:
         warning_tests.__file__ = filename
Beispiel #4
0
 def test_missing_filename_not_main(self):
     # If __file__ is not specified and __main__ is not the module name,
     # then __file__ should be set to the module name.
     filename = warning_tests.__file__
     try:
         del warning_tests.__file__
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True, module=self.module) as w:
                 warning_tests.inner("spam8", stacklevel=1)
                 self.assertEqual(w[-1].filename, warning_tests.__name__)
     finally:
         warning_tests.__file__ = filename
Beispiel #5
0
 def test_missing_filename_not_main(self):
     # If __file__ is not specified and __main__ is not the module name,
     # then __file__ should be set to the module name.
     filename = warning_tests.__file__
     try:
         del warning_tests.__file__
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                     module=self.module) as w:
                 warning_tests.inner("spam8", stacklevel=1)
                 self.assertEqual(w[-1].filename, warning_tests.__name__)
     finally:
         warning_tests.__file__ = filename
Beispiel #6
0
 def test_missing_filename_main_with_argv(self):
     filename = warning_tests.__file__
     module_name = warning_tests.__name__
     try:
         del warning_tests.__file__
         warning_tests.__name__ = '__main__'
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                                                   module=self.module) as w:
                 warning_tests.inner('spam9', stacklevel=1)
                 self.assertEqual(w[-1].filename, sys.argv[0])
     finally:
         warning_tests.__file__ = filename
         warning_tests.__name__ = module_name
Beispiel #7
0
 def test_missing_filename_main_with_argv(self):
     # If __file__ is not specified and the caller is __main__ and sys.argv
     # exists, then use sys.argv[0] as the file.
     filename = warning_tests.__file__
     module_name = warning_tests.__name__
     try:
         del warning_tests.__file__
         warning_tests.__name__ = "__main__"
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True, module=self.module) as w:
                 warning_tests.inner("spam9", stacklevel=1)
                 self.assertEqual(w[-1].filename, sys.argv[0])
     finally:
         warning_tests.__file__ = filename
         warning_tests.__name__ = module_name
Beispiel #8
0
 def test_missing_filename_main_with_argv(self):
     # If __file__ is not specified and the caller is __main__ and sys.argv
     # exists, then use sys.argv[0] as the file.
     filename = warning_tests.__file__
     module_name = warning_tests.__name__
     try:
         del warning_tests.__file__
         warning_tests.__name__ = '__main__'
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                     module=self.module) as w:
                 warning_tests.inner('spam9', stacklevel=1)
                 self.assertEqual(w[-1].filename, sys.argv[0])
     finally:
         warning_tests.__file__ = filename
         warning_tests.__name__ = module_name
Beispiel #9
0
 def test_missing_filename_main_with_argv_empty_string(self):
     file_name = warning_tests.__file__
     module_name = warning_tests.__name__
     argv = sys.argv
     try:
         del warning_tests.__file__
         warning_tests.__name__ = '__main__'
         sys.argv = ['']
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                                                   module=self.module) as w:
                 warning_tests.inner('spam11', stacklevel=1)
                 self.assertEqual(w[-1].filename, '__main__')
     finally:
         warning_tests.__file__ = file_name
         warning_tests.__name__ = module_name
         sys.argv = argv
Beispiel #10
0
 def test_missing_filename_main_without_argv(self):
     # If __file__ is not specified, the caller is __main__, and sys.argv
     # is not set, then '__main__' is the file name.
     filename = warning_tests.__file__
     module_name = warning_tests.__name__
     argv = sys.argv
     try:
         del warning_tests.__file__
         warning_tests.__name__ = "__main__"
         del sys.argv
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True, module=self.module) as w:
                 warning_tests.inner("spam10", stacklevel=1)
                 self.assertEqual(w[-1].filename, "__main__")
     finally:
         warning_tests.__file__ = filename
         warning_tests.__name__ = module_name
         sys.argv = argv
Beispiel #11
0
 def test_missing_filename_main_without_argv(self):
     # If __file__ is not specified, the caller is __main__, and sys.argv
     # is not set, then '__main__' is the file name.
     filename = warning_tests.__file__
     module_name = warning_tests.__name__
     argv = sys.argv
     try:
         del warning_tests.__file__
         warning_tests.__name__ = '__main__'
         del sys.argv
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                     module=self.module) as w:
                 warning_tests.inner('spam10', stacklevel=1)
                 self.assertEqual(w[-1].filename, '__main__')
     finally:
         warning_tests.__file__ = filename
         warning_tests.__name__ = module_name
         sys.argv = argv
Beispiel #12
0
 def test_missing_filename_main_with_argv_empty_string(self):
     # If __file__ is not specified, the caller is __main__, and sys.argv[0]
     # is the empty string, then '__main__ is the file name.
     # Tests issue 2743.
     file_name = warning_tests.__file__
     module_name = warning_tests.__name__
     argv = sys.argv
     try:
         del warning_tests.__file__
         warning_tests.__name__ = "__main__"
         sys.argv = [""]
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True, module=self.module) as w:
                 warning_tests.inner("spam11", stacklevel=1)
                 self.assertEqual(w[-1].filename, "__main__")
     finally:
         warning_tests.__file__ = file_name
         warning_tests.__name__ = module_name
         sys.argv = argv
Beispiel #13
0
 def test_missing_filename_main_with_argv_empty_string(self):
     # If __file__ is not specified, the caller is __main__, and sys.argv[0]
     # is the empty string, then '__main__ is the file name.
     # Tests issue 2743.
     file_name = warning_tests.__file__
     module_name = warning_tests.__name__
     argv = sys.argv
     try:
         del warning_tests.__file__
         warning_tests.__name__ = '__main__'
         sys.argv = ['']
         with warnings_state(self.module):
             with original_warnings.catch_warnings(record=True,
                     module=self.module) as w:
                 warning_tests.inner('spam11', stacklevel=1)
                 self.assertEqual(w[-1].filename, '__main__')
     finally:
         warning_tests.__file__ = file_name
         warning_tests.__name__ = module_name
         sys.argv = argv
Beispiel #14
0
 def test_show_warning_output(self):
     text = 'test show_warning'
     with original_warnings.catch_warnings(module=self.module):
         self.module.filterwarnings('always', category=UserWarning)
         del self.module.showwarning
         with support.captured_output('stderr') as stream:
             warning_tests.inner(text)
             result = stream.getvalue()
     self.assertEqual(result.count('\n'), 2,
                      'Too many newlines in %r' % result)
     first_line, second_line = result.split('\n', 1)
     expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
     first_line_parts = first_line.rsplit(':', 3)
     path, line, warning_class, message = first_line_parts
     line = int(line)
     self.assertEqual(expected_file, path)
     self.assertEqual(warning_class, ' ' + UserWarning.__name__)
     self.assertEqual(message, ' ' + text)
     expected_line = '  ' + linecache.getline(path, line).strip() + '\n'
     assert expected_line
     self.assertEqual(second_line, expected_line)
Beispiel #15
0
 def test_show_warning_output(self):
     # With showarning() missing, make sure that output is okay.
     text = "test show_warning"
     with original_warnings.catch_warnings(module=self.module):
         self.module.filterwarnings("always", category=UserWarning)
         del self.module.showwarning
         with support.captured_output("stderr") as stream:
             warning_tests.inner(text)
             result = stream.getvalue()
     self.assertEqual(result.count("\n"), 2, "Too many newlines in %r" % result)
     first_line, second_line = result.split("\n", 1)
     expected_file = os.path.splitext(warning_tests.__file__)[0] + ".py"
     first_line_parts = first_line.rsplit(":", 3)
     path, line, warning_class, message = first_line_parts
     line = int(line)
     self.assertEqual(expected_file, path)
     self.assertEqual(warning_class, " " + UserWarning.__name__)
     self.assertEqual(message, " " + text)
     expected_line = "  " + linecache.getline(path, line).strip() + "\n"
     assert expected_line
     self.assertEqual(second_line, expected_line)
Beispiel #16
0
    def test_stacklevel(self):
        # Test stacklevel argument
        # make sure all messages are different, so the warning won't be skipped
        with warnings_state(self.module):
            with original_warnings.catch_warnings(record=True,
                    module=self.module) as w:
                warning_tests.inner("spam3", stacklevel=1)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")
                warning_tests.outer("spam4", stacklevel=1)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")

                warning_tests.inner("spam5", stacklevel=2)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "__init__.py")
                warning_tests.outer("spam6", stacklevel=2)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")
                warning_tests.outer("spam6.5", stacklevel=3)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "__init__.py")

                warning_tests.inner("spam7", stacklevel=9999)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "sys")
Beispiel #17
0
 def test_stacklevel(self):
     with warnings_state(self.module):
         with original_warnings.catch_warnings(record=True,
                                               module=self.module) as w:
             warning_tests.inner('spam3', stacklevel=1)
             self.assertEqual(os.path.basename(w[-1].filename),
                              'stacklevel.py')
             warning_tests.outer('spam4', stacklevel=1)
             self.assertEqual(os.path.basename(w[-1].filename),
                              'stacklevel.py')
             warning_tests.inner('spam5', stacklevel=2)
             self.assertEqual(os.path.basename(w[-1].filename),
                              '__init__.py')
             warning_tests.outer('spam6', stacklevel=2)
             self.assertEqual(os.path.basename(w[-1].filename),
                              'stacklevel.py')
             warning_tests.outer('spam6.5', stacklevel=3)
             self.assertEqual(os.path.basename(w[-1].filename),
                              '__init__.py')
             warning_tests.inner('spam7', stacklevel=9999)
             self.assertEqual(os.path.basename(w[-1].filename), 'sys')