[Python-3000] python 3 closes sys.stdout

Amaury Forgeot d'Arc amauryfa at gmail.com
Sun Aug 26 23:23:37 CEST 2007


Hello,

It seems that the new I/O system closes the 3 standard descriptors
(stdin, stdout and stderr) when the sys module is unloaded.

I don't know if it is a good thing on Unix, but on Windows at least,
python crashes on exit, when call_ll_exitfuncs calls fflush(stdout)
and fflush(stderr).

As a quick correction, I changed a test in _fileio.c::internal_close():

Index: Modules/_fileio.c
===========================================
--- Modules/_fileio.c        (revision 57506)
+++ Modules/_fileio.c        (working copy)
@@ -45,7 +45,7 @@
 internal_close(PyFileIOObject *self)
 {
        int save_errno = 0;
-       if (self->fd >= 0) {
+       if (self->fd >= 3) {
                int fd = self->fd;
                self->fd = -1;
                Py_BEGIN_ALLOW_THREADS

OTOH, documentation of io.open() says
"""
    (*) If a file descriptor is given, it is closed when the returned
    I/O object is closed.  If you don't want this to happen, use
    os.dup() to create a duplicate file descriptor.
"""

So a more correct change would be to dup the three sys.stdout,
sys.stdin, sys.stderr, in site.py: installnewio()
(BTW, the -S option is broken. You guess why)

What are the consequences of a dup() on the standard descriptors?
I don't like the idea of sys.stdout.fileno() to be different than 1. I
know some code using the numbers 0,1,2 to refer to the standard files.

Or we could change the behaviour to "If a file descriptor is given, it
won't be closed". You opened it, you close it.

What do you think?

-- 
Amaury Forgeot d'Arc


More information about the Python-3000 mailing list