summaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2016-10-27 17:21:47 +0200
committerArmin Rigo <arigo@tunes.org>2016-10-27 17:21:47 +0200
commitdcfbf4bf975e66029e6ef00ce36395c75c2f3ee7 (patch)
tree1d690e444a9b53a9615c88e69982ca217ad50acc /c
parent0b175568e2b8afc92ecef2ed88fc8ae0b56c0b83 (diff)
downloadcffi-dcfbf4bf975e66029e6ef00ce36395c75c2f3ee7.tar.gz
Decided to fix ffi.sizeof() too. Update the documentation.
Diffstat (limited to 'c')
-rw-r--r--c/_cffi_backend.c7
-rw-r--r--c/test_c.py3
2 files changed, 8 insertions, 2 deletions
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
index cb584d5..1eb421b 100644
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5492,8 +5492,11 @@ static PyObject *b_sizeof(PyObject *self, PyObject *arg)
if (cd->c_type->ct_flags & CT_ARRAY)
size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size;
- else
- size = cd->c_type->ct_size;
+ else {
+ size = _cdata_var_byte_size(cd);
+ if (size < 0)
+ size = cd->c_type->ct_size;
+ }
}
else if (CTypeDescr_Check(arg)) {
size = ((CTypeDescrObject *)arg)->ct_size;
diff --git a/c/test_c.py b/c/test_c.py
index 951f112..b3ada15 100644
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3202,6 +3202,7 @@ def test_struct_array_no_length():
assert len(p.y) == 3
assert len(p[0].y) == 3
assert len(buffer(p)) == sizeof(BInt) * 4
+ assert sizeof(p[0]) == sizeof(BInt) * 4
plist.append(p)
for i in range(20):
p = plist[i]
@@ -3220,6 +3221,7 @@ def test_struct_array_no_length():
sizeof(BStruct) + 3 * sizeof(BInt),)
assert repr(p[0]) == "<cdata 'foo' owning %d bytes>" % (
sizeof(BStruct) + 3 * sizeof(BInt),)
+ assert sizeof(p[0]) == sizeof(BStruct) + 3 * sizeof(BInt)
#
# from a non-owning pointer, we can't get the length
q = cast(new_pointer_type(BStruct), p)
@@ -3229,6 +3231,7 @@ def test_struct_array_no_length():
py.test.raises(TypeError, len, q[0].y)
assert typeof(q.y) is BIntP
assert typeof(q[0].y) is BIntP
+ assert sizeof(q[0]) == sizeof(BStruct)
#
# error cases
py.test.raises(IndexError, "p.y[4]")