Issue695383

classification
Title: close()-ing sys.stdin/stdout causes Jython to exit
Type: Severity: normal
Components: Core Versions:
process
Status: open Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: daishiharada, pjenvey, ssteiner
Priority: low Keywords:

Created on 2003-03-01.00:27:00 by daishiharada, last changed 2009-12-04.11:39:49 by ssteiner.

Messages
msg821 (view) Author: daishi harada (daishiharada) Date: 2003-03-01.00:27:00
The behavior that I'm seeing is:

% jython
Jython 2.1+ on java1.3.1_02 (JIT: null)
Type "copyright", "credits" or "license" for more
information.
>>> import sys
>>> sys.stdout.close()
Traceback (innermost last):
  (no code object) at line 0
ValueError: I/O operation on closed file
%

On the other hand, under CPython:

% python
Python 2.2 (#1, Feb 26 2002, 15:49:15) 
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> import sys
>>> sys.stdin.close()
>>>

I understand raising ValueError, but I can't seem to
even catch
the exception.
msg3252 (view) Author: Philip Jenvey (pjenvey) Date: 2008-06-08.04:40:46
trunk works like CPython now in this regard:

Jython 2.3a0+ (trunk:4553:4564M, Jun 7 2008, 21:19:17) 
[Java HotSpot(TM) Client VM (Apple Inc.)] on java1.5.0_13
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.close()
>>> print 'hi'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>> print 'hi'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>> try:
...     print 'hi'
... except ValueError:
...     print >> sys.stderr, 'hi'
... 
hi
>>> 

(and note the ValueError didn't occur until I tried printing something)
msg3271 (view) Author: Philip Jenvey (pjenvey) Date: 2008-06-10.23:05:44
my mistake, we're ok when sys.stdin is closed, but we still quit when 
sys.stdout is closed
msg3272 (view) Author: Philip Jenvey (pjenvey) Date: 2008-06-10.23:29:30
it looks like we end up exiting because we actually write our prompt 
'>>>' to sys.stdout. So writing to the closed stdout raises a 
ValueError, breaking us out of the interactive prompt loop (causing the 
exit)

We should actually be writing the prompt to stderr -- that will totally 
avoid the exception.

The problem with that is when readline is enabled, the only way we can 
write the prompt to stderr instead of stdout is by doing it ourselves 
(right now Readline.readline() writes the prompt for us).

This seems to break readline in some situations: for example, when 
recalling a previous line in the history (with the up arrow), readline 
for some reason doesn't realize there's a prompt at the beginning of the 
line and allows you to muck with those characters
msg4817 (view) Author: Philip Jenvey (pjenvey) Date: 2009-06-17.03:23:25
This is no longer a problem with the default JLine console but still 
happens on the pure java version
msg5346 (view) Author: simon steiner (ssteiner) Date: 2009-12-04.11:39:49
doesnt work:
sys.stderr.write('hi')

works:
print >> sys.stderr, 'entering function'
History
Date User Action Args
2009-12-04 11:39:50ssteinersetnosy: + ssteiner
messages: + msg5346
2009-06-17 03:23:26pjenveysetmessages: + msg4817
2008-12-15 16:07:45fwierzbickisetcomponents: + Core, - None
2008-06-10 23:29:31pjenveysetmessages: + msg3272
2008-06-10 23:05:45pjenveysetstatus: closed -> open
resolution: fixed -> accepted
messages: + msg3271
2008-06-08 04:40:51pjenveysetstatus: open -> closed
resolution: fixed
messages: + msg3252
nosy: + pjenvey
2003-03-01 00:27:00daishiharadacreate