[pypy-svn] r8528 - pypy/dist/pypy/interpreter

arigo at codespeak.net arigo at codespeak.net
Mon Jan 24 15:11:27 CET 2005


Author: arigo
Date: Mon Jan 24 15:11:27 2005
New Revision: 8528

Modified:
   pypy/dist/pypy/interpreter/module.py
   pypy/dist/pypy/interpreter/typedef.py
Log:
Added a method 'ModuleType.__getattr__' that handles the special attributes
sys.exc_{type,value,traceback}.  It also gives a nicer error message for
module attributes not found.



Modified: pypy/dist/pypy/interpreter/module.py
==============================================================================
--- pypy/dist/pypy/interpreter/module.py	(original)
+++ pypy/dist/pypy/interpreter/module.py	Mon Jan 24 15:11:27 2005
@@ -3,6 +3,7 @@
 """
 
 from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.error import OperationError
 
 class Module(Wrappable):
     """A module."""
@@ -34,3 +35,34 @@
         space.setitem(self.w_dict, space.wrap('__name__'), w_name)
         if w_doc is not None:
             space.setitem(self.w_dict, space.wrap('__doc__'), w_doc)
+
+    def descr_module__getattr__(self, w_attr):
+        space = self.space
+        attr = space.str_w(w_attr)
+        # ______ for the 'sys' module only _____ XXX generalize
+        if self is space.sys:
+            if attr == 'exc_type':
+                operror = space.getexecutioncontext().sys_exc_info()
+                if operror is None:
+                    return space.w_None
+                else:
+                    return operror.w_type
+            if attr == 'exc_value':
+                operror = space.getexecutioncontext().sys_exc_info()
+                if operror is None:
+                    return space.w_None
+                else:
+                    return operror.w_value
+            if attr == 'exc_traceback':
+                operror = space.getexecutioncontext().sys_exc_info()
+                if operror is None:
+                    return space.w_None
+                else:
+                    return space.wrap(operror.application_traceback)
+        # produce a nice error message that shows the name of the module
+        try:
+            name = space.str_w(self.w_name)
+        except OperationError:
+            name = '?'
+        msg = "'%s' module has no attribute '%s'" % (name, attr)
+        raise OperationError(space.w_AttributeError, space.wrap(msg))

Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py	(original)
+++ pypy/dist/pypy/interpreter/typedef.py	Mon Jan 24 15:11:27 2005
@@ -219,6 +219,7 @@
     __new__ = interp2app(Module.descr_module__new__.im_func),
     __init__ = interp2app(Module.descr_module__init__.im_func),
     __dict__ = interp_dict_descr,
+    __getattr__ = interp2app(Module.descr_module__getattr__.im_func),
     )
 
 getset_func_doc = GetSetProperty(Function.fget_func_doc,



More information about the Pypy-commit mailing list