Why can't I restore my redirected stdout?
David Bolen
db3l at fitlinxx.com
Thu Jun 8 18:31:03 EDT 2000
More information about the Python-list mailing list
Thu Jun 8 18:31:03 EDT 2000
- Previous message (by thread): Why can't I restore my redirected stdout?
- Next message (by thread): Why can't I restore my redirected stdout?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Raymond Ng Tong Leng" <think2 at pd.jaring.my> writes: > Remco Gerlich wrote in message ... > > > >I just typed this in, and it worked fine! Something else must be happening. > >Are you in idle or so? Does it work inside a normal python interpreter? > >What's your platform? > > > > Yes, I was using IDLE 0.4 with Python 1.5.2 on Win NT. When I tried it > inside a normal Python interpreter like you suggested, it worked fine. I > hope Guido fixed this bug... sorry, improved on this feature :) in the > latest version of IDLE. What's happening in IDLE is that sys.stdout is already being re-routed (so it can be displayed in the window after being colored and such), but sys.__stdout__ is still the true original stdout. For example, the following is from an IDLE window: >>> import sys >>> print sys.stdout <PyShell.PseudoFile instance at 85d5a0> >>> print sys.__stdout__ <open file '<stdout>', mode 'w' at 792980> And in the case of IDLE, it's running as a Windows application that doesn't actually have a console, so the true <stdout> file doesn't go anywhere. Thus when you try to "restore" to sys.__stdout__ you aren't really restoring the initial IDLE output file, but a pointer to a console that technically doesn't exist (at least not with a window). I guess by definition, this is probably the "right" thing to do, since sys.__stdout__ is supposed to be the true original stdout when the Python application was started, and even IDLE is just a Python application. So it probably depends on your view of how closely IDLE should emulate running a textual Python in a real console. As one workaround, if you want your original approach (restoring to sys.__stdout__) to work, you could try re-assigning sys.__stdout__ when you start up your IDLE session, e.g.: >>> sys.__stdout__ = sys.stdout which works in a quick test, but I noticed that the PyShell class in IDLE uses sys.__stdout__ for its own restoration, so I don't know if there might be a later gotcha. Where IDLE redirects sys.stdout is in PyShell.py, so you could tweak that to do whatever you want if you really wanted different behavior. Note that you can see other places in IDLE where sys.stdout is temporarily redirected and then restored. What's probably a better, more general approach, is to just save sys.stdout yourself and restore it later. If you're building a module that may be imported from other modules that's actually the safer approach anyway, since in that case you'd want to restore the output to what sys.stdout was before you re-routed it, which may or may not be the same as the very original sys.stdout. -- -- David -- /-----------------------------------------------------------------------\ \ David Bolen \ E-mail: db3l at fitlinxx.com / | FitLinxx, Inc. \ Phone: (203) 708-5192 | / 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \ \-----------------------------------------------------------------------/
- Previous message (by thread): Why can't I restore my redirected stdout?
- Next message (by thread): Why can't I restore my redirected stdout?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list