[pypy-svn] r75533 - in pypy/branch/fast-forward/pypy/module/__builtin__: . test

benjamin at codespeak.net benjamin at codespeak.net
Wed Jun 23 20:55:09 CEST 2010


Author: benjamin
Date: Wed Jun 23 20:55:07 2010
New Revision: 75533

Modified:
   pypy/branch/fast-forward/pypy/module/__builtin__/interp_classobj.py
   pypy/branch/fast-forward/pypy/module/__builtin__/test/test_classobj.py
Log:
__enter__ and __exit__ as real special methods for old-style classes

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/interp_classobj.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/interp_classobj.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/interp_classobj.py	Wed Jun 23 20:55:07 2010
@@ -662,10 +662,15 @@
         if w_func is not None:
             space.call_function(w_func)
 
+    def descr_exit(self, space, w_type, w_value, w_tb):
+        w_func = self.getattr(space, space.wrap('__exit__'), False)
+        if w_func is not None:
+            return space.call_function(w_func, w_type, w_value, w_tb)
+
 rawdict = {}
 
 # unary operations
-for op in "neg pos abs invert int long float oct hex".split():
+for op in "neg pos abs invert int long float oct hex enter".split():
     specialname = "__%s__" % (op, )
     # fool the gateway logic by giving it a real unbound method
     meth = new.instancemethod(
@@ -755,6 +760,8 @@
     __weakref__ = make_weakref_descr(W_InstanceObject),
     __del__ = interp2app(W_InstanceObject.descr_del,
                          unwrap_spec=['self', ObjSpace]),
+    __exit__ = interp2app(W_InstanceObject.descr_exit,
+                          unwrap_spec=['self', ObjSpace, W_Root, W_Root, W_Root]),
     **rawdict
 )
 

Modified: pypy/branch/fast-forward/pypy/module/__builtin__/test/test_classobj.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/__builtin__/test/test_classobj.py	(original)
+++ pypy/branch/fast-forward/pypy/module/__builtin__/test/test_classobj.py	Wed Jun 23 20:55:07 2010
@@ -767,6 +767,19 @@
         finally:
             warnings.simplefilter('default', RuntimeWarning)
 
+    def test_context_manager(self):
+        class Context:
+            def __enter__(self):
+                self.got_enter = True
+                return 23
+            def __exit__(self, exc, value, tp):
+                self.got_exit = True
+        c = Context()
+        with c as a:
+            assert c.got_enter
+            assert a == 23
+        assert c.got_exit
+
 class AppTestOldStyleSharing(AppTestOldstyle):
     def setup_class(cls):
         cls.space = gettestobjspace(**{"objspace.std.withsharingdict": True})



More information about the Pypy-commit mailing list