embedded python questions

Syver Enstad syver-en+usenet at online.no
Wed May 7 15:48:26 EDT 2003


db <dbunix at yahoo.com> writes:

> Since a socket is just a int in C, is it possible that
> I could just use something like:
> PyObject *mysock = Py_BuildVal( "i", sockfd );
> rather then creating my own type? I'll probably be
> able to answer this myself when Im able to get back to
> my code. :)

Yes, you could do that, but how are you going to use the int from
python? I don't know the socket library by heart, so I popped up the
documentation and I see that socket.fromfd might help. It takes an
integer and creates a python socket object from it. So that way you
could create your socket on the C side and pass it into python as an
int and then use it from python by doing socket.fromfd


> If what I wrote above is true, this could be:
> PyObject_CallFunction( entryFunction, "i", sockfd );

Yes, using CallFunction you don't even have to create a Python integer
object, sockfd could be the raw C int.
 
>Please correct me if Im wrong or
> there is a way to convert a open C socket (i.e. an
> int) into a Python socket object.

See above regarding socket.fromfd. You could call this function from
the C code too, by using the Python C api. 

Some thing like this (not tested).
PyObject* socketMod = PyImport_ImportModule("socket");
PyObject* fromFdFunc = PyObject_GetAttrString(module, "fromfd");
PyObject_CallFunction(fromFdFunc, "iBlaBla", yourSocketAsCint,
        otherArgs1, and2, andSoOn);

It's up to you if you want to do the conversion in python code or
convert it before passing it into your python function. 

-- 

Vennlig hilsen 

Syver Enstad




More information about the Python-list mailing list