[pypy-svn] r76960 - in pypy/trunk/pypy/rpython/tool: . test

arigo at codespeak.net arigo at codespeak.net
Thu Sep 9 11:36:07 CEST 2010


Author: arigo
Date: Thu Sep  9 11:36:03 2010
New Revision: 76960

Modified:
   pypy/trunk/pypy/rpython/tool/rffi_platform.py
   pypy/trunk/pypy/rpython/tool/test/test_rffi_platform.py
Log:
In rffi_platform, return an integer of some RPython type that fits it:
either int, r_uint, r_longlong or r_ulonglong.


Modified: pypy/trunk/pypy/rpython/tool/rffi_platform.py
==============================================================================
--- pypy/trunk/pypy/rpython/tool/rffi_platform.py	(original)
+++ pypy/trunk/pypy/rpython/tool/rffi_platform.py	Thu Sep  9 11:36:03 2010
@@ -8,6 +8,7 @@
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.platform import CompilationError
 from pypy.tool.udir import udir
+from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong, intmask
 
 # ____________________________________________________________
 #
@@ -371,7 +372,7 @@
         yield '}'
 
     def build_result(self, info, config_result):
-        return info['value']
+        return expose_value_as_rpython(info['value'])
 
 class DefinedConstantInteger(CConfigEntry):
     """An entry in a CConfig class that stands for an externally
@@ -397,7 +398,7 @@
 
     def build_result(self, info, config_result):
         if info["defined"]:
-            return info['value']
+            return expose_value_as_rpython(info['value'])
         return None
 
 class DefinedConstantString(CConfigEntry):
@@ -620,6 +621,20 @@
     raise TypeError("conflicting field type %r for %r" % (fieldtype,
                                                           fieldname))
 
+def expose_value_as_rpython(value):
+    if intmask(value) == value:
+        return value
+    if r_uint(value) == value:
+        return r_uint(value)
+    try:
+        if r_longlong(value) == value:
+            return r_longlong(value)
+    except OverflowError:
+        pass
+    if r_ulonglong(value) == value:
+        return r_ulonglong(value)
+    raise OverflowError("value %d does not fit into any RPython integer type"
+                        % (value,))
 
 C_HEADER = """
 #include <stdio.h>

Modified: pypy/trunk/pypy/rpython/tool/test/test_rffi_platform.py
==============================================================================
--- pypy/trunk/pypy/rpython/tool/test/test_rffi_platform.py	(original)
+++ pypy/trunk/pypy/rpython/tool/test/test_rffi_platform.py	Thu Sep  9 11:36:03 2010
@@ -5,6 +5,7 @@
 from pypy.tool.udir import udir
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 from pypy.translator.platform import platform
+from pypy.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
 
 def import_ctypes():
     try:
@@ -357,3 +358,19 @@
     padding = list(S._hints['padding'])
     d = {'c_c1': 'char'}
     assert S._hints['get_padding_drop'](d) == padding
+
+def test_expose_value_as_rpython():
+    def get(x):
+        x = rffi_platform.expose_value_as_rpython(x)
+        return (x, type(x))
+    assert get(5) == (5, int)
+    assert get(-82) == (-82, int)
+    assert get(sys.maxint) == (sys.maxint, int)
+    assert get(sys.maxint+1) == (sys.maxint+1, r_uint)
+    if sys.maxint == 2147483647:
+        assert get(9999999999) == (9999999999, r_longlong)
+        assert get(-9999999999) == (-9999999999, r_longlong)
+        assert get(2**63) == (2**63, r_ulonglong)
+        assert get(-2**63) == (-2**63, r_longlong)
+    py.test.raises(OverflowError, get, -2**63-1)
+    py.test.raises(OverflowError, get, 2**64)



More information about the Pypy-commit mailing list