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


Groups > comp.lang.python > #8828

Re: embedding: how do I redirect print output?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <t@jollybox.de>
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; 'example:': 0.03; 'python.': 0.04; 'scripting': 0.05; 'constructor': 0.07; 'function,': 0.07; 'mess': 0.07; 'scripts': 0.09; 'arguments.': 0.09; 'eof': 0.09; 'interpreter.': 0.09; 'sockets': 0.09; '(0)': 0.11; 'output': 0.11; 'gui': 0.13; 'meaningful': 0.13; 'wrote:': 0.15; '"print"': 0.16; "'w')": 0.16; 'descriptors.': 0.16; 'dummy': 0.16; 'eckhardt': 0.16; 'fout': 0.16; 'low-level': 0.16; 'received:192.168.1.40': 0.16; 'stderr': 0.16; 'stdin': 0.16; 'stdout': 0.16; 'stdout:': 0.16; 'subject:print': 0.16; 'tries': 0.16; 'pm,': 0.16; 'possibly': 0.16; 'suggest': 0.17; 'seems': 0.20; 'this?': 0.22; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'interface': 0.23; 'code': 0.24; 'modules': 0.25; 'function': 0.26; "i'm": 0.27; 'work.': 0.28; 'right.': 0.28; 'bit': 0.28; 'import': 0.29; 'separate': 0.31; 'subject:?': 0.31; 'print': 0.32; 'setting': 0.32; "i'll": 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'it?': 0.34; 'there': 0.34; 'however,': 0.34; "can't": 0.34; '2.6': 0.35; 'test.': 0.35; 'here,': 0.35; "isn't": 0.35; 'skip:o 20': 0.36; 'file': 0.36; 'some': 0.37; 'but': 0.37; 'using': 0.37; 'received:192': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'something': 0.38; 'run': 0.39; 'easier': 0.39; 'received:192.168.1': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'where': 0.40; 'your': 0.60; 'received:62': 0.67; 'capabilities': 0.71; 'easy:': 0.84; 'from:addr:t': 0.84; 'window,': 0.84; 'redirect': 0.91
Date Tue, 05 Jul 2011 16:18:20 +0200
From Thomas Jollans <t@jollybox.de>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110626 Iceowl/1.0b2 Icedove/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: embedding: how do I redirect print output?
References <kkdbe8-eum.ln1@satorlaser.homedns.org>
In-Reply-To <kkdbe8-eum.ln1@satorlaser.homedns.org>
X-Enigmail-Version 1.1.2
OpenPGP id=5C8691ED
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.629.1309875877.1164.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1309875877 news.xs4all.nl 21744 [2001:888:2000:d::a6]:33517
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8828

Show key headers only | View raw


On 07/05/2011 02:50 PM, Ulrich Eckhardt wrote:
> I'm trying to add some scripting capabilities to an application. Since it is 
> a GUI application, I need some way to display output from Python. For 3.x, 
> where "print" is a function, I'd just exchange this function with one that 
> redirects the output to a log window, right.
> 
> However, I'm using 2.6 here, where that can't work. So, what I was thinking 
> was to redirect "sys.stdout" and "sys.stderr" to a log window and possibly 
> replace "sys.stdin" with something that causes meaningful errors if some 
> code tries to use it, but that last point is just icing on the cake.
> 
> What seems cumbersome is that I'll need to write something that supports the 
> file interface using C, which is still a bit awkward. I'm wondering, isn't 
> there an easier way to achieve this? How would you do it?

You can mess with low-level file descriptors. Example:

# create new stdout:
fout = file('out.txt', 'w')

# close stdout (fd 1)
import os
os.close(1)

# use fout as new stdout
os.dup2(fout.fileno(), 1)

# test. This will end up in out.txt
print "Testing."

# do the same for stdin (0) and stderr (2)

# - EOF -

However, I suggest you consider isolating your scripts from you main
program and run them in a separate interpreter. You can provide dummy
modules that communicate with your application via sockets to the
scripts. Setting the standard files in the child process is easy: the
Popen constructor takes stdin/stdout/stderr arguments.

-T

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


Thread

embedding: how do I redirect print output? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-07-05 14:50 +0200
  Re: embedding: how do I redirect print output? Thomas Jollans <t@jollybox.de> - 2011-07-05 16:18 +0200
  Re: embedding: how do I redirect print output? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-06 00:36 +1000
    Re: embedding: how do I redirect print output? Chris Angelico <rosuav@gmail.com> - 2011-07-06 01:20 +1000
    Re: embedding: how do I redirect print output? Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-05 16:04 -0500
    Re: embedding: how do I redirect print output? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-07-06 08:24 +0200

csiph-web