Newbie: Capture output of compile_dir
Bengt Richter
bokr at oz.net
Mon Jul 15 22:35:28 EDT 2002
On Mon, 15 Jul 2002 14:04:28 -0700 (PDT), Becky Hehn <chicken7373 at yahoo.com> wrote:
>In-Reply-To: <Pine.LNX.4.44L.0207152109490.1508-100000 at ods.pp.ru>
>MIME-Version: 1.0
>Content-Type: text/plain; charset=us-ascii
>
>Thanks for the response. I tried the code you gave
>me, but it didn't quite work like I wanted. I created
>a syntax error in my python file (left out a quotation
>mark) and tried the code. Below is the information
>from my interpreter session:
>
Idle or console window?
>
>>>> import sys
>>>> from cStringIO import StringIO
>>>> from compileall import compile_dir
>>>> path='/home/hehn/test'
>>>> orig_so = sys.stdout
>>>> sys.stdout = captured = StringIO()
At this point the interactive loop is echoing your keystrokes,
so presumably it has a a private reference to the old sys.stdout
for that purpose, but if you typed
print 'hello'
the output would go to the StringIO() instance.
>>>> compile_dir(path,force=1)
Still echoing, but the next 3 lines probably went to sys.stderr
>File "/home/hehn/test/me.py", line 2
>Write('status)
>Syntax Error: invalid token
>>>> sys.stdout = orig_so
Now old stdout should be active again
>>>> output = captured.getvalue()
>>>> output
>'Listing /home/hehn/test . . . Compiling
>/home/hehn/test/me.py'
>
>
>If an the prompt I just type the compile command, I
>get all the output at once(see below). I would like
>the output variable to contain all this feedback from
>the compile_dir command. Not just the initial two
>lines. Any thoughts on what I did wrong? Thank you
>in advance.
>
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> from cStringIO import StringIO
>>> orig_so = sys.stdout
>>> orig_se = sys.stderr
>>> sys.stdout = so = StringIO()
>>> sys.stderr = se = StringIO()
>>> print '-- firt line --'
>>> 1/0
>>> print '-- 2nd line --'
>>> print '-- 3nd line but without eol --',
>>> sys.stdout = orig_so
>>> sys.stderr = orig_se
>>> so.getvalue()
'-- firt line --\n-- 2nd line --\n-- 3nd line but without eol --\n'
>>> se.getvalue()
'Traceback (most recent call last):\n File "<stdin>", line 1, in ?\nZeroDivisionError: in
teger division or modulo by zero\n'
Hm, I didn't expect a '\n' on that 3rd line. StringIO doesn't seem to define
softspace, so I wonder how you can capture an accurate copy of what would go to stdout
using print without that....
A quick experiment:
>>> class X:
... def __init__(self):
... self.s = []
... self.softspace=0
... def write(self,x): self.s.append(x)
...
>>> sox=X()
>>> sys.stdout = sox
>>> print 'hello',
>>> print 'hello',
>>> sys.stdout = orig_so
>>> sox.s
['hello', '\n', 'hello', '\n']
>>> print 'hello',;print 'hello'
hello hello
>>> sox=X()
>>> sys.stdout = sox
>>> print 'hello',;print 'hello'
>>> print 'hello',;print 'hello'
>>> sys.stdout = orig_so
>>> sox.s
['hello', ' ', 'hello', '\n', 'hello', ' ', 'hello', '\n']
>>> sox=X()
>>> sys.stdout = sox
>>> print 'hello',;sys.stdout.softspace=0;print 'hello'
>>> print 'hello',;print 'hello'
>>> sys.stdout = orig_so
>>> sox.s
['hello', 'hello', '\n', 'hello', ' ', 'hello', '\n']
Nope, seems to be looking at original sys.stdout.softspace
irrespective of redirection. And I'm still not sure that
third line is right. Why should the interactive echo to the next
line make a difference to softspace when stdout is re-directed?
Or is that what does it? It seems like a bug someplace...
BTW, running python with the -u switch might be something to try also,
to get a better handle on the sequencing of outputs to stdout and stderr.
Regards,
Bengt Richter
More information about the Python-list
mailing list