[pypy-svn] r7326 - in pypy/trunk/src/pypy/objspace/std: . test
bob at codespeak.net
bob at codespeak.net
Wed Nov 17 15:48:30 CET 2004
Author: bob
Date: Wed Nov 17 15:48:30 2004
New Revision: 7326
Added:
pypy/trunk/src/pypy/objspace/std/dictproxyobject.py
pypy/trunk/src/pypy/objspace/std/dictproxytype.py
pypy/trunk/src/pypy/objspace/std/test/test_dictproxy.py
Modified:
pypy/trunk/src/pypy/objspace/std/typetype.py
Log:
dictproxy
Added: pypy/trunk/src/pypy/objspace/std/dictproxyobject.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/dictproxyobject.py Wed Nov 17 15:48:30 2004
@@ -0,0 +1,19 @@
+from pypy.objspace.std.objspace import *
+from pypy.interpreter.typedef import GetSetProperty
+
+def descr_get_dictproxy(space, w_obj):
+ obj = space.unwrap_builtin(w_obj)
+ return W_DictProxyObject(space, obj.getdict())
+
+dictproxy_descr = GetSetProperty(descr_get_dictproxy)
+
+class W_DictProxyObject(W_Object):
+ from pypy.objspace.std.dictproxytype import dictproxy_typedef as typedef
+
+ def __init__(w_self, space, w_dict):
+ W_Object.__init__(w_self, space)
+ w_self.w_dict = w_dict
+
+registerimplementation(W_DictProxyObject)
+
+register_all(vars())
Added: pypy/trunk/src/pypy/objspace/std/dictproxytype.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/dictproxytype.py Wed Nov 17 15:48:30 2004
@@ -0,0 +1,38 @@
+from pypy.objspace.std.stdtypedef import *
+from pypy.objspace.std.register_all import register_all
+from pypy.interpreter.error import OperationError
+
+# ____________________________________________________________
+
+def proxymethod(name):
+ def fget(space, w_obj):
+ obj = space.unwrap_builtin(w_obj)
+ return space.getattr(obj.w_dict, space.wrap(name))
+ return GetSetProperty(fget)
+
+# ____________________________________________________________
+
+dictproxy_typedef = StdTypeDef("dictproxy",
+ has_key = proxymethod('has_key'),
+ get = proxymethod('get'),
+ keys = proxymethod('keys'),
+ values = proxymethod('values'),
+ items = proxymethod('items'),
+ iterkeys = proxymethod('iterkeys'),
+ itervalues = proxymethod('itervalues'),
+ iteritems = proxymethod('iteritems'),
+ copy = proxymethod('copy'),
+ __len__ = proxymethod('__len__'),
+ __getitem__ = proxymethod('__getitem__'),
+ __contains__ = proxymethod('__contains__'),
+ __str__ = proxymethod('__str__'),
+ __iter__ = proxymethod('__iter__'),
+ __cmp__ = proxymethod('__cmp__'),
+ __lt__ = proxymethod('__lt__'),
+ __le__ = proxymethod('__le__'),
+ __eq__ = proxymethod('__eq__'),
+ __ne__ = proxymethod('__ne__'),
+ __gt__ = proxymethod('__gt__'),
+ __ge__ = proxymethod('__ge__'),
+)
+dictproxy_typedef.registermethods(globals())
Added: pypy/trunk/src/pypy/objspace/std/test/test_dictproxy.py
==============================================================================
--- (empty file)
+++ pypy/trunk/src/pypy/objspace/std/test/test_dictproxy.py Wed Nov 17 15:48:30 2004
@@ -0,0 +1,25 @@
+import autopath
+from pypy.tool import testit
+
+class TestUserObject(testit.AppTestCase):
+ def setUp(self):
+ self.space = testit.objspace('std')
+
+ def test_dictproxy(self):
+ class NotEmpty:
+ a = 1
+ self.assertEquals(isinstance(NotEmpty.__dict__, dict), False)
+ self.assert_('a' in NotEmpty.__dict__)
+ self.assert_('a' in NotEmpty.__dict__.keys())
+ self.assert_('b' not in NotEmpty.__dict__)
+ self.assert_(isinstance(NotEmpty.__dict__.copy(), dict))
+ self.assert_(NotEmpty.__dict__ == NotEmpty.__dict__.copy())
+ try:
+ NotEmpty.__dict__['b'] = 1
+ except:
+ pass
+ else:
+ raise AssertionError, 'this should not have been writable'
+
+if __name__ == '__main__':
+ testit.main()
Modified: pypy/trunk/src/pypy/objspace/std/typetype.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/typetype.py (original)
+++ pypy/trunk/src/pypy/objspace/std/typetype.py Wed Nov 17 15:48:30 2004
@@ -1,5 +1,5 @@
from pypy.objspace.std.stdtypedef import *
-from pypy.interpreter.typedef import default_dict_descr
+from pypy.objspace.std.dictproxyobject import dictproxy_descr
def descr__new__(space, w_typetype, w_name, w_bases, w_dict):
@@ -46,6 +46,6 @@
__bases__ = GetSetProperty(descr__bases),
__base__ = GetSetProperty(descr__base),
__mro__ = GetSetProperty(descr_get__mro__),
- __dict__ = default_dict_descr,
+ __dict__ = dictproxy_descr,
mro = newmethod(descr_mro),
)
More information about the Pypy-commit
mailing list