[pypy-commit] cffi default: Kill _cffi_backend.offsetof(), using only _cffi_backend.typeoffsetof().
arigo
noreply at buildbot.pypy.org
Wed Sep 19 01:02:02 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r944:aca9a184c13e
Date: 2012-09-19 00:55 +0200
http://bitbucket.org/cffi/cffi/changeset/aca9a184c13e/
Log: Kill _cffi_backend.offsetof(), using only
_cffi_backend.typeoffsetof().
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -3997,29 +3997,6 @@
return res;
}
-static PyObject *b_offsetof(PyObject *self, PyObject *args)
-{
- PyObject *fieldname;
- CTypeDescrObject *ct;
- CFieldObject *cf;
-
- if (!PyArg_ParseTuple(args, "O!O:offsetof",
- &CTypeDescr_Type, &ct, &fieldname))
- return NULL;
-
- if (!((ct->ct_flags & (CT_STRUCT|CT_UNION)) && ct->ct_stuff != NULL)) {
- PyErr_SetString(PyExc_TypeError,
- "not an initialized struct or union ctype");
- return NULL;
- }
- cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, fieldname);
- if (cf == NULL) {
- PyErr_SetObject(PyExc_KeyError, fieldname);
- return NULL;
- }
- return PyInt_FromSsize_t(cf->cf_offset);
-}
-
static PyObject *b_typeoffsetof(PyObject *self, PyObject *args)
{
PyObject *res, *fieldname;
@@ -4472,7 +4449,6 @@
{"alignof", b_alignof, METH_O},
{"sizeof", b_sizeof, METH_O},
{"typeof", b_typeof, METH_O},
- {"offsetof", b_offsetof, METH_VARARGS},
{"typeoffsetof", b_typeoffsetof, METH_VARARGS},
{"rawaddressof", b_rawaddressof, METH_VARARGS},
{"getcname", b_getcname, METH_VARARGS},
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -745,14 +745,17 @@
assert repr(s.a1).startswith("<cdata 'int[5]' 0x")
def test_offsetof():
+ def offsetof(BType, fieldname):
+ return typeoffsetof(BType, fieldname)[1]
BInt = new_primitive_type("int")
BStruct = new_struct_type("foo")
py.test.raises(TypeError, offsetof, BInt, "abc")
- py.test.raises(TypeError, offsetof, BStruct, "abc")
+ py.test.raises(KeyError, offsetof, BStruct, "abc")
complete_struct_or_union(BStruct, [('abc', BInt, -1), ('def', BInt, -1)])
assert offsetof(BStruct, 'abc') == 0
assert offsetof(BStruct, 'def') == size_of_int()
py.test.raises(KeyError, offsetof, BStruct, "ghi")
+ assert offsetof(new_pointer_type(BStruct), "def") == size_of_int()
def test_function_type():
BInt = new_primitive_type("int")
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -144,7 +144,7 @@
"""
if isinstance(cdecl, str):
cdecl = self._typeof(cdecl)
- return self._backend.offsetof(cdecl, fieldname)
+ return self._backend.typeoffsetof(cdecl, fieldname)[1]
def new(self, cdecl, init=None):
"""Allocate an instance according to the specified C type and
diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -1028,10 +1028,6 @@
assert issubclass(BType, CTypesData)
return BType._alignment()
- def offsetof(self, BType, fieldname):
- assert issubclass(BType, CTypesData)
- return BType._offsetof(fieldname)
-
def newp(self, BType, source):
if not issubclass(BType, CTypesData):
raise TypeError
More information about the pypy-commit
mailing list