Skip to content

Instantly share code, notes, and snippets.

@alendit
Last active August 29, 2015 14:01
Show Gist options
  • Save alendit/d8614b5be070a93e3f9d to your computer and use it in GitHub Desktop.
Save alendit/d8614b5be070a93e3f9d to your computer and use it in GitHub Desktop.
Apparent cffi bug
#include <stdlib.h>
#include <stdio.h>
#include "mylib.h"
/* initialize the array with integers 0...length */
void initialize_MyArray(MyArray* a, long length){
a->length = length;
a->arr = (int*)malloc(length * sizeof(int));
int i;
for(i=0; i<length; i++){
a->arr[i] = i;
}
}
/* free the memory when finished */
void deallocate_MyArray(MyArray* a){
free(a->arr);
a->arr = NULL;
}
/* tools to print the array */
char* stringify(MyArray* a, int nmax){
char* output = (char*) malloc(nmax * 20);
int pos = sprintf(&output[0], "[");
int k;
for (k=0; k < a->length && k < nmax; k++){
pos += sprintf(&output[pos], " %d", a->arr[k]);
}
if(a->length > nmax)
pos += sprintf(&output[pos], "...");
sprintf(&output[pos], " ]");
return output;
}
void print_MyArray(MyArray* a, int nmax){
char* s = stringify(a, nmax);
printf("%s", s);
free(s);
}
typedef struct {
int* arr;
long length;
} MyArray;
void initialize_MyArray(MyArray* a, long length);
void deallocate_MyArray(MyArray* a);
char* stringify(MyArray* a, int nmax);
void print_MyArray(MyArray* a, int nmax);
╰─➤ ./test.py
__pycache__/_cffi__xd2227dcfx6cd0d953.c:158:34: error: unknown type name ‘MyArray’
static void _cffi_check__MyArray(MyArray *p)
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c: In function ‘_cffi_layout__MyArray’:
__pycache__/_cffi__xd2227dcfx6cd0d953.c:167:10: error: unknown type name ‘MyArray’
struct _cffi_aligncheck { char x; MyArray y; };
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:169:12: error: ‘MyArray’ undeclared (first use in this function)
sizeof(MyArray),
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:169:12: note: each undeclared identifier is reported only once for each function it appears in
In file included from __pycache__/_cffi__xd2227dcfx6cd0d953.c:3:0:
__pycache__/_cffi__xd2227dcfx6cd0d953.c:171:14: error: expected specifier-qualifier-list before ‘MyArray’
offsetof(MyArray, arr),
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:172:23: error: expected expression before ‘)’ token
sizeof(((MyArray *)0)->arr),
^
In file included from __pycache__/_cffi__xd2227dcfx6cd0d953.c:3:0:
__pycache__/_cffi__xd2227dcfx6cd0d953.c:173:14: error: expected specifier-qualifier-list before ‘MyArray’
offsetof(MyArray, length),
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:174:23: error: expected expression before ‘)’ token
sizeof(((MyArray *)0)->length),
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:179:3: warning: implicit declaration of function ‘_cffi_check__MyArray’ [-Wimplicit-function-declaration]
_cffi_check__MyArray(0);
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c: In function ‘_cffi_f_deallocate_MyArray’:
__pycache__/_cffi__xd2227dcfx6cd0d953.c:185:3: error: unknown type name ‘MyArray’
MyArray * x0;
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:201:3: warning: implicit declaration of function ‘deallocate_MyArray’ [-Wimplicit-function-declaration]
{ deallocate_MyArray(x0); }
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c: In function ‘_cffi_f_initialize_MyArray’:
__pycache__/_cffi__xd2227dcfx6cd0d953.c:212:3: error: unknown type name ‘MyArray’
MyArray * x0;
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:238:3: warning: implicit declaration of function ‘initialize_MyArray’ [-Wimplicit-function-declaration]
{ initialize_MyArray(x0, x1); }
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c: In function ‘_cffi_f_print_MyArray’:
__pycache__/_cffi__xd2227dcfx6cd0d953.c:249:3: error: unknown type name ‘MyArray’
MyArray * x0;
^
__pycache__/_cffi__xd2227dcfx6cd0d953.c:275:3: warning: implicit declaration of function ‘print_MyArray’ [-Wimplicit-function-declaration]
{ print_MyArray(x0, x1); }
^
Traceback (most recent call last):
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/unixccompiler.py", line 116, in _compile
extra_postargs)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/ccompiler.py", line 909, in spawn
spawn(cmd, dry_run=self.dry_run)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/spawn.py", line 32, in spawn
_spawn_posix(cmd, search_path, dry_run=dry_run)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/spawn.py", line 163, in _spawn_posix
% (cmd[0], exit_status))
distutils.errors.DistutilsExecError: command 'gcc' failed with exit status 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alendit/anaconda/envs/numba/lib/python3.3/site-packages/cffi/ffiplatform.py", line 47, in _build
dist.run_command('build_ext')
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/dist.py", line 949, in run_command
cmd_obj.run()
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/command/build_ext.py", line 353, in run
self.build_extensions()
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/command/build_ext.py", line 462, in build_extensions
self.build_extension(ext)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/command/build_ext.py", line 517, in build_extension
depends=ext.depends)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/ccompiler.py", line 574, in compile
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/distutils/unixccompiler.py", line 118, in _compile
raise CompileError(msg)
distutils.errors.CompileError: command 'gcc' failed with exit status 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./test.py", line 22, in <module>
main()
File "./test.py", line 17, in main
lib = ffi.verify(sources=['mylib.c'], include_dirs=['.'])
File "/home/alendit/anaconda/envs/numba/lib/python3.3/site-packages/cffi/api.py", line 339, in verify
lib = self.verifier.load_library()
File "/home/alendit/anaconda/envs/numba/lib/python3.3/site-packages/cffi/verifier.py", line 74, in load_library
self._compile_module()
File "/home/alendit/anaconda/envs/numba/lib/python3.3/site-packages/cffi/verifier.py", line 139, in _compile_module
outputfilename = ffiplatform.compile(tmpdir, self.get_extension())
File "/home/alendit/anaconda/envs/numba/lib/python3.3/site-packages/cffi/ffiplatform.py", line 25, in compile
outputfilename = _build(tmpdir, ext)
File "/home/alendit/anaconda/envs/numba/lib/python3.3/site-packages/cffi/ffiplatform.py", line 50, in _build
raise VerificationError('%s: %s' % (e.__class__.__name__, e))
cffi.ffiplatform.VerificationError: CompileError: command 'gcc' failed with exit status 1
(numba)╭─alendit@alendit-UX31A ~/numba/buffer ‹master*›
╰─➤ python --version
Python 3.3.5 :: Continuum Analytics, Inc.
#!/usr/bin/env python
from cffi import FFI
def main():
ffi = FFI()
ffi.cdef("""typedef struct {
int* arr;
long length;
} MyArray;
void initialize_MyArray(MyArray* a, long length);
void deallocate_MyArray(MyArray* a);
void print_MyArray(MyArray* a, int nmax);
""")
lib = ffi.verify(sources=['mylib.c'], include_dirs=['.'])
if __name__ == "__main__":
main()
#!/usr/bin/env python
from cffi import FFI
def main():
ffi = FFI()
ffi.cdef("""typedef struct {
int* arr;
long length;
} MyArray;
void initialize_MyArray(MyArray* a, long length);
void deallocate_MyArray(MyArray* a);
void print_MyArray(MyArray* a, int nmax);
""")
lib = ffi.verify(open("mylib.c", "r").read(), include_dirs=['.'])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment