The Cython equivalent of c defines #define myfunc (node ​​x, ...) SetNode (x.getattributeNode (), __ VA_ARGS__)

The kithonic equivalent of c defines

#define myfunc(Node x,...) SetNode(x.getattributeNode(),__VA_ARGS__)

      

I have a c api SetNode that takes a first argument a node of type struct node and N variables (N is a variable number from 0-N)

here for example to solve such a problem

exampleAPI.c

#include<stdarg.h>
float sumN(int len,...){
    va_list argp;
    int i;
    float s=0;
    va_start(argp,len);
    for(i=0;i<len;i++){
       s+=va_arg(argp,int);
    }
    va_end(argp);
}

      

exampleAPI.h

#include<stdarg.h>
float sumN(int len,...)

      

examplecode.c

#include<stdarg.h>
#include"exampleAPI.h"

int len(float first,...){
    va_list argp;
    int i=1;
    va_start(argp,first);
    while(1){
       if(va_arg(argp,float)==NULL){
            return i
       }
       else{
            i++;
       }
    }
    va_end(argp);
}

#define sum(...) sumN(len(__VA_ARGS__),__VA_ARGS__)

      

Now call

sum(1,2,3,4);

      

will return 10.000000

sum(1.5,6.5);

      

will return 8.00000

I need a cython alternative to define bellow c and not the example above because I have a C-API that has a SetNode function that takes a variable number of arguments and I want to wrap it in cython and call from python

 #define myfunc(Node x,...) SetNode(x.getattributeNode(),__VA_ARGS__)

      

here node is a class defined in cython that contains the c stuct attribute as an attribute and getattributeNode () is a node class function that returns a c struct to be passed to the C-API.

cdef extern "Network.h":

    ctypedef struct node_bn:
         pass

    node_bn* SetNode(node_bn* node,...)


cdef class Node:

    cdef node_bn *node

    cdef getattributeNode(self):
        return self.node     

    def setNode(self,*arg):
        self.node=SetNode(self.node,*arg) # Error cannot convert python objects to c type

      

Alternative thing i tried

cdef extern from "stdarg.h":
   ctypedef struct va_list:
      pass
   ctypedef struct fake_type:
      pass
   void va_start(va_list, void* arg)
   void* va_arg(va_list, fake_type)
   void va_end(va_list)
   fake_type int_type "int" 

cdef extern "Network.h":

    ctypedef struct node_bn:
         pass

    node_bn* VSetNode(node_bn* node,va_list argp)


cdef class Node:

    cdef node_bn *node

    cdef getattributeNode(self):
        return self.node     

    cpdef _setNode(self,node_bn *node,...):
        cdef va_list agrp
        va_start(va_list, node)       
        self.node=VSetNode(node,argp) 
        va_end(va_list)

    def setNode(self,*arg):
        self._setNode(self.node,*arg) 

      

works great when the argument list is empty

n = Node()
n.setNode() #This works
n.SetNode("top",1) # error takes exactly one argument given 3 in  self._setNode(self.node,*arg)

      

If someone can suggest a cython equivalent, that would be great.

+3


source to share


2 answers


I don't think it's easy to do it, though Cython (the problem is telling Cython what type conversions to do for an arbitrary number of arguments). The best I can suggest is to use the standard library ctypes for this particular case and wrap the rest in Cython.

I used a very simple sum function as an example. va_sum.h contains:

typedef struct { double val; } node_bn;

node_bn* sum_va(node_bn* node,int len, ...);
/* on windows this must be:
__declspec(dllexport) node_bn* sum_va(node_bn* node,int len, ...);
*/

      

and va_sum.c contains:

#include <stdarg.h>

#include "va_sum.h"

node_bn* sum_va(node_bn* node,int len, ...) {
    int i;
    va_list vl;
    va_start(vl,len);
    for (i=0; i<len; ++i) {
        node->val += va_arg(vl,double);
    }
    va_end(vl);
    return node;
}

      



I wrote this so it adds everything to a field in a struct to demonstrate that you can navigate with pointers to structs without any problem.

Cython file:

# definition of a structure
cdef extern from "va_sum.h":   

    ctypedef struct node_bn:
        double val;

# obviously you'll want to wrap things in terms of Python accessible classes, but this atleast demonstrates how it works
def test_sum(*args):
    cdef node_bn input_node;
    cdef node_bn* output_node_p;
    input_node.val = 5.0 # create a node, and set an initial value

    from ctypes import CDLL, c_double,c_void_p
    import os.path
    # load the Cython library with ctypes to gain access to the "sum_va" function
    # Assuming you've linked it in when you build the Cython module
    full_path = os.path.realpath(__file__)
    this_file_library = CDLL(full_path)

    # I treat all my arguments as doubles - you may need to do
    # something more sophisticated, but the idea is the same:
    # convert them to the c-type the function is expecting
    args = [ c_double(arg) for arg in args ]
    sum_va = this_file_library.sum_va
    sum_va.restype = c_void_p # it returns a pointer

    # pass the pointers as a void pointer
    # my c compiler warns me if I used int instead of long
    # but which integer type you have to use is likely system dependent
    # and somewhere you have to be careful
    output_node_p_as_integer = sum_va(c_void_p(<long>&input_node),len(args),
                           *args)

    # unfortunately getting the output needs a two stage typecast - first to a long, then to a pointer
    output_node_p = <node_bn*>(<long>(output_node_p_as_integer))
    return output_node_p.val

      

You need to compile va_sum.c along with your Cython file (for example adding sources = ['cython_file.pyx','va_sum.c']

in setup.py)

Ctypes is probably a bit slower than Cython (I think there is a reasonable overhead on every call) and it's weird to mix them up, but that should at least allow you to write a basic shell in Cython and use ctypes to work around the particular limitation.

+2


source


This is probably not the correct answer as I am not sure I fully understand the question. I would answer in a comment, but the code formatting is too bad. Functions are available in Python sum

and len

:

def my_len(*args):
    return len(args)

def my_sum(*args):
    return sum(args)

print "len =", my_len("hello", 123, "there")
print "sum =", my_sum(6.5, 1.5, 2.0)

      



outputs:

len = 3
sum = 10.0

      

+2


source







All Articles