Commits

Armin Rigo committed 4631971

module objects working

  • Participants
  • Parent commits b5fde29

Comments (0)

Files changed (4)

pypy/objspace/std/moduleobject.py

     if space.is_true(space.eq(w_attr, space.wrap('__dict__'))):
         return w_module.w_dict
     else:
-        return space.getitem(w_module.w_dict, w_attr)
+        try:
+            return space.getitem(w_module.w_dict, w_attr)
+        except OperationError, e:
+            if e.match(space, space.w_KeyError):
+                raise FailedToImplement(space.w_AttributeError)
+            else:
+                raise
 
 def setattr_dict_any_any(space, w_module, w_attr, w_value):
     if space.is_true(space.eq(w_attr, space.wrap('__dict__'))):
         raise OperationError(space.w_TypeError,
                              space.wrap("readonly attribute"))
     else:
-        space.delitem(w_module.w_dict, w_attr)
+        try:
+            space.delitem(w_module.w_dict, w_attr)
+        except OperationError, e:
+            if e.match(space, space.w_KeyError):
+                raise FailedToImplement(space.w_AttributeError)
+            else:
+                raise
 
 StdObjSpace.getattr.register(getattr_dict_any, W_ModuleObject, W_ANY)
 StdObjSpace.setattr.register(setattr_dict_any_any, W_ModuleObject, W_ANY, W_ANY)

pypy/objspace/std/objspace.py

 for _name, _symbol, _arity in ObjSpace.MethodTable:
     setattr(StdObjSpace, _name, MultiMethod(_symbol, _arity))
 
+# default implementations of some multimethods for all objects
+# that don't explicitely override them or that raise FailedToImplement
+
 def default_eq(space, w_a, w_b):
     return space.is_(w_a, w_b)
 
     return True   # everything is True unless otherwise specified
 
 StdObjSpace.is_true.register(default_is_true, W_ANY)
+
+def default_getattr(space, w_obj, w_attr):
+    # XXX build a nicer error message along these lines:
+    #w_type = space.type(w_obj)
+    #w_typename = space.getattr(w_type, space.wrap('__name__'))
+    #...
+    raise OperationError(space.w_AttributeError, w_attr)
+
+StdObjSpace.getattr.register(default_getattr, W_ANY, W_ANY)
+
+def default_setattr(space, w_obj, w_attr, w_value):
+    raise OperationError(space.w_AttributeError, w_attr)
+
+StdObjSpace.setattr.register(default_getattr, W_ANY, W_ANY, W_ANY)
+
+def default_delattr(space, w_obj, w_attr, w_value):
+    raise OperationError(space.w_AttributeError, w_attr)
+
+StdObjSpace.delattr.register(default_getattr, W_ANY, W_ANY)

pypy/objspace/std/test/objspace.py

-import new
-
-class FailedToImplement(Exception):
-    pass
-
-class OperationError(Exception):
-    pass
-
-class FakeRegister:
-    def register(self,*argl,**argv):
-        pass
-
-class CallWrapper:
-    def __init__(self,module):
-        self._module = module
-
-    def call(self,space,methodname,attributes):
-        return getattr(self._module,methodname)(*attributes)
-class W_NoneObject:pass
-
-class W_BoolObject:pass
-
-class WrapClass:
-    def __init__(self,value):
-        self.value = value
-
-class ObjSpace:
-    add = FakeRegister()
-    sub = FakeRegister()
-    mul = FakeRegister()
-    pow = FakeRegister()
-    pos = FakeRegister()
-    neg = FakeRegister()
-    not_ = FakeRegister()
-    invert = FakeRegister()
-    truediv = FakeRegister()
-    floordiv = FakeRegister()
-    div = FakeRegister()
-    mod = FakeRegister()
-    lshift = FakeRegister()
-    rshift = FakeRegister()
-    and_ = FakeRegister()
-    xor = FakeRegister()
-    or_ = FakeRegister()
-    oct = FakeRegister()
-    hex = FakeRegister()
-    ord = FakeRegister()
-    float = FakeRegister()
-    repr = FakeRegister()
-    str = FakeRegister()
-    compare = FakeRegister()
-    hash = FakeRegister()
-    divmod = FakeRegister()
-    abs = FakeRegister()
-    nonzero = FakeRegister()
-    coerce = FakeRegister()
-    int = FakeRegister()
-    long = FakeRegister()
-    float = FakeRegister()
-
-    w_TypeError = "w_TypeError"
-    w_ValueError = "w_ValueError"
-    w_OverflowError = "w_OverflowError"
-    w_ZeroDivisionError = "w_ZeroDivisionError"
-
-    def wrap(self,item):
-        return WrapClass(item)
-
-    def unwrap(self,item):
-        return item.value
-
-    def AppFile(self,name):
-        thismod = new.module(name+'_app')
-        thisglobals = {}
-        thislocals = {}
-        try:
-            execfile(name+'_app.py',thismod.__dict__)
-        except:
-            try:
-                execfile(name+'-app.py',thismod.__dict__)
-            except IOError:
-                execfile(name+'.app.py',thismod.__dict__)
-        #namespace = thislocals.update(thisglobals)
-        ret = CallWrapper(thismod)
-        return ret
-
-    def newtuple(self, tuplelist):
-        return self.wrap(tuple(tuplelist))
-
-    def newdouble(self, thisdouble):
-        return self.wrap(thisdouble)
-
-    def newlong(self, thislong):
-        return self.wrap(thislong)
-
-    def newbool(self, thisbool):
-        return self.wrap(thisbool)
-
-StdObjSpace = ObjSpace()
-
-if __name__ == '__main__':
-    space = ObjSpace()
-    handle = space.applicationfile('test')
-    handle.call(space,'test',[2])

pypy/objspace/std/test/testsupport.py

         return self.failIf(condition, msg)
 
     def assertRaises_w(self, w_exc_class, callable, *args, **kw):
+        from pypy.objspace.std.objspace import OperationError
         try:
             callable(*args, **kw)
         except OperationError, e: