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

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 24 Sep 2001 20:43:44 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv7010/Lib/test

Modified Files:
	test_descr.py 
Log Message:
Make __class__ assignment possible, when the object structures are the
same.  I hope the test for structural equivalence is stringent enough.
It only allows the assignment if the old and new types:

- have the same basic size
- have the same item size
- have the same dict offset
- have the same weaklist offset
- have the same GC flag bit
- have a common base that is the same except for maybe the dict and
  weaklist (which may have been added separately at the same offsets
  in both types)



Index: test_descr.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_descr.py,v
retrieving revision 1.74
retrieving revision 1.75
diff -C2 -d -r1.74 -r1.75
*** test_descr.py	2001/09/24 21:17:50	1.74
--- test_descr.py	2001/09/25 03:43:42	1.75
***************
*** 2006,2009 ****
--- 2006,2036 ----
      check(file.name, "file name") # member descriptor
  
+ def setclass():
+     if verbose: print "Testing __class__ assignment..."
+     class C(object): pass
+     class D(object): pass
+     class E(object): pass
+     class F(D, E): pass
+     for cls in C, D, E, F:
+         for cls2 in C, D, E, F:
+             x = cls()
+             x.__class__ = cls2
+             verify(x.__class__ is cls2)
+             x.__class__ = cls
+             verify(x.__class__ is cls)
+     def cant(x, C):
+         try:
+             x.__class__ = C
+         except TypeError:
+             pass
+         else:
+             raise TestFailed, "shouldn't allow %r.__class__ = %r" % (x, C)
+     cant(C(), list)
+     cant(list(), C)
+     cant(C(), 1)
+     cant(C(), object)
+     cant(object(), list)
+     cant(list(), object)
+ 
  
  def test_main():
***************
*** 2048,2051 ****
--- 2075,2079 ----
      coercions()
      descrdoc()
+     setclass()
      if verbose: print "All OK"