Author: Armin Rigo <ar...@tunes.org> Branch: ffi-backend Changeset: r55821:e27df3a673dd Date: 2012-06-25 17:58 +0200 http://bitbucket.org/pypy/pypy/changeset/e27df3a673dd/
Log: test_enum_type diff --git a/pypy/module/_ffi_backend/__init__.py b/pypy/module/_ffi_backend/__init__.py --- a/pypy/module/_ffi_backend/__init__.py +++ b/pypy/module/_ffi_backend/__init__.py @@ -16,6 +16,7 @@ 'new_union_type': 'newtype.new_union_type', 'complete_struct_or_union': 'newtype.complete_struct_or_union', 'new_void_type': 'newtype.new_void_type', + 'new_enum_type': 'newtype.new_enum_type', 'newp': 'func.newp', 'cast': 'func.cast', diff --git a/pypy/module/_ffi_backend/ctypeobj.py b/pypy/module/_ffi_backend/ctypeobj.py --- a/pypy/module/_ffi_backend/ctypeobj.py +++ b/pypy/module/_ffi_backend/ctypeobj.py @@ -459,6 +459,33 @@ misc.write_raw_float_data(cdata, value, self.size) +class W_CTypeEnum(W_CTypePrimitiveSigned): + + def __init__(self, space, name, enumerators, enumvalues): + from pypy.module._ffi_backend.newtype import alignment + name = "enum " + name + size = rffi.sizeof(rffi.INT) + align = alignment(rffi.INT) + W_CTypePrimitiveSigned.__init__(self, space, size, + name, len(name), align) + self.enumerators2values = {} # str -> int + self.enumvalues2erators = {} # int -> str + for i in range(len(enumerators)): + self.enumerators2values[enumerators[i]] = enumvalues[i] + self.enumvalues2erators[enumvalues[i]] = enumerators[i] + + def _getfields(self): + space = self.space + lst = [] + for enumerator in self.enumerators2values: + enumvalue = self.enumerators2values[enumerator] + lst.append(space.newtuple([space.wrap(enumvalue), + space.wrap(enumerator)])) + w_lst = space.newlist(lst) + space.call_method(w_lst, 'sort') + return w_lst + + class W_CTypeStructOrUnion(W_CType): # fields added by complete_struct_or_union(): alignment = -1 diff --git a/pypy/module/_ffi_backend/newtype.py b/pypy/module/_ffi_backend/newtype.py --- a/pypy/module/_ffi_backend/newtype.py +++ b/pypy/module/_ffi_backend/newtype.py @@ -185,3 +185,17 @@ def new_void_type(space): ctype = ctypeobj.W_CTypeVoid(space) return ctype + +# ____________________________________________________________ + +@unwrap_spec(name=str) +def new_enum_type(space, name, w_enumerators, w_enumvalues): + enumerators_w = space.fixedview(w_enumerators) + enumvalues_w = space.fixedview(w_enumvalues) + if len(enumerators_w) != len(enumvalues_w): + raise OperationError(space.w_ValueError, + space.wrap("tuple args must have the same size")) + enumerators = [space.str_w(w) for w in enumerators_w] + enumvalues = [space.int_w(w) for w in enumvalues_w] + ctype = ctypeobj.W_CTypeEnum(space, name, enumerators, enumvalues) + return ctype _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit