python - How to support 64 bits pointers in cffi? -
i'm using cffi interface python module c-library.
i got working fine on linux i'm having hard time mac os x (yosemite - 64 bits). here minimal sample wrote shows problem:
foo.h
#ifndef foo_h #define foo_h #include <stddef.h> #ifdef __cplusplus extern "c" { #endif void* mymalloc(size_t); #ifdef __cplusplus } #endif #endif foo.cpp
#include "foo.h" #include <cstdlib> #include <cstdio> void* mymalloc(size_t size) { void* const result = ::malloc(size); printf("%p\n", result); return result; } makefile
run: foo.py libfoo.dylib dyld_library_path=. library_path=. python foo.py libfoo.dylib: foo.cpp foo.h g++ -dynamiclib -o $@ $< foo.py
import cffi ffi = cffi.ffi() ffi.cdef(""" void* malloc(size_t); void* mymalloc(size_t); """) api = ffi.verify("", libraries=['foo']) result = api.malloc(4) print "malloc", result result = api.mymalloc(4) print "mymalloc", result so nothing fancy here: simple mymalloc(size_t) functions acts wrapper real malloc(size_t) 1 , shows generated pointer before returning.
when executing foo.py (with make run), see following output:
g++ -dynamiclib -o libfoo.dylib foo.cpp dyld_library_path=. library_path=. python foo.py malloc <cdata 'void *' 0x7fdba0e01670> 0x7fdba0e00280 mymalloc <cdata 'void *' 0xffffffffa0e00280> in case of mymalloc(size_t), seems cffi somehow truncated/modified pointer integral value: 0xffffffffa0e00280 instead of expected 0x7fdba0e00280. value of pointer stored on 32 bits. malloc(size_t) has exact same prototype seems correctly handled cffi , returns 64 bits address.
i'm @ loss trying find wrong here. have clue ?
according official answer, way of calling verify() bogus.
the correct code should read:
import cffi ffi = cffi.ffi() code = """ void* malloc(size_t); void* mymalloc(size_t); """ ffi.cdef(code) api = ffi.verify(code, libraries=['foo']) result = api.malloc(4) print "malloc", result result = api.mymalloc(4) print "mymalloc", result which indeeds yields expected result !
Comments
Post a Comment