+++ /dev/null
-doctests = """\r
-\r
-Unpack tuple\r
-\r
- >>> t = (1, 2, 3)\r
- >>> a, b, c = t\r
- >>> a == 1 and b == 2 and c == 3\r
- True\r
-\r
-Unpack list\r
-\r
- >>> l = [4, 5, 6]\r
- >>> a, b, c = l\r
- >>> a == 4 and b == 5 and c == 6\r
- True\r
-\r
-Unpack implied tuple\r
-\r
- >>> a, b, c = 7, 8, 9\r
- >>> a == 7 and b == 8 and c == 9\r
- True\r
-\r
-Unpack string... fun!\r
-\r
- >>> a, b, c = 'one'\r
- >>> a == 'o' and b == 'n' and c == 'e'\r
- True\r
-\r
-Unpack generic sequence\r
-\r
- >>> class Seq:\r
- ... def __getitem__(self, i):\r
- ... if i >= 0 and i < 3: return i\r
- ... raise IndexError\r
- ...\r
- >>> a, b, c = Seq()\r
- >>> a == 0 and b == 1 and c == 2\r
- True\r
-\r
-Single element unpacking, with extra syntax\r
-\r
- >>> st = (99,)\r
- >>> sl = [100]\r
- >>> a, = st\r
- >>> a\r
- 99\r
- >>> b, = sl\r
- >>> b\r
- 100\r
-\r
-Now for some failures\r
-\r
-Unpacking non-sequence\r
-\r
- >>> a, b, c = 7\r
- Traceback (most recent call last):\r
- ...\r
- TypeError: 'int' object is not iterable\r
-\r
-Unpacking tuple of wrong size\r
-\r
- >>> a, b = t\r
- Traceback (most recent call last):\r
- ...\r
- ValueError: too many values to unpack\r
-\r
-Unpacking tuple of wrong size\r
-\r
- >>> a, b = l\r
- Traceback (most recent call last):\r
- ...\r
- ValueError: too many values to unpack\r
-\r
-Unpacking sequence too short\r
-\r
- >>> a, b, c, d = Seq()\r
- Traceback (most recent call last):\r
- ...\r
- ValueError: need more than 3 values to unpack\r
-\r
-Unpacking sequence too long\r
-\r
- >>> a, b = Seq()\r
- Traceback (most recent call last):\r
- ...\r
- ValueError: too many values to unpack\r
-\r
-Unpacking a sequence where the test for too long raises a different kind of\r
-error\r
-\r
- >>> class BozoError(Exception):\r
- ... pass\r
- ...\r
- >>> class BadSeq:\r
- ... def __getitem__(self, i):\r
- ... if i >= 0 and i < 3:\r
- ... return i\r
- ... elif i == 3:\r
- ... raise BozoError\r
- ... else:\r
- ... raise IndexError\r
- ...\r
-\r
-Trigger code while not expecting an IndexError (unpack sequence too long, wrong\r
-error)\r
-\r
- >>> a, b, c, d, e = BadSeq()\r
- Traceback (most recent call last):\r
- ...\r
- BozoError\r
-\r
-Trigger code while expecting an IndexError (unpack sequence too short, wrong\r
-error)\r
-\r
- >>> a, b, c = BadSeq()\r
- Traceback (most recent call last):\r
- ...\r
- BozoError\r
-\r
-"""\r
-\r
-__test__ = {'doctests' : doctests}\r
-\r
-def test_main(verbose=False):\r
- from test import test_support\r
- from test import test_unpack\r
- test_support.run_doctest(test_unpack, verbose)\r
-\r
-if __name__ == "__main__":\r
- test_main(verbose=True)\r