1

I've done a lot of research and nothing came up... I'm new to Python and Ctypes and I'm trying to call functions from a shared library. So far so good, but these functions take as parameter specifics datatypes from structures defined inside the .so

my question is, I've seen examples of how to declare the "class Structure" in Python, but this is what I have in the .so

typedef struct noPDDE
{
     void *x;
     struct noPDDE *y;
     struct noPDDE *z;
}NoPDDE,*pNoPDDE;

typedef struct PDDE
{
    int tam;
    pNoPDDE sup;
}PDDE;

I have no idea how to pass the PDDE pointer to the functions.

Any help is useful. thanks a lot.

1
  • PDDE is a struct, not a pointer. Do you mean "pass a pointer to a PDDE struct"? Commented Jul 19, 2015 at 15:01

1 Answer 1

4

This is the way one declares recursive structures in ctypes:

 from ctypes import (
     Structure,
     c_void_p,
     POINTER,
     c_int,
     byref,
 )


 class noPDDE(Structure):
     pass

 noPDDE._fields_ = [
     ("x", c_void_p),
     ("y", POINTER(noPDDE)),
     ("z", POINTER(noPDDE)),
     ]


 class PDDE(Structure):
     _fields_ = [
         ("tam", c_int),
         ("sup", POINTER(noPDDE)),
         ]



 foo = PDDE()

 mylib.func_that_takes_pointer_to_pdde(byref(foo))
Sign up to request clarification or add additional context in comments.

5 Comments

I did exactly as you wrote, but still something missing here.. The first error is "cannot import void_p" , so I used ("x", c_void_p). Ok. now, its telling me that the second element on fields in PDDE class has to be a c type...
I made a mistake, because I obviously can't run your code due to the lack of that weird library of yours, and corrected it. But you will need to figure similar things out on your own, the error-messages is pretty clear, and the documentation on ctypes-Structure as well.
ok, thank you very much @deets ! I had no idea how to declare recursive structures.
I already did @deets , but I just created my account, I guess I don't have enough reputation :/
@eryksun drat. You are correct. I did create a simple example without the call, because I possibly can't have a real c-call that works. But I should have checked with pylint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.