Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1772:7a5750bbb806
Date: 2015-04-19 10:45 +0200
http://bitbucket.org/cffi/cffi/changeset/7a5750bbb806/
Log: test and fix for ffi.sizeof()
diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -178,16 +178,25 @@
static PyObject *ffi_sizeof(FFIObject *self, PyObject *arg)
{
+ Py_ssize_t size;
CTypeDescrObject *ct = _ffi_type(self, arg, ACCEPT_ALL);
if (ct == NULL)
return NULL;
- if (ct->ct_size < 0) {
+ size = ct->ct_size;
+
+ if (CData_Check(arg)) {
+ CDataObject *cd = (CDataObject *)arg;
+ if (cd->c_type->ct_flags & CT_ARRAY)
+ size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size;
+ }
+
+ if (size < 0) {
PyErr_Format(FFIError, "don't know the size of ctype '%s'",
ct->ct_name);
return NULL;
}
- return PyInt_FromSsize_t(ct->ct_size);
+ return PyInt_FromSsize_t(size);
}
PyDoc_STRVAR(ffi_alignof_doc,
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
@@ -72,3 +72,13 @@
assert ffi.alignof("int[]") == 4
assert ffi.alignof("int[41]") == 4
assert ffi.alignof("short[41]") == 2
+ assert ffi.alignof(ffi.new("int[41]")) == 4
+ assert ffi.alignof(ffi.new("int[]", 41)) == 4
+
+def test_ffi_sizeof():
+ ffi = _cffi1_backend.FFI()
+ assert ffi.sizeof("int") == 4
+ py.test.raises(ffi.error, ffi.sizeof, "int[]")
+ assert ffi.sizeof("int[41]") == 41 * 4
+ assert ffi.sizeof(ffi.new("int[41]")) == 41 * 4
+ assert ffi.sizeof(ffi.new("int[]", 41)) == 41 * 4
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit