3

I am attempting to use Cython to wrap an external C library which has several functions which use the ellipsis (...) as part of the signature. The code here provides a partial solution to the problem. The relevant portion for my question is

cdef int foo(int n, ...):
    // Print the variable number of arguments

def call_foo():
    foo(1, 2, 3, 0)
    foo(1, 2, 0)

This will output 1, 2, 3 and 1, 2 which is fine, as far as it goes. However, what I need to be able to do is pass the variable arguments through Python. Something like

def call_foo(*args):
    foo(args)

call_foo(1,0)
call_foo(1,2,3,4,0)

However, while the above code will compile, upon execution I get a TypeError:

File "cytest.pyx", line 31, in cytest.call_foo (cytest.cpp:915)
foo(args)
TypeError: an integer is required

An alternate form,

def call_foo(*args):
    foo(args[0],args[1:])

call_foo(1,0)
call_foo(1,2,3,4,0)

results in a compile error:

cytest.pyx:30:24: Python object cannot be passed as a varargs parameter

Is there any way to accomplish this using Cython, or will I have to rewrite the C functions?

PaxRomana99
  • 564
  • 1
  • 5
  • 12
  • I don't think the works with Cython. I think it might with ctypes if that's any help? (You could use it just for this one function) – DavidW Jan 16 '17 at 19:58
  • I'm afraid that I am not familiar with ctypes. Is it possible to mix Cython and ctypes in the same module? Or would this approach force me to break the functionality out into multiple modules? – PaxRomana99 Jan 17 '17 at 13:44
  • I've flagged it as a duplicate since I've answered something very similar before. I don't think it's a perfect duplicate (the original question also tries to involve variadic macros) so if the answer doesn't help you then I'll retract the duplicate vote. I'm reasonably confident that you can just use that solution with a few types changed. – DavidW Jan 17 '17 at 13:57
  • And yes, you can use ctypes from within Cython so you can do it all in one module. – DavidW Jan 17 '17 at 13:58
  • That looks like a valid work-around. Let me implement and test and I will get back to you. – PaxRomana99 Jan 17 '17 at 14:40
  • I'm getting an error: "AttributeError: function 'sum_va' not found" when I try to load the *.pyd file (ie sum_va = this_file_library.sum_va). I hard coded the absolute path for testing, and am unfortunately on a Windows 7 OS. My other particulars are 64 bit Python 3.5 and Visual Studio 2015 64 bit compiler. Did I read your solution correctly, I should be trying to load the *.pyd file using CDLL? – PaxRomana99 Jan 17 '17 at 19:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133382/discussion-between-paxromana99-and-davidw). – PaxRomana99 Jan 17 '17 at 19:52

0 Answers0