embedding python in C++

Alex Martelli aleaxit at yahoo.com
Mon Jan 8 11:33:01 EST 2001


"Syver Enstad" <syver at NOSPAMcyberwatcher.com> wrote in message
news:93b02a$tr8$1 at troll.powertech.no...
>
> "Alex Martelli" <aleaxit at yahoo.com> wrote in message
> news:93a3lp01kpg at news1.newsguy.com...
>
> > For "grabbing the output", I assume you mean getting characters that the
> > script is emitting to standard-output.
>
> Yep, that's it.

That's what one normally wants to do, which is how I guessed:-).


> > For that, it's somewhat simpler to do
> > the 'output capture' in Python, with a StringIO object, then call its
> > getvalue method to get the contents as a string.
>
> Thank you very much, Alex. I had managed to solve the problem myself, but
> your solution involves massively less code. Very helpful. (I made a Python
> type in C++ and instantiated that in the python code, redirected
sys.stdout
> to it and got hold of the object and called it from C++ to get the string
> after casting it to the C++ type, this is much easier.)

You could do it all in C++ without real pain -- whence, the 'somewhat' in
my sentence above, rather than 'massively':-): instantiating the Python
type C++ object (which need only implement a 'write' method, and let
'softspace' be set & gotten from Python) in C++, and using, again from C++,
PyObject_SetAttrString to set it directly as the 'stdout' attribute of
the sys module.  But even if it's just 20/30 more lines, you get no real
extra value from it (...unless you need to catch output *as it's happening*,
e.g. to show it on some GUI...!), so, on the basis of "do the simplest
thing that can possibly work", it's probably best avoided anyway.

Another principle (a corollary of the main one) being, of course, "as little
C++ as you can possibly get away with":-).


> I am just playing around with extending/embedding Python to learn how to
> interface C++ to Python and vice versa. As a suitable/interesting (to me
> that is) project I choose to see how difficult it would be to make an
isapi
> extension (IIS) that processes url's that point to py files. If I could
make
> the ISAPI extension so that it could use cgi scripts written in python
> without modification it would be neat.

I'm only moderately familiar with IIS (I've written ASP's, but made no
other real use of ISAPI, in other words), so I can't be of much direct
help.  Personally, I currently use the Boost Python Library (BPL), at
www.boost.org, for all C++/Python work that I don't do through COM (with
the sole exception of what I need to do in C for general acceptance:-).


> I think my biggest problem is the multithreading and how that would affect
> the interpreter. For the extension to work it is vital that each thread
has
> it's "own interpreter" so that code from the different threads aren't
mixed
> with each other. I have another posting that concerns this issue but I am
> not sure if my requirements mandates using the Py_NewInterpreter function.

Sorry, can't help you here, either.  If I _was_ in a threading-is-crucial,
Win32-only situation, I'd be *certain* to use ActiveScripting, which
*ensures* I can instantiate just as many interpreters as I need (and they
_shouldn't_ thread on each other toes -- though I have NOT had occasion
to test this... as threading is another of those "the least I can possibly
get away with" complications, in my worldview:-).


Very interest project, anyway -- best of luck!

Alex






More information about the Python-list mailing list