Re: [python-cffi] cannot instantiate ctype of unknown size

37 views
Skip to first unread message

Armin Rigo

unread,
Feb 18, 2023, 4:49:49 PM2/18/23
to pytho...@googlegroups.com
Hi Rohit,

On Sat, 18 Feb 2023 at 18:37, 'Rohit' via python-cffi
<pytho...@googlegroups.com> wrote:
> commit_ptr = ffi.new("git_annotated_commit *")
>
> Error:
> TypeError: cannot instantiate ctype 'struct git_annotated_commit' of unknown size

The error means that as far as cffi knows, this struct is opaque. It
was declared in ffi.cdef() by writing `struct git_annotated_commit;`
with no braces and no list of fields. The line with the error is
equivalent to the C code `malloc(sizeof(git_annotated_commit))`. As
that struct is opaque, sizeof() doesn't make sense on it.

If you were writing C code, do you really want to call
`malloc(sizeof(git_annotated_commit))` here? I don't know what you
are really trying to do, but maybe you are trying to get from the git
library a pointer to an already-existing commit, and not make a new
one yourself from scratch. In that case, such a malloc doesn't really
make sense. If I'm wrong and you really want to make a new structure
from scratch, then you will likely need the actual field names in the
following lines anyway, otherwise you couldn't fill your new
structure. In that case, it means that the actual header of the
library hasn't been completely ported to cffi yet: the header contains
actual details about what is in the struct git_annotated_commit, but
the ffi.cdef() is missing them, treating that struct as opaque.


Hope this helps,

Armin Rigo
Reply all
Reply to author
Forward
0 new messages