summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-06-21 02:49:55 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-06-21 02:49:55 (GMT)
commitd6d010b874a82df889037bdb1cc71032d2155034 (patch)
tree643bda2ce2915c0142dfd240afb36bd5b1141403 /Lib
parent2b13ce831795f6f0304c157a31909ac2ce5d5ccb (diff)
downloadcpython-d6d010b874a82df889037bdb1cc71032d2155034.zip
cpython-d6d010b874a82df889037bdb1cc71032d2155034.tar.gz
cpython-d6d010b874a82df889037bdb1cc71032d2155034.tar.bz2
Teach the UNPACK_SEQUENCE opcode how to tease an iterable object into
giving up the goods. NEEDS DOC CHANGES
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_iter.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index ddc58a7..63e488e 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -594,4 +594,57 @@ class TestCase(unittest.TestCase):
except OSError:
pass
+ # Test iterators on RHS of unpacking assignments.
+ def test_unpack_iter(self):
+ a, b = 1, 2
+ self.assertEqual((a, b), (1, 2))
+
+ a, b, c = IteratingSequenceClass(3)
+ self.assertEqual((a, b, c), (0, 1, 2))
+
+ try: # too many values
+ a, b = IteratingSequenceClass(3)
+ except ValueError:
+ pass
+ else:
+ self.fail("should have raised ValueError")
+
+ try: # not enough values
+ a, b, c = IteratingSequenceClass(2)
+ except ValueError:
+ pass
+ else:
+ self.fail("should have raised ValueError")
+
+ try: # not iterable
+ a, b, c = len
+ except TypeError:
+ pass
+ else:
+ self.fail("should have raised TypeError")
+
+ a, b, c = {1: 42, 2: 42, 3: 42}.itervalues()
+ self.assertEqual((a, b, c), (42, 42, 42))
+
+ f = open(TESTFN, "w")
+ lines = ("a\n", "bb\n", "ccc\n")
+ try:
+ for line in lines:
+ f.write(line)
+ finally:
+ f.close()
+ f = open(TESTFN, "r")
+ try:
+ a, b, c = f
+ self.assertEqual((a, b, c), lines)
+ finally:
+ f.close()
+ try:
+ unlink(TESTFN)
+ except OSError:
+ pass
+
+ (a, b), (c,) = IteratingSequenceClass(2), {42: 24}
+ self.assertEqual((a, b, c), (0, 1, 42))
+
run_unittest(TestCase)