(root)/
Python-3.12.0/
Lib/
test/
test_baseexception.py
       1  import unittest
       2  import builtins
       3  import os
       4  from platform import system as platform_system
       5  
       6  
       7  class ESC[4;38;5;81mExceptionClassTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
       8  
       9      """Tests for anything relating to exception objects themselves (e.g.,
      10      inheritance hierarchy)"""
      11  
      12      def test_builtins_new_style(self):
      13          self.assertTrue(issubclass(Exception, object))
      14  
      15      def verify_instance_interface(self, ins):
      16          for attr in ("args", "__str__", "__repr__"):
      17              self.assertTrue(hasattr(ins, attr),
      18                      "%s missing %s attribute" %
      19                          (ins.__class__.__name__, attr))
      20  
      21      def test_inheritance(self):
      22          # Make sure the inheritance hierarchy matches the documentation
      23          exc_set = set()
      24          for object_ in builtins.__dict__.values():
      25              try:
      26                  if issubclass(object_, BaseException):
      27                      exc_set.add(object_.__name__)
      28              except TypeError:
      29                  pass
      30  
      31          inheritance_tree = open(
      32                  os.path.join(os.path.split(__file__)[0], 'exception_hierarchy.txt'),
      33                  encoding="utf-8")
      34          try:
      35              superclass_name = inheritance_tree.readline().rstrip()
      36              try:
      37                  last_exc = getattr(builtins, superclass_name)
      38              except AttributeError:
      39                  self.fail("base class %s not a built-in" % superclass_name)
      40              self.assertIn(superclass_name, exc_set,
      41                            '%s not found' % superclass_name)
      42              exc_set.discard(superclass_name)
      43              superclasses = []  # Loop will insert base exception
      44              last_depth = 0
      45              for exc_line in inheritance_tree:
      46                  exc_line = exc_line.rstrip()
      47                  depth = exc_line.rindex('')
      48                  exc_name = exc_line[depth+2:]  # Slice past space
      49                  if '(' in exc_name:
      50                      paren_index = exc_name.index('(')
      51                      platform_name = exc_name[paren_index+1:-1]
      52                      exc_name = exc_name[:paren_index-1]  # Slice off space
      53                      if platform_system() != platform_name:
      54                          exc_set.discard(exc_name)
      55                          continue
      56                  if '[' in exc_name:
      57                      left_bracket = exc_name.index('[')
      58                      exc_name = exc_name[:left_bracket-1]  # cover space
      59                  try:
      60                      exc = getattr(builtins, exc_name)
      61                  except AttributeError:
      62                      self.fail("%s not a built-in exception" % exc_name)
      63                  if last_depth < depth:
      64                      superclasses.append((last_depth, last_exc))
      65                  elif last_depth > depth:
      66                      while superclasses[-1][0] >= depth:
      67                          superclasses.pop()
      68                  self.assertTrue(issubclass(exc, superclasses[-1][1]),
      69                  "%s is not a subclass of %s" % (exc.__name__,
      70                      superclasses[-1][1].__name__))
      71                  try:  # Some exceptions require arguments; just skip them
      72                      self.verify_instance_interface(exc())
      73                  except TypeError:
      74                      pass
      75                  self.assertIn(exc_name, exc_set)
      76                  exc_set.discard(exc_name)
      77                  last_exc = exc
      78                  last_depth = depth
      79          finally:
      80              inheritance_tree.close()
      81          self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set)
      82  
      83      interface_tests = ("length", "args", "str", "repr")
      84  
      85      def interface_test_driver(self, results):
      86          for test_name, (given, expected) in zip(self.interface_tests, results):
      87              self.assertEqual(given, expected, "%s: %s != %s" % (test_name,
      88                  given, expected))
      89  
      90      def test_interface_single_arg(self):
      91          # Make sure interface works properly when given a single argument
      92          arg = "spam"
      93          exc = Exception(arg)
      94          results = ([len(exc.args), 1], [exc.args[0], arg],
      95                     [str(exc), str(arg)],
      96              [repr(exc), '%s(%r)' % (exc.__class__.__name__, arg)])
      97          self.interface_test_driver(results)
      98  
      99      def test_interface_multi_arg(self):
     100          # Make sure interface correct when multiple arguments given
     101          arg_count = 3
     102          args = tuple(range(arg_count))
     103          exc = Exception(*args)
     104          results = ([len(exc.args), arg_count], [exc.args, args],
     105                  [str(exc), str(args)],
     106                  [repr(exc), exc.__class__.__name__ + repr(exc.args)])
     107          self.interface_test_driver(results)
     108  
     109      def test_interface_no_arg(self):
     110          # Make sure that with no args that interface is correct
     111          exc = Exception()
     112          results = ([len(exc.args), 0], [exc.args, tuple()],
     113                  [str(exc), ''],
     114                  [repr(exc), exc.__class__.__name__ + '()'])
     115          self.interface_test_driver(results)
     116  
     117      def test_setstate_refcount_no_crash(self):
     118          # gh-97591: Acquire strong reference before calling tp_hash slot
     119          # in PyObject_SetAttr.
     120          import gc
     121          d = {}
     122          class ESC[4;38;5;81mHashThisKeyWillClearTheDict(ESC[4;38;5;149mstr):
     123              def __hash__(self) -> int:
     124                  d.clear()
     125                  return super().__hash__()
     126          class ESC[4;38;5;81mValue(ESC[4;38;5;149mstr):
     127              pass
     128          exc = Exception()
     129  
     130          d[HashThisKeyWillClearTheDict()] = Value()  # refcount of Value() is 1 now
     131  
     132          # Exception.__setstate__ should aquire a strong reference of key and
     133          # value in the dict. Otherwise, Value()'s refcount would go below
     134          # zero in the tp_hash call in PyObject_SetAttr(), and it would cause
     135          # crash in GC.
     136          exc.__setstate__(d)  # __hash__() is called again here, clearing the dict.
     137  
     138          # This GC would crash if the refcount of Value() goes below zero.
     139          gc.collect()
     140  
     141  
     142  class ESC[4;38;5;81mUsageTests(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     143  
     144      """Test usage of exceptions"""
     145  
     146      def raise_fails(self, object_):
     147          """Make sure that raising 'object_' triggers a TypeError."""
     148          try:
     149              raise object_
     150          except TypeError:
     151              return  # What is expected.
     152          self.fail("TypeError expected for raising %s" % type(object_))
     153  
     154      def catch_fails(self, object_):
     155          """Catching 'object_' should raise a TypeError."""
     156          try:
     157              try:
     158                  raise Exception
     159              except object_:
     160                  pass
     161          except TypeError:
     162              pass
     163          except Exception:
     164              self.fail("TypeError expected when catching %s" % type(object_))
     165  
     166          try:
     167              try:
     168                  raise Exception
     169              except (object_,):
     170                  pass
     171          except TypeError:
     172              return
     173          except Exception:
     174              self.fail("TypeError expected when catching %s as specified in a "
     175                          "tuple" % type(object_))
     176  
     177      def test_raise_new_style_non_exception(self):
     178          # You cannot raise a new-style class that does not inherit from
     179          # BaseException; the ability was not possible until BaseException's
     180          # introduction so no need to support new-style objects that do not
     181          # inherit from it.
     182          class ESC[4;38;5;81mNewStyleClass(ESC[4;38;5;149mobject):
     183              pass
     184          self.raise_fails(NewStyleClass)
     185          self.raise_fails(NewStyleClass())
     186  
     187      def test_raise_string(self):
     188          # Raising a string raises TypeError.
     189          self.raise_fails("spam")
     190  
     191      def test_catch_non_BaseException(self):
     192          # Trying to catch an object that does not inherit from BaseException
     193          # is not allowed.
     194          class ESC[4;38;5;81mNonBaseException(ESC[4;38;5;149mobject):
     195              pass
     196          self.catch_fails(NonBaseException)
     197          self.catch_fails(NonBaseException())
     198  
     199      def test_catch_BaseException_instance(self):
     200          # Catching an instance of a BaseException subclass won't work.
     201          self.catch_fails(BaseException())
     202  
     203      def test_catch_string(self):
     204          # Catching a string is bad.
     205          self.catch_fails("spam")
     206  
     207  
     208  if __name__ == '__main__':
     209      unittest.main()