Two questions on embedding :)

Stephen Hansen stephen at cerebralmaelstrom.com
Thu Jun 1 19:42:47 EDT 2000


    Last night I was fooling around and embedding Python into a little C++
Program I was working on, for no other reason then the fact that I had to do
some string manipulation that was a pain in C,...so, I spent God Only Knows
how long sticking Python into my program to make writing above said
manipulation easier. Somehow, I don't think the end effecti s Less Work at
all :)

    What I wanted to do was import two modules -- sys and languages -- the
latter of which was one that I wrote for the program. The latter provides
two functions: Load() and Translate(lang,string). Load returns nothing,
Translate returns the translated string.

Question#1:
    I ended up having to do PyRun_SimpleString("import sys, languages");
because I couldn't get the PyImport modules to work, at all :P

    PyImport_ImportModule("sys") didn't raise any exceptions but later calls
to PyRun_SimpleString("print sys.path") raised an NameError exception on
sys.

    I tried PyImport_AddModule("sys") afterwards, thinking from the docs
that mabye the above function didn't add it into sys.modules and this was
was needed.. still didn't work. I don't recall exactly what else I did, but
I tried about a dozen different combinations of commands and so on and so
forth until I just gave up and did PyRun_SimpleString("import sys");.

    Does anyone have an example program anywhere that incorporates importing
modules in a C++ level? Hrm. Actually, is there a need to bother? Is it any
better then just doing a PyRun_SimpleString?

    I ended up finding the solution to most of my problems by looking int he
'pysvr' demo but couldn't find a demo to show the above opperations.

Question#2:

    Later, I basicaly wanted to call: "languages.Translate('name','string')"
and have the result returned to the C program for processing. My code was
somewhat like the below:
----->
    PyObject *Result,*MainModule,*Globals;

    MainModule = PyImport_AddModule("__main__");
    Py_INCREF(MainModule); // since I'm storing it?

    Globals = PyModule_GetDict(MainModule);
    Py_INCREF(Globals);

    Result =
PyRun_String("languages.Translate('name','string')",Py_single_input,Globals,
Globals);
<-----
    Origionally, I was then moving on to do: if(PyString_Check(Result))
Output->Lines->SetText(PyString_AsString(Result));

    But I discovered that the if was never executing -- because
PyString_Check(Result) never returned true. I did a couple other debugging
checks, and it also didn't show up true as PyTuple_Check() or anything
else...
    The help files say that PyRun_String returns the result as a Python
object -- which it does.. but what KIND of python object? How do I get at
the result?

    To make it work, I ended up just appending a 'tmp =' in front of my
command, and for the assignment into Output->Lines I grabbed the text via
PyMapping_GetItemString(Globals,'tmp'); but that doesn't seem like the Right
Way To Do It. ;)


Sorry for the verbosity, but I wanted to include enough information that yuo
shuoldn't have to ask any questions.. but of course i'll forget that one
most important bit of information that you'll need so you'll have to ask
anyways.  Ah well. :)

--S





More information about the Python-list mailing list