[pypy-commit] cffi cffi-1.0: ffi.alignof()

arigo noreply at buildbot.pypy.org
Sun Apr 19 10:45:27 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1771:1ae2580ab916
Date: 2015-04-19 10:40 +0200
http://bitbucket.org/cffi/cffi/changeset/1ae2580ab916/

Log:	ffi.alignof()

diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -190,6 +190,23 @@
     return PyInt_FromSsize_t(ct->ct_size);
 }
 
+PyDoc_STRVAR(ffi_alignof_doc,
+"Return the natural alignment size in bytes of the argument.\n"
+"It can be a string naming a C type, or a 'cdata' instance.");
+
+static PyObject *ffi_alignof(FFIObject *self, PyObject *arg)
+{
+    int align;
+    CTypeDescrObject *ct = _ffi_type(self, arg, ACCEPT_ALL);
+    if (ct == NULL)
+        return NULL;
+
+    align = get_alignment(ct);
+    if (align < 0)
+        return NULL;
+    return PyInt_FromLong(align);
+}
+
 PyDoc_STRVAR(ffi_typeof_doc,
 "Parse the C type given as a string and return the\n"
 "corresponding <ctype> object.\n"
@@ -526,6 +543,7 @@
 #if 0
     {"addressof",     (PyCFunction)ffi_addressof, METH_VARARGS},
 #endif
+    {"alignof",       (PyCFunction)ffi_alignof,   METH_O,      ffi_alignof_doc},
     {"cast",          (PyCFunction)ffi_cast,      METH_VARARGS, ffi_cast_doc},
 #if 0
     {"close_library", ffi_close_library,          METH_VARARGS | METH_STATIC},
diff --git a/new/test_ffi_obj.py b/new/test_ffi_obj.py
--- a/new/test_ffi_obj.py
+++ b/new/test_ffi_obj.py
@@ -65,3 +65,10 @@
     ffi = _cffi1_backend.FFI()
     ffi.errno = 42
     assert ffi.errno == 42
+
+def test_ffi_alignof():
+    ffi = _cffi1_backend.FFI()
+    assert ffi.alignof("int") == 4
+    assert ffi.alignof("int[]") == 4
+    assert ffi.alignof("int[41]") == 4
+    assert ffi.alignof("short[41]") == 2


More information about the pypy-commit mailing list