[pypy-commit] cffi default: One more case, this time in CompiledFFI.sizeof().
arigo
pypy.commits at gmail.com
Thu Oct 27 18:06:13 EDT 2016
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r2801:4dd053d0aa06
Date: 2016-10-28 00:05 +0200
http://bitbucket.org/cffi/cffi/changeset/4dd053d0aa06/
Log: One more case, this time in CompiledFFI.sizeof().
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5483,20 +5483,25 @@
return PyInt_FromLong(align);
}
+static Py_ssize_t direct_sizeof_cdata(CDataObject *cd)
+{
+ Py_ssize_t size;
+ if (cd->c_type->ct_flags & CT_ARRAY)
+ size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size;
+ else {
+ size = _cdata_var_byte_size(cd);
+ if (size < 0)
+ size = cd->c_type->ct_size;
+ }
+ return size;
+}
+
static PyObject *b_sizeof(PyObject *self, PyObject *arg)
{
Py_ssize_t 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;
- else {
- size = _cdata_var_byte_size(cd);
- if (size < 0)
- size = cd->c_type->ct_size;
- }
+ size = direct_sizeof_cdata((CDataObject *)arg);
}
else if (CTypeDescr_Check(arg)) {
size = ((CTypeDescrObject *)arg)->ct_size;
diff --git a/c/ffi_obj.c b/c/ffi_obj.c
--- a/c/ffi_obj.c
+++ b/c/ffi_obj.c
@@ -257,22 +257,20 @@
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;
-
- 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;
+ size = direct_sizeof_cdata((CDataObject *)arg);
}
-
- if (size < 0) {
- PyErr_Format(FFIError, "don't know the size of ctype '%s'",
- ct->ct_name);
- return NULL;
+ else {
+ CTypeDescrObject *ct = _ffi_type(self, arg, ACCEPT_ALL);
+ if (ct == NULL)
+ return NULL;
+ size = ct->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(size);
}
diff --git a/testing/cffi0/test_verify.py b/testing/cffi0/test_verify.py
--- a/testing/cffi0/test_verify.py
+++ b/testing/cffi0/test_verify.py
@@ -594,7 +594,7 @@
assert ffi.sizeof(ffi.typeof(s[0])) == 1 * ffi.sizeof('int')
assert ffi.sizeof(s[0]) == 5 * ffi.sizeof('int')
# ^^^ explanation: if you write in C: "char x[5];", then
- # "sizeof(ax" will evaluate to 5. The behavior above is
+ # "sizeof(x)" will evaluate to 5. The behavior above is
# a generalization of that to "struct foo_s[len(a)=5] x;"
# if you could do that in C.
assert s.a[3] == 0
diff --git a/testing/cffi1/test_verify1.py b/testing/cffi1/test_verify1.py
--- a/testing/cffi1/test_verify1.py
+++ b/testing/cffi1/test_verify1.py
@@ -576,10 +576,15 @@
ffi.verify("struct foo_s { int x; int a[]; };")
assert ffi.sizeof('struct foo_s') == 1 * ffi.sizeof('int')
s = ffi.new("struct foo_s *", [424242, 4])
- assert ffi.sizeof(s[0]) == 1 * ffi.sizeof('int') # the same in C
+ assert ffi.sizeof(ffi.typeof(s[0])) == 1 * ffi.sizeof('int')
+ assert ffi.sizeof(s[0]) == 5 * ffi.sizeof('int')
+ # ^^^ explanation: if you write in C: "char x[5];", then
+ # "sizeof(x)" will evaluate to 5. The behavior above is
+ # a generalization of that to "struct foo_s[len(a)=5] x;"
+ # if you could do that in C.
assert s.a[3] == 0
s = ffi.new("struct foo_s *", [424242, [-40, -30, -20, -10]])
- assert ffi.sizeof(s[0]) == 1 * ffi.sizeof('int')
+ assert ffi.sizeof(s[0]) == 5 * ffi.sizeof('int')
assert s.a[3] == -10
s = ffi.new("struct foo_s *")
assert ffi.sizeof(s[0]) == 1 * ffi.sizeof('int')
@@ -594,10 +599,10 @@
ffi.verify("struct foo_s { int x, y; int a[]; };")
assert ffi.sizeof('struct foo_s') == 2 * ffi.sizeof('int')
s = ffi.new("struct foo_s *", [424242, 4])
- assert ffi.sizeof(s[0]) == 2 * ffi.sizeof('int')
+ assert ffi.sizeof(s[0]) == 6 * ffi.sizeof('int')
assert s.a[3] == 0
s = ffi.new("struct foo_s *", [424242, [-40, -30, -20, -10]])
- assert ffi.sizeof(s[0]) == 2 * ffi.sizeof('int')
+ assert ffi.sizeof(s[0]) == 6 * ffi.sizeof('int')
assert s.a[3] == -10
s = ffi.new("struct foo_s *")
assert ffi.sizeof(s[0]) == 2 * ffi.sizeof('int')
More information about the pypy-commit
mailing list