[pypy-svn] r9457 - in pypy/dist/pypy/translator: . test

pedronis at codespeak.net pedronis at codespeak.net
Wed Feb 23 17:08:35 CET 2005


Author: pedronis
Date: Wed Feb 23 17:08:35 2005
New Revision: 9457

Modified:
   pypy/dist/pypy/translator/genc.py
   pypy/dist/pypy/translator/test/snippet.py
   pypy/dist/pypy/translator/test/test_ctrans.py
Log:
reproduce copy_reg._reconstructor behavior, this should avoid calling class own __new__,
this  doesn't support slots way or __new__ with complex side-effects (they are very likely overkill for our 
purpose)



Modified: pypy/dist/pypy/translator/genc.py
==============================================================================
--- pypy/dist/pypy/translator/genc.py	(original)
+++ pypy/dist/pypy/translator/genc.py	Wed Feb 23 17:08:35 2005
@@ -240,8 +240,15 @@
         self.latercode.append((gen, self.debugstack))
 
     def nameof_instance(self, instance):
-        name = self.uniquename('ginst_' + instance.__class__.__name__)
-        cls = self.nameof(instance.__class__)
+        klass = instance.__class__
+        name = self.uniquename('ginst_' + klass.__name__)
+        cls = self.nameof(klass)
+        if hasattr(klass, '__base__'):
+            base_class = klass.__base__
+            base = self.nameof(base_class)
+        else:
+            base_class = None
+            base = cls
         def initinstance():
             content = instance.__dict__.items()
             content.sort()
@@ -253,15 +260,15 @@
             import copy_reg
             reduced = instance.__reduce_ex__()
             assert reduced[0] is copy_reg._reconstructor
+            assert reduced[1][1] is base_class
             state = reduced[1][2]
         else:
             state = None
         self.initcode.append('if isinstance(%s, type):' % cls)
         if state is not None:
-            #print "INST",'    %s = %s.__new__(%s, %r)' % (name, cls, cls, state)
-            self.initcode.append('    %s = %s.__new__(%s, %r)' % (name, cls, cls, state))
+            self.initcode.append('    %s = %s.__new__(%s, %r)' % (name, base, cls, state))
         else:
-            self.initcode.append('    %s = %s.__new__(%s)' % (name, cls, cls))
+            self.initcode.append('    %s = %s.__new__(%s)' % (name, base, cls))
         self.initcode.append('else:')
         self.initcode.append('    %s = new.instance(%s)' % (name, cls))
         self.later(initinstance())

Modified: pypy/dist/pypy/translator/test/snippet.py
==============================================================================
--- pypy/dist/pypy/translator/test/snippet.py	(original)
+++ pypy/dist/pypy/translator/test/snippet.py	Wed Feb 23 17:08:35 2005
@@ -826,3 +826,12 @@
 def one_thing1():
     return thing1
 
+
+class Thing2(long):
+    def __new__(t,v):
+       return  long.__new__(t,v*2)
+
+thing2 = Thing2(2)
+
+def one_thing2():
+    return thing2

Modified: pypy/dist/pypy/translator/test/test_ctrans.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_ctrans.py	(original)
+++ pypy/dist/pypy/translator/test/test_ctrans.py	Wed Feb 23 17:08:35 2005
@@ -197,6 +197,10 @@
         fn = self.build_cfunc(snippet.one_thing1)
         assert fn().thingness == 1
 
+    def test_global_const_w_new(self):
+        fn = self.build_cfunc(snippet.one_thing2)
+        assert fn() == 4
+
 class TestTypedTestCase:
 
     def getcompiled(self, func):



More information about the Pypy-commit mailing list