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

arigo at codespeak.net arigo at codespeak.net
Fri Apr 7 16:21:24 CEST 2006


Author: arigo
Date: Fri Apr  7 16:21:23 2006
New Revision: 25502

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

Detect the precise type of externally-declared types (mostly for numeric
types).



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:21:23 2006
@@ -66,11 +66,28 @@
                                                           fieldname))
 
 
+C_HEADER = """
+#include <stdio.h>
+#include <stddef.h>   /* for offsetof() */
+
+void dump(char* key, int value) {
+    printf("%s: %d\\n", key, value);
+}
+"""
+
+def run_example_code(filepath):
+    executable = build_executable([filepath])
+    output = py.process.cmdexec(executable)
+    info = {}
+    for line in output.splitlines():
+        key, value = line.strip().split(': ')
+        info[key] = int(value)
+    return info
+
 def getstruct(name, c_header_source, interesting_fields):
     filepath = uniquefilepath()
     f = filepath.open('w')
-    print >> f, '#include <stdio.h>'
-    print >> f, '#include <stddef.h>   /* for offsetof() */'
+    print >> f, C_HEADER
     print >> f
     print >> f, c_header_source
     print >> f
@@ -80,10 +97,6 @@
     print >> f, '    ctypesplatcheck_t s;'
     print >> f, '} ctypesplatcheck2_t;'
     print >> f
-    print >> f, 'void dump(char* key, int value) {'
-    print >> f, '    printf("%s: %d\\n", key, value);'
-    print >> f, '}'
-    print >> f
     print >> f, 'int main(void) {'
     print >> f, '    ctypesplatcheck_t s;'
     print >> f, '    dump("align", offsetof(ctypesplatcheck2_t, s));'
@@ -103,12 +116,7 @@
     print >> f, '}'
     f.close()
 
-    executable = build_executable([filepath])
-    output = py.process.cmdexec(executable)
-    info = {}
-    for line in output.splitlines():
-        key, value = line.strip().split(': ')
-        info[key] = int(value)
+    info = run_example_code(filepath)
 
     alignment = 1
     layout = [None] * info['size']
@@ -161,6 +169,34 @@
     return S
 
 
+def getsimpletype(name, c_header_source, ctype_hint):
+    filepath = uniquefilepath()
+    f = filepath.open('w')
+    print >> f, C_HEADER
+    print >> f
+    print >> f, c_header_source
+    print >> f
+    print >> f, 'typedef %s ctypesplatcheck_t;' % (name,)
+    print >> f
+    print >> f, 'int main(void) {'
+    print >> f, '    ctypesplatcheck_t x;'
+    print >> f, '    dump("size",  sizeof(ctypesplatcheck_t));'
+    if ctype_hint in integer_class:
+        print >> f, '    x = 0; x = ~x;'
+        print >> f, '    dump("unsigned", x > 0);'
+    print >> f, '    return 0;'
+    print >> f, '}'
+    f.close()
+
+    info = run_example_code(filepath)
+
+    size = info['size']
+    sign = info.get('unsigned', False)
+    if (size, sign) != size_and_sign(ctype_hint):
+        ctype_hint = fixup_ctype(ctype_hint, name, (size, sign))
+    return ctype_hint
+
+
 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:21:23 2006
@@ -83,3 +83,8 @@
     assert fields["f"] == ctypes.c_float
     assert fields["d"] == ctypes.c_double
 
+def test_simple_type():
+    ctype = ctypes_platform.getsimpletype('test_t',
+                                          'typedef unsigned short test_t;',
+                                          ctypes.c_int)
+    assert ctype == ctypes.c_ushort



More information about the Pypy-commit mailing list