[Python-checkins] CVS: python/dist/src/Lib/test test_extcall.py,1.14,1.15 test_iter.py,1.9,1.10
Tim Peters
tim_one@users.sourceforge.net
Fri, 04 May 2001 20:56:39 -0700
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv17966/python/dist/src/Lib/test
Modified Files:
test_extcall.py test_iter.py
Log Message:
Generalize tuple() to work nicely with iterators.
NEEDS DOC CHANGES.
This one surprised me! While I expected tuple() to be a no-brainer, turns
out it's actually dripping with consequences:
1. It will *allow* the popular PySequence_Fast() to work with any iterable
object (code for that not yet checked in, but should be trivial).
2. It caused two std tests to fail. This because some places used
PyTuple_Sequence() (the C spelling of tuple()) as an indirect way to test
whether something *is* a sequence. But tuple() code only looked for the
existence of sq->item to determine that, and e.g. an instance passed
that test whether or not it supported the other operations tuple()
needed (e.g., __len__). So some things the tests *expected* to fail
with an AttributeError now fail with a TypeError instead. This looks
like an improvement to me; e.g., test_coercion used to produce 559
TypeErrors and 2 AttributeErrors, and now they're all TypeErrors. The
error details are more informative too, because the places calling this
were *looking* for TypeErrors in order to replace the generic tuple()
"not a sequence" msg with their own more specific text, and
AttributeErrors snuck by that.
Index: test_extcall.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_extcall.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -r1.14 -r1.15
*** test_extcall.py 2001/04/11 13:53:35 1.14
--- test_extcall.py 2001/05/05 03:56:37 1.15
***************
*** 59,66 ****
try:
g(*Nothing())
! except AttributeError, attr:
pass
else:
! print "should raise AttributeError: __len__"
class Nothing:
--- 59,66 ----
try:
g(*Nothing())
! except TypeError, attr:
pass
else:
! print "should raise TypeError"
class Nothing:
***************
*** 69,76 ****
try:
g(*Nothing())
! except AttributeError, attr:
pass
else:
! print "should raise AttributeError: __getitem__"
class Nothing:
--- 69,76 ----
try:
g(*Nothing())
! except TypeError, attr:
pass
else:
! print "should raise TypeError"
class Nothing:
Index: test_iter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_iter.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** test_iter.py 2001/05/04 04:39:21 1.9
--- test_iter.py 2001/05/05 03:56:37 1.10
***************
*** 276,279 ****
--- 276,312 ----
pass
+ # Test tuples()'s use of iterators.
+ def test_builtin_tuple(self):
+ self.assertEqual(tuple(SequenceClass(5)), (0, 1, 2, 3, 4))
+ self.assertEqual(tuple(SequenceClass(0)), ())
+ self.assertEqual(tuple([]), ())
+ self.assertEqual(tuple(()), ())
+ self.assertEqual(tuple("abc"), ("a", "b", "c"))
+
+ d = {"one": 1, "two": 2, "three": 3}
+ self.assertEqual(tuple(d), tuple(d.keys()))
+
+ self.assertRaises(TypeError, tuple, list)
+ self.assertRaises(TypeError, tuple, 42)
+
+ f = open(TESTFN, "w")
+ try:
+ for i in range(5):
+ f.write("%d\n" % i)
+ finally:
+ f.close()
+ f = open(TESTFN, "r")
+ try:
+ self.assertEqual(tuple(f), ("0\n", "1\n", "2\n", "3\n", "4\n"))
+ f.seek(0, 0)
+ self.assertEqual(tuple(f.xreadlines()),
+ ("0\n", "1\n", "2\n", "3\n", "4\n"))
+ finally:
+ f.close()
+ try:
+ unlink(TESTFN)
+ except OSError:
+ pass
+
# Test filter()'s use of iterators.
def test_builtin_filter(self):