[pypy-svn] r25504 - in pypy/dist/pypy/rpython/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Fri Apr 7 16:38:02 CEST 2006


Author: arigo
Date: Fri Apr  7 16:38:01 2006
New Revision: 25504

Modified:
   pypy/dist/pypy/rpython/rctypes/ctypes_platform.py
   pypy/dist/pypy/rpython/rctypes/test/test_ctypes_platform.py
Log:
(arre, arigo)

Retrieve the value of integer constants.


Modified: pypy/dist/pypy/rpython/rctypes/ctypes_platform.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/ctypes_platform.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/ctypes_platform.py	Fri Apr  7 16:38:01 2006
@@ -197,6 +197,31 @@
     return ctype_hint
 
 
+def getconstantinteger(name, c_header_source):
+    filepath = uniquefilepath()
+    f = filepath.open('w')
+    print >> f, C_HEADER
+    print >> f
+    print >> f, c_header_source
+    print >> f
+    print >> f, 'int main(void) {'
+    print >> f, '    if ((%s) < 0) {' % (name,)
+    print >> f, '        long long x = (long long)(%s);' % (name,)
+    print >> f, '        printf("value: %lld\\n", x);'
+    print >> f, '    } else {'
+    print >> f, '        unsigned long long x = (unsigned long long)(%s);' % (
+                            name,)
+    print >> f, '        printf("value: %llu\\n", x);'
+    print >> f, '    }'
+    print >> f, '    return 0;'
+    print >> f, '}'
+    f.close()
+
+    info = run_example_code(filepath)
+
+    return info['value']
+
+
 if __name__ == '__main__':
     doc = """Example:
     

Modified: pypy/dist/pypy/rpython/rctypes/test/test_ctypes_platform.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_ctypes_platform.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_ctypes_platform.py	Fri Apr  7 16:38:01 2006
@@ -88,3 +88,14 @@
                                           'typedef unsigned short test_t;',
                                           ctypes.c_int)
     assert ctype == ctypes.c_ushort
+
+def test_constant_integer():
+    value = ctypes_platform.getconstantinteger('BLAH',
+                                               '#define BLAH (6*7)')
+    assert value == 42
+    value = ctypes_platform.getconstantinteger('BLAH',
+                                               '#define BLAH (-2147483648LL)')
+    assert value == -2147483648
+    value = ctypes_platform.getconstantinteger('BLAH',
+                                               '#define BLAH (3333333333ULL)')
+    assert value == 3333333333



More information about the Pypy-commit mailing list