[Python-checkins] CVS: python/dist/src/Lib/test test_contains.py,1.6,1.7 test_iter.py,1.11,1.12

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


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

Modified Files:
	test_contains.py test_iter.py 
Log Message:
Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
NEEDS DOC CHANGES
A few more AttributeErrors turned into TypeErrors, but in test_contains
this time.
The full story for instance objects is pretty much unexplainable, because
instance_contains() tries its own flavor of iteration-based containment
testing first, and PySequence_Contains doesn't get a chance at it unless
instance_contains() blows up.  A consequence is that
    some_complex_number in some_instance
dies with a TypeError unless some_instance.__class__ defines __iter__ but
does not define __getitem__.


Index: test_contains.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_contains.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** test_contains.py	2000/10/23 17:22:07	1.6
--- test_contains.py	2001/05/05 10:06:14	1.7
***************
*** 32,36 ****
      1 in a
      check(0, "in base_set did not raise error")
! except AttributeError:
      pass
  
--- 32,36 ----
      1 in a
      check(0, "in base_set did not raise error")
! except TypeError:
      pass
  
***************
*** 38,42 ****
      1 not in a
      check(0, "not in base_set did not raise error")
! except AttributeError:
      pass
  
--- 38,42 ----
      1 not in a
      check(0, "not in base_set did not raise error")
! except TypeError:
      pass
  

Index: test_iter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_iter.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** test_iter.py	2001/05/05 05:36:48	1.11
--- test_iter.py	2001/05/05 10:06:14	1.12
***************
*** 473,475 ****
--- 473,530 ----
                  pass
  
+     # Test iterators with 'x in y' and 'x not in y'.
+     def test_in_and_not_in(self):
+         sc5 = IteratingSequenceClass(5)
+         for i in range(5):
+             self.assert_(i in sc5)
+         # CAUTION:  This test fails on 3-12j if sc5 is SequenceClass(5)
+         # instead, with:
+         #     TypeError: cannot compare complex numbers using <, <=, >, >=
+         # The trail leads back to instance_contains() in classobject.c,
+         # under comment:
+         #     /* fall back to previous behavior */
+         # IteratingSequenceClass(5) avoids the same problem only because
+         # it lacks __getitem__:  instance_contains *tries* to do a wrong
+         # thing with it too, but aborts with an AttributeError the first
+         # time it calls instance_item(); PySequence_Contains() then catches
+         # that and clears it, and tries the iterator-based "contains"
+         # instead.  But this is hanging together by a thread.
+         for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
+             self.assert_(i not in sc5)
+         del sc5
+ 
+         self.assertRaises(TypeError, lambda: 3 in 12)
+         self.assertRaises(TypeError, lambda: 3 not in map)
+ 
+         d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
+         for k in d:
+             self.assert_(k in d)
+             self.assert_(k not in d.itervalues())
+         for v in d.values():
+             self.assert_(v in d.itervalues())
+             self.assert_(v not in d)
+         for k, v in d.iteritems():
+             self.assert_((k, v) in d.iteritems())
+             self.assert_((v, k) not in d.iteritems())
+         del d
+ 
+         f = open(TESTFN, "w")
+         try:
+             f.write("a\n" "b\n" "c\n")
+         finally:
+             f.close()
+         f = open(TESTFN, "r")
+         try:
+             for chunk in "abc":
+                 f.seek(0, 0)
+                 self.assert_(chunk not in f)
+                 f.seek(0, 0)
+                 self.assert_((chunk + "\n") in f)
+         finally:
+             f.close()
+             try:
+                 unlink(TESTFN)
+             except OSError:
+                 pass
+ 
  run_unittest(TestCase)