[Python-checkins] python/dist/src/Lib/test test_class.py,1.11,1.12

nascheme at users.sourceforge.net nascheme at users.sourceforge.net
Mon Jul 19 18:29:18 CEST 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28866/Lib/test

Modified Files:
	test_class.py 
Log Message:
Check the type of values returned by __int__, __float__, __long__,
__oct__, and __hex__.  Raise TypeError if an invalid type is
returned.  Note that PyNumber_Int and PyNumber_Long can still
return ints or longs.  Fixes SF bug #966618.


Index: test_class.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_class.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** test_class.py	20 Oct 2003 14:01:51 -0000	1.11
--- test_class.py	19 Jul 2004 16:29:15 -0000	1.12
***************
*** 44,52 ****
      "pos",
      "abs",
-     "int",
-     "long",
-     "float",
-     "oct",
-     "hex",
  
  # generic operations
--- 44,47 ----
***************
*** 59,62 ****
--- 54,62 ----
  #    "str",
  #    "repr",
+ #    "int",
+ #    "long",
+ #    "float",
+ #    "oct",
+ #    "hex",
  
  # These are separate because they can influence the test of other methods.
***************
*** 82,85 ****
--- 82,105 ----
          return "AllTests"
  
+     def __int__(self, *args):
+         print "__int__:", args
+         return 1
+ 
+     def __float__(self, *args):
+         print "__float__:", args
+         return 1.0
+ 
+     def __long__(self, *args):
+         print "__long__:", args
+         return 1L
+ 
+     def __oct__(self, *args):
+         print "__oct__:", args
+         return '01'
+ 
+     def __hex__(self, *args):
+         print "__hex__:", args
+         return '0x1'
+ 
      def __cmp__(self, *args):
          print "__cmp__:", args
***************
*** 196,214 ****
  +testme
  abs(testme)
! if sys.platform[:4] != 'java':
!     int(testme)
!     long(testme)
!     float(testme)
!     oct(testme)
!     hex(testme)
! else:
!     # Jython enforced that these methods return
!     # a value of the expected type.
!     print "__int__: ()"
!     print "__long__: ()"
!     print "__float__: ()"
!     print "__oct__: ()"
!     print "__hex__: ()"
! 
  
  # And the rest...
--- 216,224 ----
  +testme
  abs(testme)
! int(testme)
! long(testme)
! float(testme)
! oct(testme)
! hex(testme)
  
  # And the rest...
***************
*** 255,258 ****
--- 265,316 ----
  
  
+ # return values of some method are type-checked
+ class BadTypeClass:
+     def __int__(self):
+         return None
+     __float__ = __int__
+     __long__ = __int__
+     __str__ = __int__
+     __repr__ = __int__
+     __oct__ = __int__
+     __hex__ = __int__
+ 
+ def check_exc(stmt, exception):
+     """Raise TestFailed if executing 'stmt' does not raise 'exception'
+     """
+     try:
+         exec stmt
+     except exception:
+         pass
+     else:
+         raise TestFailed, "%s should raise %s" % (stmt, exception)
+ 
+ check_exc("int(BadTypeClass())", TypeError)
+ check_exc("float(BadTypeClass())", TypeError)
+ check_exc("long(BadTypeClass())", TypeError)
+ check_exc("str(BadTypeClass())", TypeError)
+ check_exc("repr(BadTypeClass())", TypeError)
+ check_exc("oct(BadTypeClass())", TypeError)
+ check_exc("hex(BadTypeClass())", TypeError)
+ 
+ # mixing up ints and longs is okay
+ class IntLongMixClass:
+     def __int__(self):
+         return 0L
+ 
+     def __long__(self):
+         return 0
+ 
+ try:
+     int(IntLongMixClass())
+ except TypeError:
+     raise TestFailed, "TypeError should not be raised"
+ 
+ try:
+     long(IntLongMixClass())
+ except TypeError:
+     raise TestFailed, "TypeError should not be raised"
+ 
+ 
  # Test correct errors from hash() on objects with comparisons but no __hash__
  
***************
*** 265,279 ****
      def __cmp__(self, other): return 0
  
! try: hash(C1())
! except TypeError: pass
! else: raise TestFailed, "hash(C1()) should raise an exception"
  
  class C2:
      def __eq__(self, other): return 1
  
! try: hash(C2())
! except TypeError: pass
! else: raise TestFailed, "hash(C2()) should raise an exception"
! 
  
  # Test for SF bug 532646
--- 323,332 ----
      def __cmp__(self, other): return 0
  
! check_exc("hash(C1())", TypeError)
  
  class C2:
      def __eq__(self, other): return 1
  
! check_exc("hash(C2())", TypeError)
  
  # Test for SF bug 532646



More information about the Python-checkins mailing list