[pypy-svn] pypy default: (fijal, arigo)

arigo commits-noreply at bitbucket.org
Wed Jan 19 18:40:42 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r40946:2d76984ee1a0
Date: 2011-01-19 18:39 +0100
http://bitbucket.org/pypy/pypy/changeset/2d76984ee1a0/

Log:	(fijal, arigo)

	Fix(?) gateway.py to always call '__int__' or '__float__' from the
	unwrap_specs of 'int', 'float', etc., as seems to occur in CPython.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1156,38 +1156,66 @@
         # 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)
+    # This is all interface for gateway.py.
+    def gateway_int_w(self, w_obj):
+        if self.is_true(self.isinstance(w_obj, self.w_float)):
+            raise OperationError(self.w_TypeError,
+                            self.wrap("integer argument expected, got float"))
+        return self.int_w(self.int(w_obj))
+
+    def gateway_float_w(self, w_obj):
+        return self.float_w(self.float(w_obj))
+
+    def gateway_r_longlong_w(self, w_obj):
+        if self.is_true(self.isinstance(w_obj, self.w_float)):
+            raise OperationError(self.w_TypeError,
+                            self.wrap("integer argument expected, got float"))
+        return self.r_longlong_w(self.int(w_obj))
+
+    def gateway_r_uint_w(self, w_obj):
+        if self.is_true(self.isinstance(w_obj, self.w_float)):
+            raise OperationError(self.w_TypeError,
+                            self.wrap("integer argument expected, got float"))
+        return self.uint_w(self.int(w_obj))
+
+    def gateway_r_ulonglong_w(self, w_obj):
+        if self.is_true(self.isinstance(w_obj, self.w_float)):
+            raise OperationError(self.w_TypeError,
+                            self.wrap("integer argument expected, got float"))
+        return self.r_ulonglong_w(self.int(w_obj))
+
+    def gateway_nonnegint_w(self, w_obj):
+        # Like space.gateway_int_w(), but raises an app-level ValueError if
+        # the integer is negative.  Here for gateway.py.
+        value = self.gateway_int_w(w_obj)
         if value < 0:
             raise OperationError(self.w_ValueError,
                                  self.wrap("expected a non-negative integer"))
         return value
 
     def c_int_w(self, w_obj):
-        # Like space.int_w(), but raises an app-level OverflowError if
-        # the integer does not fit in 32 bits.  Mostly here for gateway.py.
-        value = self.int_w(w_obj)
+        # Like space.gateway_int_w(), but raises an app-level OverflowError if
+        # the integer does not fit in 32 bits.  Here for gateway.py.
+        value = self.gateway_int_w(w_obj)
         if value < -2147483647-1 or value > 2147483647:
             raise OperationError(self.w_OverflowError,
                                  self.wrap("expected a 32-bit integer"))
         return value
 
     def c_uint_w(self, w_obj):
-        # Like space.uint_w(), but raises an app-level OverflowError if
-        # the integer does not fit in 32 bits.  Mostly here for gateway.py.
-        value = self.uint_w(w_obj)
+        # Like space.gateway_uint_w(), but raises an app-level OverflowError if
+        # the integer does not fit in 32 bits.  Here for gateway.py.
+        value = self.gateway_r_uint_w(w_obj)
         if value > UINT_MAX_32_BITS:
             raise OperationError(self.w_OverflowError,
                               self.wrap("expected an unsigned 32-bit integer"))
         return value
 
     def c_nonnegint_w(self, w_obj):
-        # Like space.int_w(), but raises an app-level ValueError if
-        # the integer is negative or does not fit in 32 bits.  Mostly here
+        # Like space.gateway_int_w(), but raises an app-level ValueError if
+        # the integer is negative or does not fit in 32 bits.  Here
         # for gateway.py.
-        value = self.int_w(w_obj)
+        value = self.gateway_int_w(w_obj)
         if value < 0:
             raise OperationError(self.w_ValueError,
                                  self.wrap("expected a non-negative integer"))
@@ -1197,6 +1225,10 @@
         return value
 
     def c_filedescriptor_w(self, w_fd):
+        # This is only used sometimes in CPython, e.g. for os.fsync() but
+        # not os.close().  It's likely designed for 'select'.  It's irregular
+        # in the sense that it expects either a real int/long or an object
+        # with a fileno(), but not an object with an __int__().
         if (not self.isinstance_w(w_fd, self.w_int) and
             not self.isinstance_w(w_fd, self.w_long)):
             try:

diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -245,7 +245,8 @@
         self.run_args.append("space.str_or_None_w(%s)" % (self.scopenext(),))
 
     def visit_nonnegint(self, typ):
-        self.run_args.append("space.nonnegint_w(%s)" % (self.scopenext(),))
+        self.run_args.append("space.gateway_nonnegint_w(%s)" % (
+            self.scopenext(),))
 
     def visit_c_int(self, typ):
         self.run_args.append("space.c_int_w(%s)" % (self.scopenext(),))
@@ -375,7 +376,7 @@
         self.unwrap.append("space.str_or_None_w(%s)" % (self.nextarg(),))
 
     def visit_nonnegint(self, typ):
-        self.unwrap.append("space.nonnegint_w(%s)" % (self.nextarg(),))
+        self.unwrap.append("space.gateway_nonnegint_w(%s)" % (self.nextarg(),))
 
     def visit_c_int(self, typ):
         self.unwrap.append("space.c_int_w(%s)" % (self.nextarg(),))
@@ -418,11 +419,11 @@
 def int_unwrapping_space_method(typ):
     assert typ in (int, str, float, unicode, r_longlong, r_uint, r_ulonglong, bool)
     if typ is r_int is r_longlong:
-        return 'r_longlong_w'
-    elif typ is r_uint:
-        return 'uint_w'
+        return 'gateway_r_longlong_w'
+    elif typ in (str, unicode, bool):
+        return typ.__name__ + '_w'
     else:
-        return typ.__name__ + '_w'
+        return 'gateway_' + typ.__name__ + '_w'
 
 
 def unwrap_spec(*spec):


More information about the Pypy-commit mailing list