[Python-bugs-list] [ python-Bugs-416508 ] Reproductible parser crash with os.fdopen()

noreply@sourceforge.net noreply@sourceforge.net
Thu, 26 Apr 2001 11:00:24 -0700


Bugs item #416508, was updated on 2001-04-16 13:08
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=416508&group_id=5470

>Category: Extension Modules
>Group: Not a Bug
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: Reproductible parser crash with os.fdopen()

Initial Comment:
Hi there!

Just found this bugs, fully reproductible.
They are all the similar, derived from os.fdopen() as you will see.


Copy/paste is much better than any words, so (note: ';' is my bash prompt):

; python
Python 2.0 (#2, Feb  3 2001, 18:06:18)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import os
>>> infd = os.fdopen(0)
>>> infd = os.fdopen(0)
>>>
;

You can put it in a try..except, changing fdopen parameters, etc; but the same
happens.
A weird thing also happens if you try to do it with fd 1 instead of 0, it seems
to hung expecting for input... but ^C gives KeyboardInterrupt, see:

; python
Python 2.0 (#2, Feb  3 2001, 18:06:18)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import os
>>> infd = os.fdopen(1)
>>> infd = os.fdopen(0)
		<--- ^C, then:
KeyboardInterrupt
		<--- ^C again
KeyboardInterrupt
		<--- ^D (EOF), and got prompt.
;

The same if you try to fdopen(2) any number of times (as opposed with regular
files, when you can do it only two times before the exception, see below), and
then fdopen(1) _TWICE_; except that ^C has no effect here.


Now... if we try with a regular file:

>>> import os
>>> fd = open('/var/log/messages')
>>> fd.fileno()
>>> 3
>>> infd = os.fdopen(3)		# first call, ok
>>> infd = os.fdopen(3)		# second call, ok
>>> infd = os.fdopen(3)		# third call, exception
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   OSError: [Errno 9] Bad file descriptor
>>> infd = os.fdopen(3)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   OSError: [Errno 9] Bad file descriptor
>>>

And the interpreter is still fully functional, i can even del(infd)
The behaviour with fd 2 is also sane.

Well, any questions or testing you need, just ask me at
albertogli@altavista.net.

Thanks¸
		Alberto



----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2001-04-16 14:02

Message:
Logged In: YES 
user_id=6656

in the first example, the second assignment to infd deletes 
the file object which closes fd 0, i.e. standard input, so 
python thinks you're done and exits.  this only affects 
interactive sessions.


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=416508&group_id=5470