1

How do I use a C struct foo, defined in a header file as a datatype in my Python code?

(This document does not seem to address the issue.)

typedef struct {

    PyObject_HEAD

    /* Type-specific fields go here. */

     struct api_query query; /* instead of PyObject * type here */

} api_Request;
Peter O.
  • 32,158
  • 14
  • 82
  • 96
qwrty
  • 331
  • 3
  • 15
  • 1
    What is your objective? What do you intend to do with the C structure? Pass to some shared library function calls, access it's contents? – isedev Jan 31 '13 at 09:33
  • Pass the structs ( request )and get a response (another struct) to a socket, so that the server may give the required result – qwrty Jan 31 '13 at 09:38
  • The document you link is indeed the right source. Read it carefully; it does answer your question. – Martin v. Löwis Jan 31 '13 at 11:01

1 Answers1

0

Building an extension module is not a trivial task (the document you linked to does explain how to do it in detail). To wrap a C structure that way, you need to define the new type and a Python's object usual methods (constructor, destructor, access methods, etc...).

You might find the ctypes package is an easier way to go about it.

Better still, if all you need to do is create the structure and send it (to a socket, as you say), and if the structure is simple enough, then the struct.pack function might prove easiest.

isedev
  • 18,848
  • 3
  • 60
  • 59