I’m trying to redo the Misaka module in ctypes, but when I try to use bufputs I get an error (see end of 2nd code sample). When I pass the pointer to the function I use pointer(b). That doesn’t work and byref(b) doesn’t work either.
This is the function signature:
/* bufputs • appends a NUL-terminated string to a buffer */
void
bufputs(struct buf *, const char*);
This is my code:
>>> from ctypes import *
>>> sundown = cdll.LoadLibrary('./libsundown.so.1')
>>> sundown
<CDLL './libsundown.so.1', handle 1e2f190 at 1bea0d0>
>>> # OUT: <CDLL './libsundown.so.1', handle 2840d80 at 2797290>
>>> class buf(Structure):
... _fields_ = [
... ('data', c_char_p),
... ('size', c_size_t),
... ('asize', c_size_t),
... ('unit', c_size_t),
... ('ref', c_int)]
...
>>> sundown.bufnew.argtypes = [c_size_t]
>>> sundown.bufnew.restype = buf
>>> b = sundown.bufnew(c_size_t(1024))
>>> sundown.bufputs.argtypes = [POINTER(buf), c_char_p]
>>> s = c_char_p('this is a test')
>>> sundown.bufputs(pointer(b), s)
python2: malloc.c:3574: mremap_chunk: Assertion `((size + offset) & (mp_.pagesize-1)) == 0' failed.
Aborted
I can’t figure out what I did wrong.
The OP’s solution, originally posted in the question