[pypy-svn] r45704 - in pypy/branch/pypy-more-rtti-inprogress/rpython/tool: . test
fijal at codespeak.net
fijal at codespeak.net
Thu Aug 16 12:52:35 CEST 2007
Author: fijal
Date: Thu Aug 16 12:52:35 2007
New Revision: 45704
Added:
pypy/branch/pypy-more-rtti-inprogress/rpython/tool/test/test_rffi_platform.py
- copied, changed from r45676, pypy/branch/pypy-more-rtti-inprogress/rpython/rctypes/tool/test/test_ctypes_platform.py
Modified:
pypy/branch/pypy-more-rtti-inprogress/rpython/tool/rffi_platform.py
Log:
First test passes.
Most of functions in rffi_platform.py are disabled by now and
will be re-enabled as we'll have more tests passing.
Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/tool/rffi_platform.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/tool/rffi_platform.py (original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/tool/rffi_platform.py Thu Aug 16 12:52:35 2007
@@ -1,7 +1,9 @@
#! /usr/bin/env python
import os, py, sys
-import ctypes
+from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.lltypesystem import rffi
+from pypy.rpython.lltypesystem import llmemory
from pypy.translator.tool.cbuild import build_executable
from pypy.tool.udir import udir
@@ -15,7 +17,7 @@
STRUCT = Struct(name, interesting_fields)
return configure(CConfig)['STRUCT']
-def getsimpletype(name, c_header_source, ctype_hint=ctypes.c_int):
+def getsimpletype(name, c_header_source, ctype_hint=rffi.INT):
class CConfig:
_header_ = c_header_source
TYPE = SimpleType(name, ctype_hint)
@@ -129,19 +131,19 @@
def prepare_code(self):
if self.ifdef is not None:
yield '#ifdef %s' % (self.ifdef,)
- yield 'typedef %s ctypesplatcheck_t;' % (self.name,)
+ yield 'typedef %s platcheck_t;' % (self.name,)
yield 'typedef struct {'
yield ' char c;'
- yield ' ctypesplatcheck_t s;'
- yield '} ctypesplatcheck2_t;'
+ yield ' platcheck_t s;'
+ yield '} platcheck2_t;'
yield ''
- yield 'ctypesplatcheck_t s;'
+ yield 'platcheck_t s;'
if self.ifdef is not None:
yield 'dump("defined", 1);'
- yield 'dump("align", offsetof(ctypesplatcheck2_t, s));'
- yield 'dump("size", sizeof(ctypesplatcheck_t));'
+ yield 'dump("align", offsetof(platcheck2_t, s));'
+ yield 'dump("size", sizeof(platcheck_t));'
for fieldname, fieldtype in self.interesting_fields:
- yield 'dump("fldofs %s", offsetof(ctypesplatcheck_t, %s));'%(
+ yield 'dump("fldofs %s", offsetof(platcheck_t, %s));'%(
fieldname, fieldname)
yield 'dump("fldsize %s", sizeof(s.%s));' % (
fieldname, fieldname)
@@ -160,7 +162,6 @@
if self.ifdef is not None:
if not info['defined']:
return None
- alignment = 1
layout = [None] * info['size']
for fieldname, fieldtype in self.interesting_fields:
if isinstance(fieldtype, Struct):
@@ -168,7 +169,6 @@
size = info['fldsize ' + fieldname]
c_fieldtype = config_result.get_entry_result(fieldtype)
layout_addfield(layout, offset, c_fieldtype, fieldname)
- alignment = max(alignment, ctype_alignment(c_fieldtype))
else:
offset = info['fldofs ' + fieldname]
size = info['fldsize ' + fieldname]
@@ -176,33 +176,15 @@
if (size, sign) != size_and_sign(fieldtype):
fieldtype = fixup_ctype(fieldtype, fieldname, (size, sign))
layout_addfield(layout, offset, fieldtype, fieldname)
- alignment = max(alignment, ctype_alignment(fieldtype))
-
- # try to enforce the same alignment as the one of the original
- # structure
- if alignment < info['align']:
- choices = [ctype for ctype in alignment_types
- if ctype_alignment(ctype) == info['align']]
- assert choices, "unsupported alignment %d" % (info['align'],)
- choices = [(ctypes.sizeof(ctype), i, ctype)
- for i, ctype in enumerate(choices)]
- csize, _, ctype = min(choices)
- for i in range(0, info['size'] - csize + 1, info['align']):
- if layout[i:i+csize] == [None] * csize:
- layout_addfield(layout, i, ctype, '_alignment')
- break
- else:
- raise AssertionError("unenforceable alignment %d" % (
- info['align'],))
n = 0
for i, cell in enumerate(layout):
if cell is not None:
continue
- layout_addfield(layout, i, ctypes.c_char, '_pad%d' % (n,))
+ layout_addfield(layout, i, rffi.UCHAR, '_pad%d' % (n,))
n += 1
- # build the ctypes Structure
+ # build the lltype Structure
seen = {}
fields = []
for cell in layout:
@@ -211,20 +193,18 @@
fields.append((cell.name, cell.ctype))
seen[cell] = True
- class S(ctypes.Structure):
- _fields_ = fields
name = self.name
if name.startswith('struct '):
name = name[7:]
- S.__name__ = name
- return S
-
+ kwds = {'hints':{'align':info['align'],
+ 'size':info['size']}}
+ return rffi.CStruct(name, *fields, **kwds).TO
class SimpleType(CConfigEntry):
"""An entry in a CConfig class that stands for an externally
defined simple numeric type.
"""
- def __init__(self, name, ctype_hint=ctypes.c_int, ifdef=None):
+ def __init__(self, name, ctype_hint=rffi.INT, ifdef=None):
self.name = name
self.ctype_hint = ctype_hint
self.ifdef = ifdef
@@ -232,12 +212,12 @@
def prepare_code(self):
if self.ifdef is not None:
yield '#ifdef %s' % (self.ifdef,)
- yield 'typedef %s ctypesplatcheck_t;' % (self.name,)
+ yield 'typedef %s platcheck_t;' % (self.name,)
yield ''
- yield 'ctypesplatcheck_t x;'
+ yield 'platcheck_t x;'
if self.ifdef is not None:
yield 'dump("defined", 1);'
- yield 'dump("size", sizeof(ctypesplatcheck_t));'
+ yield 'dump("size", sizeof(platcheck_t));'
if self.ctype_hint in integer_class:
yield 'x = 0; x = ~x;'
yield 'dump("unsigned", x > 0);'
@@ -354,9 +334,10 @@
class Library(CConfigEntry):
- """The loaded CTypes library object.
+ """The loaded library object.
"""
def __init__(self, name):
+ XXX # uh
self.name = name
def prepare_code(self):
@@ -382,6 +363,7 @@
# internal helpers
def ctype_alignment(c_type):
+ xxxx
if issubclass(c_type, ctypes.Structure):
return max([ctype_alignment(fld_type)
for fld_name, fld_type in c_type._fields_])
@@ -391,28 +373,16 @@
def uniquefilepath(LAST=[0]):
i = LAST[0]
LAST[0] += 1
- return udir.join('ctypesplatcheck_%d.c' % i)
+ return udir.join('platcheck_%d.c' % i)
-alignment_types = [
- ctypes.c_short,
- ctypes.c_int,
- ctypes.c_long,
- ctypes.c_float,
- ctypes.c_double,
- ctypes.c_char_p,
- ctypes.c_void_p,
- ctypes.c_longlong,
- ctypes.c_wchar,
- ctypes.c_wchar_p,
- ]
-
-integer_class = [ctypes.c_byte, ctypes.c_ubyte,
- ctypes.c_short, ctypes.c_ushort,
- ctypes.c_int, ctypes.c_uint,
- ctypes.c_long, ctypes.c_ulong,
- ctypes.c_longlong, ctypes.c_ulonglong,
- ]
-float_class = [ctypes.c_float, ctypes.c_double]
+integer_class = [rffi.SIGNEDCHAR, rffi.CHAR,
+ rffi.SHORT, rffi.USHORT,
+ rffi.INT, rffi.UINT,
+ rffi.LONG, rffi.ULONG,
+ rffi.LONGLONG, rffi.ULONGLONG]
+# XXX SIZE_T?
+
+float_class = [rffi.DOUBLE]
class Field(object):
def __init__(self, name, ctype):
@@ -422,7 +392,7 @@
return '<field %s: %s>' % (self.name, self.ctype)
def layout_addfield(layout, offset, ctype, prefix):
- size = ctypes.sizeof(ctype)
+ size = _sizeof(ctype)
name = prefix
i = 0
while name in layout:
@@ -434,9 +404,16 @@
layout[i] = field
return field
+def _sizeof(ctype):
+ return ctype._type.BITS / 8
+
def size_and_sign(ctype):
- return (ctypes.sizeof(ctype),
- ctype in integer_class and ctype(-1).value > 0)
+ # XXX uh, dirty hack
+ if ctype is lltype.Char:
+ return 1, 1
+ assert isinstance(ctype, lltype.Number)
+ r_type = ctype._type
+ return _sizeof(ctype), not r_type.SIGNED
def fixup_ctype(fieldtype, fieldname, expected_size_and_sign):
for typeclass in [integer_class, float_class]:
@@ -444,12 +421,13 @@
for ctype in typeclass:
if size_and_sign(ctype) == expected_size_and_sign:
return ctype
+ xxxx
if (hasattr(fieldtype, '_length_')
- and getattr(fieldtype, '_type_', None) == ctypes.c_char):
+ and getattr(fieldtype, '_type_', None) == rffi.UCHAR):
# for now, assume it is an array of chars; otherwise we'd also
# have to check the exact integer type of the elements of the array
size, sign = expected_size_and_sign
- return ctypes.c_char * size
+ return rffi.UCHAR * size
if (hasattr(fieldtype, '_length_')
and getattr(fieldtype, '_type_', None) == ctypes.c_ubyte):
# grumble, fields of type 'c_char array' have automatic cast-to-
@@ -497,9 +475,9 @@
if __name__ == '__main__':
doc = """Example:
- ctypes_platform.py -h sys/types.h -h netinet/in.h
+ rffi_platform.py -h sys/types.h -h netinet/in.h
'struct sockaddr_in'
- sin_port c_int
+ sin_port INT
"""
import sys, getopt
opts, args = getopt.gnu_getopt(sys.argv[1:], 'h:')
@@ -514,7 +492,7 @@
name = args[0]
fields = []
for i in range(1, len(args), 2):
- ctype = getattr(ctypes, args[i+1])
+ ctype = getattr(rffi, args[i+1])
fields.append((args[i], ctype))
S = getstruct(name, '\n'.join(headers), fields)
More information about the Pypy-commit
mailing list