SWIG C++ std::cout do not output to interactive interpreter IDLE

Juergen Perlinger juergen.perlinger at t-online.de
Tue Apr 22 21:03:00 CEST 2008

wongjoekmeu at yahoo.com wrote:

> Dear All,
> I have some functions written in C++, which I try to approach from
> python using swig. In the C++ functions I use std::cout to print stuff
> to output. Everything works fine, but the only problem that I have is
> that when I start IDLE and use the functions what std::cout should
> print to the "IDLE console" simply does not appear. When I don't use
> IDLE but simply double click on the .py script, it works all fine. I
> was wondering how I could make sure that std::cout would print also to
> IDLE. Thanks a lot in advance for helping to answer my question.
> RR

Hmmmmm... probably tricky. Most GUIs replace 'sys.stdout' by something that
behaves like a Python(!) stream, without actually touching file descriptor
1 (which is the actual process file descriptor for stdout).

>From my experience with embedding/extending Python this is going to be a bit
nasty, because you would need to get access to the actual stream object in
Pythons 'sys' module (easy...), wrap this one in C++ to create a C++
ostream descendant that uses an underlying Python stream (here's the
work...) and use it to replace 'cout' (that one's simple after all...).

Are you sure you want to tackle this? I've always tried to avoid it and
lived with the fact that such an extension was only printing something
useful to its output when used as a console application.

I haven't had a deeper look into the BOOST Python bindings, (www.boost.org)
but they have a rather good reputation. Perhaps there is something in it
that takes away most of the burden.

juergen 'pearly' perlinger
"It's hard to make new errors!"

More information about the Python-list mailing list