First, you *need* to rename the function C-side to something else (the
symbol will conflict with Python's main function).
Then, I think this should work:
# import malloc, free
def wrapper(args):
cdef char **c_argv
# make *sure* that we have the exact string object for every argument
#, and don't invoke __str__ on something else!!
args = [b'calling_from_cython'] + [bytes(x) for x in args]
# or, use str(x).encode(...) above, depending on what API you want
and what encoding C program expect
c_argv = <char**>malloc(sizeof(char*) * len(args)) # + try/finally
and free!!
for idx, s in enumerate(args):
c_argv[idx] = s
main_in_c(len(args), c_argv)
Dag Sverre
I'd question this. The subprocess module is simple and relatively fast (at
least on well behaved operating systems). Unless you are calling the
program very often and it's short running so that the startup overhead
matters (or unless that would allow you to use a substantially faster way
of communicating with it than stdin/out), I'd go with subprocess.
Stefan
Then, I think this should work:
# import malloc, free
def wrapper(args):
cdef char **c_argv
# make *sure* that we have the exact string object for every argument
#, and don't invoke __str__ on something else!!
args = [b'calling_from_cython'] + [bytes(x) for x in args]
# or, use str(x).encode(...) above, depending on what API you want and what encoding C program expect
c_argv = <char**>malloc(sizeof(char*) * len(args)) # + try/finally and free!!
for idx, s in enumerate(args):
c_argv[idx] = s
main_in_c(len(args), c_argv)
Yes, that's about what it takes.
> In the example in http://wiki.cython.org/DynamicMemoryAllocation I don't see
> the check after the malloc. Is there a reason for not doing it?
That page is slightly outdated. And, yes, the check is missing.
Stefan