Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_warnings._WarningsTests Class Reference
Inheritance diagram for test.test_warnings._WarningsTests:
Inheritance graph
[legend]
Collaboration diagram for test.test_warnings._WarningsTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_filter
def test_onceregistry
def test_default_action
def test_showwarning_missing
def test_showwarning_not_callable
def test_show_warning_output
def test_filename_none
def setUp
def tearDown

Static Public Attributes

 module = c_warnings

Detailed Description

Tests specific to the _warnings module.

Definition at line 420 of file test_warnings.py.


Member Function Documentation

def test.test_warnings.BaseTest.setUp (   self) [inherited]

Definition at line 47 of file test_warnings.py.

00047 
00048     def setUp(self):
00049         # The __warningregistry__ needs to be in a pristine state for tests
00050         # to work properly.
00051         if '__warningregistry__' in globals():
00052             del globals()['__warningregistry__']
00053         if hasattr(warning_tests, '__warningregistry__'):
00054             del warning_tests.__warningregistry__
00055         if hasattr(sys, '__warningregistry__'):
00056             del sys.__warningregistry__
00057         # The 'warnings' module must be explicitly set so that the proper
00058         # interaction between _warnings and 'warnings' can be controlled.
00059         sys.modules['warnings'] = self.module
00060         super(BaseTest, self).setUp()

Here is the caller graph for this function:

def test.test_warnings.BaseTest.tearDown (   self) [inherited]

Definition at line 61 of file test_warnings.py.

00061 
00062     def tearDown(self):
00063         sys.modules['warnings'] = original_warnings
00064         super(BaseTest, self).tearDown()
00065 

Here is the caller graph for this function:

Definition at line 466 of file test_warnings.py.

00466 
00467     def test_default_action(self):
00468         # Replacing or removing defaultaction should be okay.
00469         message = UserWarning("defaultaction test")
00470         original = self.module.defaultaction
00471         try:
00472             with original_warnings.catch_warnings(record=True,
00473                     module=self.module) as w:
00474                 self.module.resetwarnings()
00475                 registry = {}
00476                 self.module.warn_explicit(message, UserWarning, "<test>", 42,
00477                                             registry=registry)
00478                 self.assertEqual(w[-1].message, message)
00479                 self.assertEqual(len(w), 1)
00480                 self.assertEqual(len(registry), 1)
00481                 del w[:]
00482                 # Test removal.
00483                 del self.module.defaultaction
00484                 __warningregistry__ = {}
00485                 registry = {}
00486                 self.module.warn_explicit(message, UserWarning, "<test>", 43,
00487                                             registry=registry)
00488                 self.assertEqual(w[-1].message, message)
00489                 self.assertEqual(len(w), 1)
00490                 self.assertEqual(len(registry), 1)
00491                 del w[:]
00492                 # Test setting.
00493                 self.module.defaultaction = "ignore"
00494                 __warningregistry__ = {}
00495                 registry = {}
00496                 self.module.warn_explicit(message, UserWarning, "<test>", 44,
00497                                             registry=registry)
00498                 self.assertEqual(len(w), 0)
00499         finally:
00500             self.module.defaultaction = original

Here is the call graph for this function:

Definition at line 545 of file test_warnings.py.

00545 
00546     def test_filename_none(self):
00547         # issue #12467: race condition if a warning is emitted at shutdown
00548         globals_dict = globals()
00549         oldfile = globals_dict['__file__']
00550         try:
00551             with original_warnings.catch_warnings(module=self.module) as w:
00552                 self.module.filterwarnings("always", category=UserWarning)
00553                 globals_dict['__file__'] = None
00554                 original_warnings.warn('test', UserWarning)
00555         finally:
00556             globals_dict['__file__'] = oldfile
00557 

Definition at line 426 of file test_warnings.py.

00426 
00427     def test_filter(self):
00428         # Everything should function even if 'filters' is not in warnings.
00429         with original_warnings.catch_warnings(module=self.module) as w:
00430             self.module.filterwarnings("error", "", Warning, "", 0)
00431             self.assertRaises(UserWarning, self.module.warn,
00432                                 'convert to error')
00433             del self.module.filters
00434             self.assertRaises(UserWarning, self.module.warn,
00435                                 'convert to error')

Here is the call graph for this function:

Definition at line 436 of file test_warnings.py.

00436 
00437     def test_onceregistry(self):
00438         # Replacing or removing the onceregistry should be okay.
00439         global __warningregistry__
00440         message = UserWarning('onceregistry test')
00441         try:
00442             original_registry = self.module.onceregistry
00443             __warningregistry__ = {}
00444             with original_warnings.catch_warnings(record=True,
00445                     module=self.module) as w:
00446                 self.module.resetwarnings()
00447                 self.module.filterwarnings("once", category=UserWarning)
00448                 self.module.warn_explicit(message, UserWarning, "file", 42)
00449                 self.assertEqual(w[-1].message, message)
00450                 del w[:]
00451                 self.module.warn_explicit(message, UserWarning, "file", 42)
00452                 self.assertEqual(len(w), 0)
00453                 # Test the resetting of onceregistry.
00454                 self.module.onceregistry = {}
00455                 __warningregistry__ = {}
00456                 self.module.warn('onceregistry test')
00457                 self.assertEqual(w[-1].message.args, message.args)
00458                 # Removal of onceregistry is okay.
00459                 del w[:]
00460                 del self.module.onceregistry
00461                 __warningregistry__ = {}
00462                 self.module.warn_explicit(message, UserWarning, "file", 42)
00463                 self.assertEqual(len(w), 0)
00464         finally:
00465             self.module.onceregistry = original_registry

Here is the call graph for this function:

Definition at line 522 of file test_warnings.py.

00522 
00523     def test_show_warning_output(self):
00524         # With showarning() missing, make sure that output is okay.
00525         text = 'test show_warning'
00526         with original_warnings.catch_warnings(module=self.module):
00527             self.module.filterwarnings("always", category=UserWarning)
00528             del self.module.showwarning
00529             with support.captured_output('stderr') as stream:
00530                 warning_tests.inner(text)
00531                 result = stream.getvalue()
00532         self.assertEqual(result.count('\n'), 2,
00533                              "Too many newlines in %r" % result)
00534         first_line, second_line = result.split('\n', 1)
00535         expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
00536         first_line_parts = first_line.rsplit(':', 3)
00537         path, line, warning_class, message = first_line_parts
00538         line = int(line)
00539         self.assertEqual(expected_file, path)
00540         self.assertEqual(warning_class, ' ' + UserWarning.__name__)
00541         self.assertEqual(message, ' ' + text)
00542         expected_line = '  ' + linecache.getline(path, line).strip() + '\n'
00543         assert expected_line
00544         self.assertEqual(second_line, expected_line)

Here is the call graph for this function:

Definition at line 501 of file test_warnings.py.

00501 
00502     def test_showwarning_missing(self):
00503         # Test that showwarning() missing is okay.
00504         text = 'del showwarning test'
00505         with original_warnings.catch_warnings(module=self.module):
00506             self.module.filterwarnings("always", category=UserWarning)
00507             del self.module.showwarning
00508             with support.captured_output('stderr') as stream:
00509                 self.module.warn(text)
00510                 result = stream.getvalue()
00511         self.assertIn(text, result)

Here is the call graph for this function:

Definition at line 512 of file test_warnings.py.

00512 
00513     def test_showwarning_not_callable(self):
00514         with original_warnings.catch_warnings(module=self.module):
00515             self.module.filterwarnings("always", category=UserWarning)
00516             old_showwarning = self.module.showwarning
00517             self.module.showwarning = 23
00518             try:
00519                 self.assertRaises(TypeError, self.module.warn, "Warning!")
00520             finally:
00521                 self.module.showwarning = old_showwarning

Here is the call graph for this function:


Member Data Documentation

Definition at line 424 of file test_warnings.py.


The documentation for this class was generated from the following file: