[Python-checkins] CVS: python/dist/src/Lib/test test_b2.py,1.24,1.25 test_iter.py,1.15,1.16

Tim Peters tim_one@users.sourceforge.net
Sat, 05 May 2001 18:05:03 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv31904/python/dist/src/Lib/test

Modified Files:
	test_b2.py test_iter.py 
Log Message:
Generalize zip() to work with iterators.
NEEDS DOC CHANGES.
More AttributeErrors transmuted into TypeErrors, in test_b2.py, and,
again, this strikes me as a good thing.
This checkin completes the iterator generalization work that obviously
needed to be done.  Can anyone think of others that should be changed?


Index: test_b2.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_b2.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -r1.24 -r1.25
*** test_b2.py	2001/01/19 21:57:52	1.24
--- test_b2.py	2001/05/06 01:05:01	1.25
***************
*** 310,314 ****
  try:
      zip(a, G())
! except AttributeError:
      exc = 1
  except:
--- 310,314 ----
  try:
      zip(a, G())
! except TypeError:
      exc = 1
  except:
***************
*** 316,320 ****
      raise TestFailed, 'zip(a, b) - b instance w/o __getitem__'
  if not exc:
!     raise TestFailed, 'zip(a, b) - missing expected AttributeError'
  
  
--- 316,320 ----
      raise TestFailed, 'zip(a, b) - b instance w/o __getitem__'
  if not exc:
!     raise TestFailed, 'zip(a, b) - missing expected TypeError'
  
  

Index: test_iter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_iter.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -r1.15 -r1.16
*** test_iter.py	2001/05/05 21:36:52	1.15
--- test_iter.py	2001/05/06 01:05:01	1.16
***************
*** 419,422 ****
--- 419,468 ----
                  pass
  
+     # Test zip()'s use of iterators.
+     def test_builtin_zip(self):
+         self.assertRaises(TypeError, zip)
+         self.assertRaises(TypeError, zip, None)
+         self.assertRaises(TypeError, zip, range(10), 42)
+         self.assertRaises(TypeError, zip, range(10), zip)
+ 
+         self.assertEqual(zip(IteratingSequenceClass(3)),
+                          [(0,), (1,), (2,)])
+         self.assertEqual(zip(SequenceClass(3)),
+                          [(0,), (1,), (2,)])
+ 
+         d = {"one": 1, "two": 2, "three": 3}
+         self.assertEqual(d.items(), zip(d, d.itervalues()))
+ 
+         # Generate all ints starting at constructor arg.
+         class IntsFrom:
+             def __init__(self, start):
+                 self.i = start
+ 
+             def __iter__(self):
+                 return self
+ 
+             def next(self):
+                 i = self.i
+                 self.i = i+1
+                 return i
+ 
+         f = open(TESTFN, "w")
+         try:
+             f.write("a\n" "bbb\n" "cc\n")
+         finally:
+             f.close()
+         f = open(TESTFN, "r")
+         try:
+             self.assertEqual(zip(IntsFrom(0), f, IntsFrom(-100)),
+                              [(0, "a\n", -100),
+                               (1, "bbb\n", -99),
+                               (2, "cc\n", -98)])
+         finally:
+             f.close()
+             try:
+                 unlink(TESTFN)
+             except OSError:
+                 pass
+ 
      # Test reduces()'s use of iterators.
      def test_builtin_reduce(self):