4

I need a simple way to pass a C struct to a Python function. I have embedded Python into a game server, and I intend to write game logic in Python. I've scoured Google and mailing lists and found nothing useful. I have a complex structure in C (with pointers to other relatively complex structures) and have found no reasonable way of doing this.

I have this struct:

struct client {
 int state;
 int sockfd;
 struct sockaddr_in *addr;
 struct epoll_event *epollev;
 struct buffer *in_buffer;
 struct buffer *out_buffer;
 struct packet *packet;
 struct player *player;
};

And need to pass it to a Python function where I can easily access the members with common syntax (preferably not the use of things like dicts although that's alright too). It's almost like I need a PyObject_FromStruct function or something.

Is there any relatively simple way to do this?

Alfeky
6588 silver badges17 bronze badges
asked Dec 25, 2011 at 21:45
1
  • How exactly would I use Cython to pass a struct though? I'm looking through the documentation and can't find anything that's a working example of what I'm looking for. Thanks for your time. Commented Dec 27, 2011 at 6:29

4 Answers 4

2

SWIG can do pointers, especially if you can treat them as opaque blobs in your Python code.

You might also get somewhere with Cython - it's a dialect of Python that allows you to intermix Python and C datatypes.

answered Dec 25, 2011 at 23:50
Sign up to request clarification or add additional context in comments.

Comments

0

i think that the most simple way may be to write get()/set() functions for every element in the structure (ex: get_addr() which gets the address value) and then:

Option 1 (i think it is the simplest) :compile your c code as dll file with get/set exported and load it into python to call "get" functions (check my post here for using dll in python and this Q&A may be useful for using .so files in linux

Option 2 :you may use SWIG to get the pyd file and import it into python to call "get/set" functions

using this way python is dealing only with pointee NOT pointers and do not involve any memory addresses usage.

answered Dec 27, 2011 at 10:18

1 Comment

Yeah that's actually a good idea I think - having get/set functions in C with some API and then using them with Python. Kind of clunky, but it's the only seamless way. Marking this as my accepted answer for now unless someone can find a magical solution.
0

Your best bet would be something like swig, but I would think the pointers will give you problems.

answered Dec 25, 2011 at 21:59

1 Comment

Thanks for the input. I looked into it (here: swig.org/Doc1.3/Python.html#Python_nn19) but it seems like I will be having problems with pointers and it just wont work. I'm debating whether or not I should write the game server in Java and use Jython which will require much less "glue code," but I'm really looking for the performance and lightweight nature of non-abstracted C.
0

I'd wrap the struct in something digestible by python with boost::python. And then access the functionality you want exported to python.

answered Dec 27, 2011 at 11:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.