[pypy-svn] r4689 - in pypy/trunk/src/pypy/module: . test

arigo at codespeak.net arigo at codespeak.net
Thu May 27 18:09:44 CEST 2004


Author: arigo
Date: Thu May 27 18:09:43 2004
New Revision: 4689

Modified:
   pypy/trunk/src/pypy/module/__builtin__interp.py
   pypy/trunk/src/pypy/module/__builtin__module.py
   pypy/trunk/src/pypy/module/test/test_builtin.py
Log:
super() hack.


Modified: pypy/trunk/src/pypy/module/__builtin__interp.py
==============================================================================
--- pypy/trunk/src/pypy/module/__builtin__interp.py	(original)
+++ pypy/trunk/src/pypy/module/__builtin__interp.py	Thu May 27 18:09:43 2004
@@ -162,3 +162,8 @@
 def setattr(w_object, w_name, w_val):
     space.setattr(w_object, w_name, w_val)
     return space.w_None
+
+def _pypy_get(w_value, w_self, w_class=None):   # XXX temporary
+    if w_class is None:
+        w_class = space.w_None
+    return space.get(w_value, w_self, w_class)

Modified: pypy/trunk/src/pypy/module/__builtin__module.py
==============================================================================
--- pypy/trunk/src/pypy/module/__builtin__module.py	(original)
+++ pypy/trunk/src/pypy/module/__builtin__module.py	Thu May 27 18:09:43 2004
@@ -426,7 +426,7 @@
 from __interplevel__ import abs, chr, len, ord, pow, repr
 from __interplevel__ import hash, oct, hex, round
 from __interplevel__ import getattr, setattr, delattr, iter, hash, id
-from __interplevel__ import issubclass
+from __interplevel__ import issubclass, _pypy_get
 from __interplevel__ import compile
 from __interplevel__ import globals, locals, _caller_globals, _caller_locals
 
@@ -487,14 +487,15 @@
                 i+=step
 
 
-# XXX the following comes from http://<<<fill this blank>>>
+# Descriptor code, shamelessly stolen to Raymond Hettinger:
+#    http://users.rcn.com/python/download/Descriptor.htm
 class property(object):
 
     def __init__(self, fget=None, fset=None, fdel=None, doc=None):
         self.fget = fget
         self.fset = fset
         self.fdel = fdel
-        self.__doc__ = doc or ""
+        self.__doc__ = doc or ""   # XXX why:  or ""  ?
 
     def __get__(self, obj, objtype=None):
         if obj is None:
@@ -536,6 +537,42 @@
         return newfunc
 
 
+# super is a modified version from Guido's tutorial
+#     http://www.python.org/2.2.3/descrintro.html
+# it exposes the same special attributes as CPython's.
+class super(object):
+    def __init__(self, type, obj=None):
+        self.__thisclass__ = type
+        self.__self__ = obj
+        if obj is not None and isinstance(obj, type):
+            self.__self_class__ = obj.__class__
+        else:
+            self.__self_class__ = obj
+    def __get__(self, obj, type=None):
+        if self.__self__ is None and obj is not None:
+            return super(self.__thisclass__, obj)
+        else:
+            return self
+    def __getattr__(self, attr):
+        mro = iter(self.__self_class__.__mro__)
+        for cls in mro:
+            if cls is self.__thisclass__:
+                break
+        # Note: mro is an iterator, so the second loop
+        # picks up where the first one left off!
+        for cls in mro:
+            try:
+                # XXX
+                # XXX  build-in classes have no __dict__ currently!
+                # XXX
+                x = getattr(cls, attr)
+            except AttributeError:
+                continue
+            x = _pypy_get(x, self.__self__)    # XXX replace with x.__get__
+            return x
+        raise AttributeError, attr
+
+
 # ________________________________________________________________________
 ##    def app___import__(*args):
 ##        # NOTE: No import statements can be done in this function,

Modified: pypy/trunk/src/pypy/module/test/test_builtin.py
==============================================================================
--- pypy/trunk/src/pypy/module/test/test_builtin.py	(original)
+++ pypy/trunk/src/pypy/module/test/test_builtin.py	Thu May 27 18:09:43 2004
@@ -230,7 +230,21 @@
         self.assertRaises(TypeError, hash, [])
         self.assertRaises(TypeError, hash, {})
 
-
+    def test_super(self):
+        class A:
+            def f(self):
+                return 'A'
+        class B(A):
+            def f(self):
+                return 'B' + super(B,self).f()
+        class C(A):
+            def f(self):
+                return 'C' + super(C,self).f()
+        class D(B, C):
+            def f(self):
+                return 'D' + super(D,self).f()
+        d = D()
+        self.assertEquals(d.f(), "DBCA")
 
         
 class TestInternal(testit.IntTestCase):



More information about the Pypy-commit mailing list