Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36977

Re: python sys.stdout and C++ iostreams::cout

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'essentially': 0.04; 'case.': 0.05; 'sys': 0.05; 'wrapped': 0.07; 'wrapper': 0.07; 'python': 0.09; 'assigning': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referencing': 0.09; 'stdout': 0.09; 'stringio': 0.09; 'sys.stdout': 0.09; 'subject:python': 0.11; 'url:)': 0.13; 'advance.': 0.15; 'passing': 0.15; '"-"': 0.16; 'c++.': 0.16; 'cstringio': 0.16; 'orig_stdout': 0.16; 'received:80.91.229.3': 0.16; 'received:edu.au': 0.16; 'received:plane.gmane.org': 0.16; 'stringio()': 0.16; 'string': 0.17; 'wrote:': 0.17; 'code,': 0.18; 'written': 0.20; 'import': 0.21; 'assuming': 0.22; 'finally,': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'c++': 0.27; 'replace': 0.27; "doesn't": 0.28; 'header:X -Complaints-To:1': 0.28; 'writes': 0.30; 'function': 0.30; 'on,': 0.30; 'running': 0.32; 'print': 0.32; 'anyone': 0.33; 'to:addr :python-list': 0.33; 'hi,': 0.33; 'thanks': 0.34; 'process,': 0.35; 'so,': 0.35; 'similar': 0.35; 'received:org': 0.36; 'but': 0.36; 'received:au': 0.36; 'does': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'back': 0.62; 'physical': 0.69; 'captures': 0.84; 'console,': 0.84; 'url:reference': 0.84; 'luck': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Lie Ryan <lie.1296@gmail.com>
Subject Re: python sys.stdout and C++ iostreams::cout
Date Fri, 18 Jan 2013 18:21:47 +1100
References <2699150a-cf67-46ac-b863-34c92bffceaf@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host cpmon.mq.edu.au
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
In-Reply-To <2699150a-cf67-46ac-b863-34c92bffceaf@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.612.1358493723.2939.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358493723 news.xs4all.nl 6847 [2001:888:2000:d::a6]:47269
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36977

Show key headers only | View raw


On 18/01/13 02:02, Utpal Sarkar wrote:
> Hi,
>
> I was assuming that sys.stdout would be referencing the same physical stream as iostreams::cout running in the same process, but this doesn't seem to be the case.
> The following code, which makes a call to a C++ function with a python wrapper called "write", that writes to cout:
>
> from cStringIO import StringIO
> import sys
> orig_stdout = sys.stdout
> sys.stdout = stringout = StringIO()
> write("cout") # wrapped C++ function that writes to cout
> print "-" * 40
> print "stdout"
> sys.stdout = orig_stdout
> print stringout.getvalue()
>
> immediately writes "cout" to the console, then the separator "---...", and finally, as the return value of stringout.getvalue(), the string "stdout".
> My intention was to capture in stringout also the string written to cout from C++.
> Does anyone know what is going on, and if so, how I can capture what is written to cout in a python string?
>
> Thanks in advance.
>

You might have a better luck if you check std::ios::rdbuf 
(http://www.cplusplus.com/reference/ios/ios/rdbuf/)

Using std::ios::rdbuf() you can get the cout's streambuf, which is a 
filebuf for the stdout and then use std::ios::rdbuf(streambuf*) to 
replace cout's internal streambuf and replace it with your own 
implementation that captures everything written using cout before 
passing it back to the original streambuf.

This is essentially similar to assigning to sys.stdout in python.

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

python sys.stdout and C++ iostreams::cout Utpal Sarkar <doetoe@gmail.com> - 2013-01-17 07:02 -0800
  Re: python sys.stdout and C++ iostreams::cout Nobody <nobody@nowhere.com> - 2013-01-17 15:35 +0000
  Re: python sys.stdout and C++ iostreams::cout Chris Angelico <rosuav@gmail.com> - 2013-01-18 02:42 +1100
    Re: python sys.stdout and C++ iostreams::cout Utpal Sarkar <doetoe@gmail.com> - 2013-01-17 07:51 -0800
      Re: python sys.stdout and C++ iostreams::cout Chris Angelico <rosuav@gmail.com> - 2013-01-18 02:54 +1100
    Re: python sys.stdout and C++ iostreams::cout Utpal Sarkar <doetoe@gmail.com> - 2013-01-17 07:51 -0800
  Re: python sys.stdout and C++ iostreams::cout Lie Ryan <lie.1296@gmail.com> - 2013-01-18 18:21 +1100

csiph-web