Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting stdout using ctypes.

147 views
Skip to first unread message

Mathias Lorente

unread,
Aug 14, 2008, 11:31:12 AM8/14/08
to pytho...@python.org
Hello all.

I have a simple application (C++) that relies on shared libraries. It
works fine in console mode.
Lot of job is done into the shared library, so there is some calls to
'std::cout' to inform the user in it.

Now, I would like to wrap everything into a GUI, remove the application
and call directly everything from Python using ctypes. (I still need the
console application to launch it manually if needed).
I've made a simple library to test ctypes and everything works fine
except that I don't know how to get stout in order to redirect it
somewhere (dialog box or so).

I've looked for some help into the mailing list history and found
nothing useful (until now).
Do someone has any suggestion?

Mathias

Larry Bates

unread,
Aug 14, 2008, 12:49:34 PM8/14/08
to
If I'm understanding your question correctly, you can replace sys.stdout with
any class that provides a write method.

class myStdout(object):
def __init__(self):
self.lines = list()

def write(self, data):
self.lines.append(data)


Then in program do something like

import sys
sys.stdout = myStdout()

Now everything that would have gone to stdout will be buffered into the list in
sys.stdout.lines.

-Larry

Grant Edwards

unread,
Aug 14, 2008, 12:58:42 PM8/14/08
to

That's not going to redirect output that C/C++ dlls are writing
to stdout (Which is what the OP wants to do AFAICT).

If he's running Unix, he needs to replace the stdout file
descriptor (fd 1), with something that's connected to the
"write" end of a pipe. Then he needs to read the other
("read") end of the pipe in his application.

You do that using the dup2() system call:

http://www.unixwiz.net/techtips/remap-pipe-fds.html

--
Grant Edwards grante Yow! I have a VISION! It's
at a RANCID double-FISHWICH on
visi.com an ENRICHED BUN!!

Christian Heimes

unread,
Aug 14, 2008, 1:42:45 PM8/14/08
to Grant Edwards, pytho...@python.org
Grant Edwards wrote:
> If he's running Unix, he needs to replace the stdout file
> descriptor (fd 1), with something that's connected to the
> "write" end of a pipe. Then he needs to read the other
> ("read") end of the pipe in his application.
>
> You do that using the dup2() system call:

It's dangerous to mix streams and descriptors. Traditionally freopen()
is used to redirect a standard stream to a file:

http://www.gnu.org/software/libc/manual/html_mono/libc.html#Standard-Streams

— Function: FILE * freopen (const char *filename, const char *opentype,
FILE *stream)

This function is like a combination of fclose and fopen. It first
closes the stream referred to by stream, ignoring any errors that are
detected in the process. (Because errors are ignored, you should not use
freopen on an output stream if you have actually done any output using
the stream.) Then the file named by filename is opened with mode
opentype as for fopen, and associated with the same stream object stream.

If the operation fails, a null pointer is returned; otherwise,
freopen returns stream.

freopen has traditionally been used to connect a standard stream
such as stdin with a file of your own choice. This is useful in programs
in which use of a standard stream for certain purposes is hard-coded. In
the GNU C library, you can simply close the standard streams and open
new ones with fopen. But other systems lack this ability, so using
freopen is more portable.

Christian Heimes

unread,
Aug 14, 2008, 1:42:45 PM8/14/08
to pytho...@python.org, pytho...@python.org
Grant Edwards wrote:
> If he's running Unix, he needs to replace the stdout file
> descriptor (fd 1), with something that's connected to the
> "write" end of a pipe. Then he needs to read the other
> ("read") end of the pipe in his application.
>
> You do that using the dup2() system call:

It's dangerous to mix streams and descriptors. Traditionally freopen()

0 new messages