[Python-checkins] python/dist/src/Lib/test test_builtin.py,1.11,1.12
doerwalter@users.sourceforge.net
doerwalter@users.sourceforge.net
Mon, 10 Feb 2003 05:19:15 -0800
Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv29110/Lib/test
Modified Files:
test_builtin.py
Log Message:
Change filterstring() and filterunicode(): If the
object is not a real str or unicode but an instance
of a subclass, construct the output via looping
over __getitem__. This guarantees that the result
is the same for function==None and function==lambda x:x
This doesn't happen for tuples, because filtertuple()
uses PyTuple_GetItem().
(This was discussed on SF bug #665835).
Index: test_builtin.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_builtin.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** test_builtin.py 10 Feb 2003 08:21:05 -0000 1.11
--- test_builtin.py 10 Feb 2003 13:19:13 -0000 1.12
***************
*** 419,442 ****
def test_filter_subclasses(self):
# test, that filter() never returns tuple, str or unicode subclasses
funcs = (None, lambda x: True)
class tuple2(tuple):
! pass
class str2(str):
! pass
inputs = {
! tuple2: [(), (1,2,3)],
! str2: ["", "123"]
}
if have_unicode:
class unicode2(unicode):
! pass
! inputs[unicode2] = [unicode(), unicode("123")]
! for func in funcs:
! for (cls, inps) in inputs.iteritems():
! for inp in inps:
! out = filter(func, cls(inp))
! self.assertEqual(inp, out)
! self.assert_(not isinstance(out, cls))
def test_float(self):
--- 419,456 ----
def test_filter_subclasses(self):
# test, that filter() never returns tuple, str or unicode subclasses
+ # and that the result always go's through __getitem__
+ # FIXME: For tuple currently it doesn't go through __getitem__
funcs = (None, lambda x: True)
class tuple2(tuple):
! def __getitem__(self, index):
! return 2*tuple.__getitem__(self, index)
class str2(str):
! def __getitem__(self, index):
! return 2*str.__getitem__(self, index)
inputs = {
! tuple2: {(): (), (1, 2, 3): (1, 2, 3)}, # FIXME
! str2: {"": "", "123": "112233"}
}
if have_unicode:
class unicode2(unicode):
! def __getitem__(self, index):
! return 2*unicode.__getitem__(self, index)
! inputs[unicode2] = {
! unicode(): unicode(),
! unicode("123"): unicode("112233")
! }
! for (cls, inps) in inputs.iteritems():
! for (inp, exp) in inps.iteritems():
! # make sure the output goes through __getitem__
! # even if func is None
! self.assertEqual(
! filter(funcs[0], cls(inp)),
! filter(funcs[1], cls(inp))
! )
! for func in funcs:
! outp = filter(func, cls(inp))
! self.assertEqual(outp, exp)
! self.assert_(not isinstance(outp, cls))
def test_float(self):