embedding python in C++

Alex Martelli aleaxit at yahoo.com
Tue Jan 9 12:07:11 EST 2001


"Syver Enstad" <syver at NOSPAMcyberwatcher.com> wrote in message
news:YmE66.99$k25.1060 at news1.oke.nextra.no...
>
> "Alex Martelli" <aleaxit at yahoo.com> wrote in message
> news:93cq6n0186t at news1.newsguy.com...
> > 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++,
> Mmm, that's what I did, didn't implement the softspace attribute though.
> What's it for anyway?

Python's print statement sets it when it sees a comma (requiring
space-separation), reads it back to determine if it must output a
space, etc.  Basically it's used to avoid a useless trailing space
in cases such as
    print goo,
    print
I don't know what happens if you don't let softspace be set & read
(i.e., whether print degrades gracefully or what) -- note that all
Python instance objects ARE 'expando' (Javascript terminology) by
defaults, so you don't normally have to do anything special to enable
it, but, if you're using your own C-level type rather than a Python
implemented instance...


> > PyObject_SetAttrString to set it directly as the 'stdout' attribute of
> > the sys module.
>
> I did that from python instead.

Yep, but that's not significantly more difficult to do from C.


> >on the basis of "do the simplest
> > thing that can possibly work", it's probably best avoided anyway.
> Yeah, shave it with Occam's razor!
>
> > Another principle (a corollary of the main one) being, of course, "as
> little
> > C++ as you can possibly get away with":-).
> Yes, but sometimes it's fun struggle with C++ too, but it generally

Sure.  Running 10 miles every morning is very good exercise, too
(and probably even better for your health), just not the easiest
way to go from point A to point B (generally:-).

> dispappears when I start trying to reuse things which is a real pain in
C++,
> not to mention runtime libraries, and the need to have a lib compiled in
at
> least 4 different versions dependening on which other libraries you link
> with.

Platform issues I guess...


> >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:-).
>
> Yes, boost looks interesting. I've downloaded it, but I don't think I'll
use
> it before I have a better grasp of the underlying API.

It can spoil you by masking the API so gracefully, yes:-).


> > 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:-).
>
> Okay, do you have any examples on how to use that instead?

Use what?  ActiveScripting?  Sure, it's pretty easy (via Microsoft
ScriptControl).  Instantiate in each relevant thread (after suitably
CoInitializeEx'ing, of course) a "MSScriptControl.Control" object,
set its "Language" property to the string "Python", add code to
this instance with the method 'AddCode', and of course use its
ExecuteStatement and Eval methods to do -- guess what (execute
statements, and evaluate methods, respectively!-).  You need to
expose your own functionalities as Automation (COM) interfaces and
add them with names to the ScriptControl via the ScriptControl's
AddObject method, see e.g. Q185889 on MSDN (all examples I've ever
found of ScriptControl are in/for VB, but then you do generally
need to be able to read some VB code to make heads or tails out of
most object models' documentations!-).  A good general overview
article (again VB-oriented) can be found at
    http://www.microsoft.com/mind/0799/script/script.htm

My "Win32 API" tutorial (for C, and in Italian) does include an
example of ScriptControl use (http://aleax.supereva.it/TutWin32/s39.htm),
but the comments &c are in Italian and the example does NOT need to
expose any functionality to the scripts (just uses the control to
interpretively execute arbitrary expressions entered by the user,
for the purpose of doing a function-plot -- it's really just a
didactical exercise on doing some graphics at the API level!-).


Alex






More information about the Python-list mailing list