[pypy-svn] r59505 - in pypy/trunk/pypy/interpreter: . test

arigo at codespeak.net arigo at codespeak.net
Tue Oct 28 19:12:51 CET 2008


Author: arigo
Date: Tue Oct 28 19:12:51 2008
New Revision: 59505

Modified:
   pypy/trunk/pypy/interpreter/baseobjspace.py
   pypy/trunk/pypy/interpreter/gateway.py
   pypy/trunk/pypy/interpreter/test/test_gateway.py
Log:
Add the unwrap_spec string 'nonnegint'.


Modified: pypy/trunk/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/pypy/interpreter/baseobjspace.py	Tue Oct 28 19:12:51 2008
@@ -986,6 +986,15 @@
         # This is here mostly just for gateway.int_unwrapping_space_method().
         return bool(self.int_w(w_obj))
 
+    def nonnegint_w(self, w_obj):
+        # Like space.int_w(), but raises an app-level ValueError if
+        # the integer is negative.  Mostly here for gateway.py.
+        value = self.int_w(w_obj)
+        if value < 0:
+            raise OperationError(self.w_ValueError,
+                                 self.wrap("expected a non-negative integer"))
+        return value
+
     def warn(self, msg, w_warningcls):
         self.appexec([self.wrap(msg), w_warningcls], """(msg, warningcls):
             import warnings

Modified: pypy/trunk/pypy/interpreter/gateway.py
==============================================================================
--- pypy/trunk/pypy/interpreter/gateway.py	(original)
+++ pypy/trunk/pypy/interpreter/gateway.py	Tue Oct 28 19:12:51 2008
@@ -123,6 +123,9 @@
     def visit_bufferstr(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_nonnegint(self, el, app_sig):
+        self.checked_space_method(el, app_sig)
+
     def visit__Wrappable(self, el, app_sig):
         name = el.__name__
         argname = self.orig_arg()
@@ -224,6 +227,9 @@
     def visit_bufferstr(self, typ):
         self.run_args.append("space.bufferstr_w(%s)" % (self.scopenext(),))
 
+    def visit_nonnegint(self, typ):
+        self.run_args.append("space.nonnegint_w(%s)" % (self.scopenext(),))
+
     def _make_unwrap_activation_class(self, unwrap_spec, cache={}):
         try:
             key = tuple(unwrap_spec)
@@ -339,6 +345,9 @@
     def visit_bufferstr(self, typ):
         self.unwrap.append("space.bufferstr_w(%s)" % (self.nextarg(),))
 
+    def visit_nonnegint(self, typ):
+        self.unwrap.append("space.nonnegint_w(%s)" % (self.nextarg(),))
+
     def make_fastfunc(unwrap_spec, func):
         unwrap_info = UnwrapSpec_FastFunc_Unwrap()
         unwrap_info.apply_over(unwrap_spec)

Modified: pypy/trunk/pypy/interpreter/test/test_gateway.py
==============================================================================
--- pypy/trunk/pypy/interpreter/test/test_gateway.py	(original)
+++ pypy/trunk/pypy/interpreter/test/test_gateway.py	Tue Oct 28 19:12:51 2008
@@ -165,6 +165,21 @@
         assert self.space.eq_w(space.call_function(w_app_g, space.wrap(True)),
                                space.wrap(True))
 
+    def test_interp2app_unwrap_spec_nonnegint(self):
+        space = self.space
+        w = space.wrap
+        def g(space, x):
+            return space.wrap(x * 6)
+        app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace,
+                                                   'nonnegint'])
+        w_app_g = space.wrap(app_g)
+        assert self.space.eq_w(space.call_function(w_app_g, space.wrap(7)),
+                               space.wrap(42))
+        assert self.space.eq_w(space.call_function(w_app_g, space.wrap(0)),
+                               space.wrap(0))
+        space.raises_w(space.w_ValueError,
+                       space.call_function, w_app_g, space.wrap(-1))
+
     def test_interp2app_unwrap_spec_args_w(self):
         space = self.space
         w = space.wrap



More information about the Pypy-commit mailing list