Could Emacs be rewritten in Python?

Robin Munn rmunn at pobox.com
Wed Apr 16 14:39:37 EDT 2003


Carl Banks <imbosol-1050441179 at aerojockey.com> wrote:
> Robin Munn wrote:
>> I suppose I just illustrated how a try ... finally block might be
>> considered "broken and fragile". I tried to use it, and missed some
>> subtleties. I'm still not convinced that a "with"-like statement is
>> necessary in Python, but I think I just went from -1 to -0 on it.
> 
> I wouldn't be so hasty.
> 
> First, you're basing it on a bad example.  Rebinding sys.stdout is bad
> programming, period.  Wanting to save and restore it is even worse.

Since Alex Martelli gave such a lengthy response (thanks Alex!), I'll
limit myself to a counterexample: pipes. I would call using Unix pipes
"rebinding stdout", handled by the shell. What I was trying to do in my
example was do something like pipes, purely in Python. Specifically,
I've got this function that prints something to sys.stdout. And this
function is not under my control. It could be something in the standard
library, or it could be a hook or a callback which will be written later
by someone else -- wherever it comes from, I can't go diving into the
function and change all occurrences of "sys.stdout.write('foo')" to
"myfileobject.write('foo')". But I need to capture the output of that
program for processing (maybe to display it in a GUI window). If it was
a separate process, I'd use a pipe or os.popen(). But it's a Python
function. Therefore I want to rebind sys.stdout temporarily.

How would you suggest capturing output *without* rebinding sys.stdout?
If there's an alternative, I don't know about it and would welcome a
chance to learn a new approach to the problem.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list