test_unpack.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. doctests = """
  2. Unpack tuple
  3. >>> t = (1, 2, 3)
  4. >>> a, b, c = t
  5. >>> a == 1 and b == 2 and c == 3
  6. True
  7. Unpack list
  8. >>> l = [4, 5, 6]
  9. >>> a, b, c = l
  10. >>> a == 4 and b == 5 and c == 6
  11. True
  12. Unpack implied tuple
  13. >>> a, b, c = 7, 8, 9
  14. >>> a == 7 and b == 8 and c == 9
  15. True
  16. Unpack string... fun!
  17. >>> a, b, c = 'one'
  18. >>> a == 'o' and b == 'n' and c == 'e'
  19. True
  20. Unpack generic sequence
  21. >>> class Seq:
  22. ... def __getitem__(self, i):
  23. ... if i >= 0 and i < 3: return i
  24. ... raise IndexError
  25. ...
  26. >>> a, b, c = Seq()
  27. >>> a == 0 and b == 1 and c == 2
  28. True
  29. Single element unpacking, with extra syntax
  30. >>> st = (99,)
  31. >>> sl = [100]
  32. >>> a, = st
  33. >>> a
  34. 99
  35. >>> b, = sl
  36. >>> b
  37. 100
  38. Now for some failures
  39. Unpacking non-sequence
  40. >>> a, b, c = 7
  41. Traceback (most recent call last):
  42. ...
  43. TypeError: 'int' object is not iterable
  44. Unpacking tuple of wrong size
  45. >>> a, b = t
  46. Traceback (most recent call last):
  47. ...
  48. ValueError: too many values to unpack
  49. Unpacking tuple of wrong size
  50. >>> a, b = l
  51. Traceback (most recent call last):
  52. ...
  53. ValueError: too many values to unpack
  54. Unpacking sequence too short
  55. >>> a, b, c, d = Seq()
  56. Traceback (most recent call last):
  57. ...
  58. ValueError: need more than 3 values to unpack
  59. Unpacking sequence too long
  60. >>> a, b = Seq()
  61. Traceback (most recent call last):
  62. ...
  63. ValueError: too many values to unpack
  64. Unpacking a sequence where the test for too long raises a different kind of
  65. error
  66. >>> class BozoError(Exception):
  67. ... pass
  68. ...
  69. >>> class BadSeq:
  70. ... def __getitem__(self, i):
  71. ... if i >= 0 and i < 3:
  72. ... return i
  73. ... elif i == 3:
  74. ... raise BozoError
  75. ... else:
  76. ... raise IndexError
  77. ...
  78. Trigger code while not expecting an IndexError (unpack sequence too long, wrong
  79. error)
  80. >>> a, b, c, d, e = BadSeq()
  81. Traceback (most recent call last):
  82. ...
  83. BozoError
  84. Trigger code while expecting an IndexError (unpack sequence too short, wrong
  85. error)
  86. >>> a, b, c = BadSeq()
  87. Traceback (most recent call last):
  88. ...
  89. BozoError
  90. """
  91. __test__ = {'doctests' : doctests}
  92. def test_main(verbose=False):
  93. from test import test_support
  94. from test import test_unpack
  95. test_support.run_doctest(test_unpack, verbose)
  96. if __name__ == "__main__":
  97. test_main(verbose=True)