embedded python questions

db dbunix at yahoo.com
Wed May 7 23:16:58 EDT 2003


Syver Enstad wrote:

> Yes, you could do that, but how are you going to use
the int from
> python?

Using os.read() and os.write() like I mentioned in my
first mail. posix_write(fd,str) quite neatly parses
its first argument into an int, then passes it
directly to the write(2) system call, just like I
need.

I did get this to work! :) It didn't for quite a while
until I realized that I needed to "import os" as the
first line of my python script in order to get
os.write() . I felt like a complete noob :P

Here is the setup (based on your help from yesterday)
sans error checking:
In C:
int sockfd;
PyObject *module, *function, *ret;
sockfd = accept( blah, blah, blah );

module = PyImport_ImportModule( "mymod" );
function = PyObject_GetAttrString( module, "myfunc" );
ret = PyObject_CallFunction( function, "i", sockfd );

In mymod.py:
import os             // the key ;)
def myfunc( out )
  os.write( out, "Hello from Python on socket\n" )

So, now I have python talking over a pre-opened socket
from C, yay! Ok, now that I've got all that working, I
wish to break it again, so I have a couple more
questions.

First is error checking/exception handling. The
embedding manual discusses how to create/set
exceptions for handling in python, but not how to get
that info from C. Right now I have something lame
like:
ret = PyObject_CallFunction( function, "i", sockfd );
if( ret == NULL )
{
  print( "Call to myfunc failed\n" );
  exit( 1 );
}

This is enough to tell me when stuff breaks, but not
Python's reason for why. I see there are variables
like sys.exc_type, sys.exc_value, and
sys.exc_traceback that  must have C equivalents. Are
there sane Python C API calls that allow me to get at
these variables? Im guessing something about importing
sys and extracting more variables?
PyObject *sysmod = PyImport_ImportModule("sys");
PyObject *trace = PyObject_GetAttrString( sysmod,
"exc_traceback" );
Or is it part of the PyErr_* family?

Second, is it possible to import modules from C and
have them appear already imported in the script? For
example, is there a way I could load the "os" module
and then remove the first line of my script and still
have os.write() work? I tried adding a call to
PyImport_ImportModule( "os" ) before I call
PyObject_CallFunction() but it didn't seem to work.

Finally, is it possible to compile the python script
and save the result for execution later with different
sets of inputs?
PyObject *code = Py_Compile( "mymod" );
PyObject *val1 = Py_BuildValue( "i", someint );
PyObject *result1 = Py_Execute( code, val1 );
PyObject *val2 = Py_BuildValue( "i", anotherint );
PyObject *result2 = Py_Execute( code, val2 );

I've been through the C API reference manual and
didn't see anything that fit, but looking through the
source makes me think it might be possible. I know
this is getting pretty advanced, and I really
appreciate the help you've given me already.

Thanks again,

db

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





More information about the Python-list mailing list