[issue38819] The redirect

2019-11-15 Thread Terry J. Reedy


Terry J. Reedy  added the comment:

If you do the same in the standard python REPL, you get the same result.  So 
this behavior is not specific to IDLE, although one has to be slightly more 
careful restoring sys.stdout in IDLE.  (Restoring stdout from saved stdout 
should work the same without or with IDLE.  Restoring stdout from __stdout__ 
does not work in IDLE because IDLE redirects stdout from the original value.)

This tracker is for patching real bugs.  Questions about how to use python 
should be directed elsewhere, such as python-list.

--
assignee: terry.reedy -> 
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38819] The redirect

2019-11-15 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

If your goal is to temporarily redirect stdout, there are a couple ways to do 
it:

with open('dest1.txt', 'w') as target_file:
print('hello world', file=target_file)

or:

with open('dest1.txt', 'w') as target_file:
with contextlib.redirect_stdout(target_file)
 print('hello world')

Both of these techniques temporarily alter where printing is directed.

In contrast, the code posted above permanently alters the target unless 
explictly reset.

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38819] The redirect

2019-11-15 Thread Alex


New submission from Alex <2423067...@qq.com>:

Hi.
This bug I've found is about the redirect, you know, to redirect the standard 
input/output stream. It's not really a bug, you can think of it as a feature.
The code is like this:

import sys
sys.stdout = open('redirect.txt','w')

print('hello world!') #Something you want to print
after you run it in the IDLE:

 RESTART: XXX.py===
>>> print(1 + 1)
>>>

You will find you can't output as normal.
The version I choose for this issue is Python 3.8.0b4. However, I find this 
problem is also exist in Python 3.7.5.
How to repair it?

--
assignee: terry.reedy
components: IDLE
messages: 356742
nosy: Alex-Python-Programmer, terry.reedy
priority: normal
severity: normal
status: open
title: The redirect
type: behavior
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com