views:

88

answers:

2

Hi folks,

I've been trying to figure out how to wrap the following C functions = compress.c , compress.h.

I tried following the tutorials, but after creating the .pxd file I don't know what to do :|

From what I have understood this is the pxd file that I should have

cdef extern from "compress.h":

    size_t compress(void *s_start, void *d_start, size_t s_len)
    size_t decompress(void *s_start, void *d_start, size_t s_len, size_t d_len)
    uint32_t checksum32(void *cp_arg, size_t length)

After this I have no clue what to do :|


Help would be great guys! =)


Edit:

Getting this error

~/essentials/pylzjb# gcc -I /usr/include/python2.6 -shared -o pylzjb.so pylzjb.c compress.c

/usr/bin/ld: /tmp/ccQLZTaG.o: relocation R_X86_64_32S against `_Py_NoneStruct' 
can not be used when making a shared object; recompile with -fPIC
/tmp/ccQLZTaG.o: could not read symbols: Bad value
collect2: ld returned 1 exit status

thanks anyway =)

+2  A: 

write a .pyx file that implements a wrapper calling the C functions? I think the toughest part might be buffer handling...

pylzjb.pyx could look as follows (note that your .pxd is inlined):

cdef extern from "compress.h":
    size_t compress(void *s_start, void *d_start, size_t s_len)

from stdlib cimport *

def cmpr(bytes s):
    cdef size_t n = len(s)
    cdef unsigned char *dst = <unsigned char *> malloc(n * sizeof(unsigned char))
    try:
        m = compress(<unsigned char *> s, dst, n)
        ret = [dst[i] for i from 0 <= i < m]
    finally:
        free(dst)
    return ret

compile with:

cython -I. pylzjb.pyx
gcc -I /usr/include/python2.5 -shared -o pylzjb.so pylzjb.c compres.c

and try to use with

import pylzjb
pylzjb.cmpr('asfd')
pmeerw
pmeerw: I am really happy I got your reply, thank you so much!
RadiantHex
how different would decompress be? that's the function I really really need. Thank you again
RadiantHex
+1  A: 

the Google code project pylzjb seems to implement a Python interface for compress.c|h already?

pmeerw
I have no clue how to use it :|
RadiantHex
If you are talking about the binaries, they aren't compatible with my system, linux 64bit
RadiantHex
The `setup.py` won't run due to a module that seems to only exist on the author's hard drive.
Ignacio Vazquez-Abrams
Thanks Ignacio, I guess that nails the coffin :)
RadiantHex