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

Tim Peters tim_one@users.sourceforge.net
2001年10月16日 13:18:26 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv11274/python/Lib/test
Modified Files:
	test_descr.py 
Log Message:
SF bug [#468061] __str__ ignored in str subclass.
object.c, PyObject_Str: Don't try to optimize anything except exact
string objects here; in particular, let str subclasses go thru tp_str,
same as non-str objects. This allows overrides of tp_str to take
effect.
stringobject.c:
+ string_print (str's tp_print): If the argument isn't an exact string
 object, get one from PyObject_Str.
+ string_str (str's tp_str): Make a genuine-string copy of the object if
 it's of a proper str subclass type. str() applied to a str subclass
 that doesn't override __str__ ends up here.
test_descr.py: New str_of_str_subclass() test.
Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.89
retrieving revision 1.90
diff -C2 -d -r1.89 -r1.90
*** test_descr.py	2001年10月15日 21:05:10	1.89
--- test_descr.py	2001年10月16日 20:18:24	1.90
***************
*** 2299,2302 ****
--- 2299,2332 ----
 pass
 
+ def str_of_str_subclass():
+ import binascii
+ import cStringIO
+ 
+ if verbose:
+ print "Testing __str__ defined in subclass of str ..."
+ 
+ class octetstring(str):
+ def __str__(self):
+ return binascii.b2a_hex(self)
+ def __repr__(self):
+ return self + " repr"
+ 
+ o = octetstring('A')
+ vereq(type(o), octetstring)
+ vereq(type(str(o)), str)
+ vereq(type(repr(o)), str)
+ vereq(ord(o), 0x41)
+ vereq(str(o), '41')
+ vereq(repr(o), 'A repr')
+ vereq(o.__str__(), '41')
+ vereq(o.__repr__(), 'A repr')
+ 
+ capture = cStringIO.StringIO()
+ # Calling str() or not exercises different internal paths.
+ print >> capture, o
+ print >> capture, str(o)
+ vereq(capture.getvalue(), '41\n41\n')
+ capture.close()
+ 
 def test_main():
 class_docstrings()
***************
*** 2347,2350 ****
--- 2377,2381 ----
 subclasspropagation()
 buffer_inherit()
+ str_of_str_subclass()
 if verbose: print "All OK"
 

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