python (3.11.7)

(root)/
lib/
python3.11/
test/
test_unpack.py
       1  import doctest
       2  import unittest
       3  
       4  
       5  doctests = """
       6  
       7  Unpack tuple
       8  
       9      >>> t = (1, 2, 3)
      10      >>> a, b, c = t
      11      >>> a == 1 and b == 2 and c == 3
      12      True
      13  
      14  Unpack list
      15  
      16      >>> l = [4, 5, 6]
      17      >>> a, b, c = l
      18      >>> a == 4 and b == 5 and c == 6
      19      True
      20  
      21  Unpack implied tuple
      22  
      23      >>> a, b, c = 7, 8, 9
      24      >>> a == 7 and b == 8 and c == 9
      25      True
      26  
      27  Unpack string... fun!
      28  
      29      >>> a, b, c = 'one'
      30      >>> a == 'o' and b == 'n' and c == 'e'
      31      True
      32  
      33  Unpack generic sequence
      34  
      35      >>> class Seq:
      36      ...     def __getitem__(self, i):
      37      ...         if i >= 0 and i < 3: return i
      38      ...         raise IndexError
      39      ...
      40      >>> a, b, c = Seq()
      41      >>> a == 0 and b == 1 and c == 2
      42      True
      43  
      44  Single element unpacking, with extra syntax
      45  
      46      >>> st = (99,)
      47      >>> sl = [100]
      48      >>> a, = st
      49      >>> a
      50      99
      51      >>> b, = sl
      52      >>> b
      53      100
      54  
      55  Now for some failures
      56  
      57  Unpacking non-sequence
      58  
      59      >>> a, b, c = 7
      60      Traceback (most recent call last):
      61        ...
      62      TypeError: cannot unpack non-iterable int object
      63  
      64  Unpacking tuple of wrong size
      65  
      66      >>> a, b = t
      67      Traceback (most recent call last):
      68        ...
      69      ValueError: too many values to unpack (expected 2)
      70  
      71  Unpacking tuple of wrong size
      72  
      73      >>> a, b = l
      74      Traceback (most recent call last):
      75        ...
      76      ValueError: too many values to unpack (expected 2)
      77  
      78  Unpacking sequence too short
      79  
      80      >>> a, b, c, d = Seq()
      81      Traceback (most recent call last):
      82        ...
      83      ValueError: not enough values to unpack (expected 4, got 3)
      84  
      85  Unpacking sequence too long
      86  
      87      >>> a, b = Seq()
      88      Traceback (most recent call last):
      89        ...
      90      ValueError: too many values to unpack (expected 2)
      91  
      92  Unpacking a sequence where the test for too long raises a different kind of
      93  error
      94  
      95      >>> class BozoError(Exception):
      96      ...     pass
      97      ...
      98      >>> class BadSeq:
      99      ...     def __getitem__(self, i):
     100      ...         if i >= 0 and i < 3:
     101      ...             return i
     102      ...         elif i == 3:
     103      ...             raise BozoError
     104      ...         else:
     105      ...             raise IndexError
     106      ...
     107  
     108  Trigger code while not expecting an IndexError (unpack sequence too long, wrong
     109  error)
     110  
     111      >>> a, b, c, d, e = BadSeq()
     112      Traceback (most recent call last):
     113        ...
     114      test.test_unpack.BozoError
     115  
     116  Trigger code while expecting an IndexError (unpack sequence too short, wrong
     117  error)
     118  
     119      >>> a, b, c = BadSeq()
     120      Traceback (most recent call last):
     121        ...
     122      test.test_unpack.BozoError
     123  
     124  Allow unpacking empty iterables
     125  
     126      >>> () = []
     127      >>> [] = ()
     128      >>> [] = []
     129      >>> () = ()
     130  
     131  Unpacking non-iterables should raise TypeError
     132  
     133      >>> () = 42
     134      Traceback (most recent call last):
     135        ...
     136      TypeError: cannot unpack non-iterable int object
     137  
     138  Unpacking to an empty iterable should raise ValueError
     139  
     140      >>> () = [42]
     141      Traceback (most recent call last):
     142        ...
     143      ValueError: too many values to unpack (expected 0)
     144  
     145  """
     146  
     147  __test__ = {'doctests' : doctests}
     148  
     149  def load_tests(loader, tests, pattern):
     150      tests.addTest(doctest.DocTestSuite())
     151      return tests
     152  
     153  
     154  class ESC[4;38;5;81mTestCornerCases(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
     155      def test_extended_oparg_not_ignored(self):
     156          # https://github.com/python/cpython/issues/91625
     157          target = "(" + "y,"*400 + ")"
     158          code = f"""def unpack_400(x):
     159              {target} = x
     160              return y
     161          """
     162          ns = {}
     163          exec(code, ns)
     164          unpack_400 = ns["unpack_400"]
     165          # Warm up the function for quickening (PEP 659)
     166          for _ in range(30):
     167              y = unpack_400(range(400))
     168              self.assertEqual(y, 399)
     169  
     170  if __name__ == "__main__":
     171      unittest.main()