[Python-checkins] CVS: python/dist/src/Lib/test test_descr.py,1.50,1.51

Tim Peters tim_one@users.sourceforge.net
2001年9月12日 22:38:57 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv20497/python/Lib/test
Modified Files:
	test_descr.py 
Log Message:
SF bug [#460467] file objects should be subclassable.
Preliminary support. What's here works, but needs fine-tuning.
Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.50
retrieving revision 1.51
diff -C2 -d -r1.50 -r1.51
*** test_descr.py	2001年09月12日 19:53:15	1.50
--- test_descr.py	2001年09月13日 05:38:55	1.51
***************
*** 1,5 ****
 # Test descriptor-related enhancements
 
! from test_support import verify, verbose, TestFailed
 from copy import deepcopy
 
--- 1,5 ----
 # Test descriptor-related enhancements
 
! from test_support import verify, verbose, TestFailed, TESTFN
 from copy import deepcopy
 
***************
*** 1636,1639 ****
--- 1636,1686 ----
 verify(u[0:0].__class__ is unicode)
 verify(u[0:0] == u"")
+ 
+ class CountedInput(file):
+ """Counts lines read by self.readline().
+ 
+ self.lineno is the 0-based ordinal of the last line read, up to
+ a maximum of one greater than the number of lines in the file.
+ 
+ self.ateof is true if and only if the final "" line has been read,
+ at which point self.lineno stops incrementing, and further calls
+ to readline() continue to return "".
+ """
+ 
+ lineno = 0
+ ateof = 0
+ def readline(self):
+ if self.ateof:
+ return ""
+ s = file.readline(self)
+ # Next line works too.
+ # s = super(CountedInput, self).readline()
+ self.lineno += 1
+ if s == "":
+ self.ateof = 1
+ return s
+ 
+ f = open(TESTFN, 'w')
+ lines = ['a\n', 'b\n', 'c\n']
+ try:
+ f.writelines(lines)
+ f.close()
+ f = CountedInput(TESTFN)
+ for (i, expected) in zip(range(1, 5) + [4], lines + 2 * [""]):
+ got = f.readline()
+ verify(expected == got)
+ verify(f.lineno == i)
+ verify(f.ateof == (i > len(lines)))
+ f.close()
+ finally:
+ try:
+ f.close()
+ except:
+ pass
+ try:
+ import os
+ os.unlink(TESTFN)
+ except:
+ pass
 
 def all():

AltStyle によって変換されたページ (->オリジナル) /