[pypy-svn] rev 2369 - pypy/trunk/src/pypy/interpreter

pedronis at codespeak.net pedronis at codespeak.net
Tue Dec 16 15:14:32 CET 2003


Author: pedronis
Date: Tue Dec 16 15:14:31 2003
New Revision: 2369

Modified:
   pypy/trunk/src/pypy/interpreter/baseobjspace.py
   pypy/trunk/src/pypy/interpreter/function.py
Log:
- refactored function pypy_getattr, implemented genericly in Wrappable
  using a dict (w_dict) mapping names to the attribute values


Modified: pypy/trunk/src/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/src/pypy/interpreter/baseobjspace.py	Tue Dec 16 15:14:31 2003
@@ -9,6 +9,29 @@
 class Wrappable(object):
     """A subclass of Wrappable is an internal, interpreter-level class
     that can nevertheless be exposed at application-level by space.wrap()."""
+    w_dict = None
+
+    def get_wdict(self):
+        space = self.space
+        if self.w_dict is None:
+            w_dict = self.w_dict = space.newdict([])
+            for name,w_value in self.app_visible():
+                space.setitem(w_dict,space.wrap(name),w_value)
+        return self.w_dict
+
+    def app_visible(self):
+        """ returns [(name,w_value)...] for application-level visible attributes """ 
+        raise NotImplementedError
+            
+    def pypy_getattr(self,w_name):
+        space = self.space
+        w_dict = self.get_wdict()
+        try:
+            return space.getitem(w_dict,w_name)
+        except OperationError,e:
+            if not e.match(space,space.w_KeyError):
+                raise
+            raise OperationError(space.w_AttributeError,w_name)
 
 
 class NoValue(Exception):

Modified: pypy/trunk/src/pypy/interpreter/function.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/function.py	(original)
+++ pypy/trunk/src/pypy/interpreter/function.py	Tue Dec 16 15:14:31 2003
@@ -183,15 +183,15 @@
         # (for FlowObjSpace)
         return self.space.call(wrap(self), w_args, w_kwds)
 
-    def pypy_getattr(self, w_name):
-        space = self.space
-        raise OperationError(space.w_AttributeError, w_name)
+    #def pypy_getattr(self, w_name):
+    #    space = self.space
+    #    raise OperationError(space.w_AttributeError, w_name)
 
     def app_visible(self):  
         space = self.space
-        def f(*kw):
+        def items(**kw):
             return kw.items()
-        return f(
+        return items(
                 func_defaults = self.defs_w and space.newtuple(self.defs_w) or space.w_None,
                 func_code = space.wrap(self.code),
                 func_dict = self.w_func_dict,
@@ -199,7 +199,8 @@
                 __doc__ = space.wrap(self.doc),
                 func_name = space.wrap(self.name),
                 __name__ = space.wrap(self.name),
-                func_globals = self.w_func_globals)
+                func_globals = self.w_func_globals,
+                func_closure = space.wrap(self.closure))
 
 class Method(object):
     """A method is a function bound to a specific instance or class."""


More information about the Pypy-commit mailing list