Calling C-Code from a Python Class Method -- Please Help!


Please can someone help me with an example of how to call a c code function from a python class method.

I am not having a problem with calling c functions in general, just from within a class.
I have this working just fine for some setup functions that don’t carry any input arguments.
However I am having no luck at all in sending across a single integer argument.
Later I will need to pass more fancy python objects so I really need to understand this.

example … in my Python class I have a method that looks like this…

def setRotation(self,r):

I am calling this from my main python file like this …

r = 3
disp.setRotation( r )

[where disp is the name of the class which I have already instantiated]

eventually I have c code which looks like this …

C_NATIVE(c_setRotation) {

int32_t a;
if (parse_py_args("ii",nargs,self,args,&a)!=2) return ERR_TYPE_EXC;

printf("set rot %x\n",a); 
return ERR_OK;


I obviously plan to do a little more in here but for the moment I am just trying to correctly find the value of the argument !!!

At the moment, regardless of what the value of r is in the the output is …
“set rot 10090008” which looks suspiciously like it might be an address ??
[and yes I have tried printing the contents of that address and it’s nothing useful].

The problem is with parse_py_args and I have tried so many variations. For instance if I remove the ‘self’ which I am really not sure is correctly applied to this function anyway … then the program either locks up completely or crashes.
Same thing if I reduce the fmt from two arguments “ii” down to one “i”. [and remove self]

I am sure there must be a very simple answer but I am at a loss now. Please Help !!!


Hi @w1bworx,

the problem is that it is not recommended to pass the object instance to your C function since with your code you are trying to parse the self object as an integer.

I would recommend to declare the hybrid C/Python function outside the class and call it from your method:

def c_setRotation(r):

def setRotation(self, r):
    self.rotation = c_setRotation(r)