embedded python questions

db dbunix at yahoo.com
Wed May 7 04:16:46 EDT 2003


Syver Enstad wrote:

> Yes, but you can only pass Python objects into
python
> code. In other words, you have to create Python
objects
> in your C code and then pass them to python. The
> scenario you present is a quite typical one, you
will
> typically have to write a Python C extension type
that
> enables python to use your C object.

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. :)

>
> Like this:
> // the module you want to pass your C extension type
to
> PyObject* module =
PyImport_ImportModule("MyModule");
> PyObject* entryFunction =
PyObject_GetAttrString(module, "MyFunc");
> // arg1 and arg2 would be data that you want to
expose to Python in
> // some fashion
> PyObject* myextensionType =
YouDefineThisFunction(arg1, arg2);
> PyObject_CallFunction(entryFunction, "O",
myExtensionType);

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

> For the socket example, there already exists a
socket c module so you
> should probably use that instead of writing your own
wrapper.

I saw this too, but I don't think I can use it in the
manner I propose. Python "sockets" are objects based
on a struct called PySocketSockObject defined in
Modules/socketmodule.c. They store other bits of
information other then the SOCKET_T sock_fd field
which is the low level part that I need. As the struct
is defined directly in the module and not in Python.h
header file, I don't think I have access to it even
from a C extension. 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.

Thanks for your help!

db

__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com





More information about the Python-list mailing list