New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
abort when stderr is closed #51360
Comments
bash$ python3.1 -c 'pass' 2>&- (I verified, the core dump belongs to python.) If you remove the redirection thingy at the end, it works. Not sure why I ever wrote that code, but it has been working since |
The problem is the check_fd in _fileio.c checks fstat of 2, which |
After some searching around with Google, "2>&-" means "close file |
Please note that normally an error message is output, but of course it It's clearer if you close stdout instead: $ ./python -c 'pass' >&-
Fatal Python error: Py_Initialize: can't initialize sys standard streams
OSError: [Errno 9] Bad file descriptor
Abandon If we want to allow for closed {stdin, stdout, stderr}, I'm not sure Under Python 2.x, you don't get a crash but the behaviour is quite $ python -c 'print 1' >&-
close failed in file object destructor:
Error in sys.excepthook: Original exception was: |
Is it even possible to portably test the validity of a file descriptor When I first saw this bug, my gut feeling was "well, don't do that Python 2 will raise IOError: Bad File Descriptor when the user tries to I just tested pythonw.exe. If I set sys.stderr by hand to a file, then |
|
For what it's worth, the code in question is used here (using "import This is obviously a completely gratuitous variant on 2>/dev/null, but it Nevertheless, I think Python shouldn't core dump. It may choose to exit Check this for possible alternative behaviors: $ ls 1>&-
ls: write error: Bad file descriptor
($? = 2)
$ ls 1>&- 2>&-
($? = 2, no output) |
sorry, title restored! |
I would say it should be a *pseudo*-file object which always throws a *descriptive* error. Note that setting sys.stdout to None makes print() do nothing rather than report an error:
See also bpo-6501. |
On the second thought, as long as python used fd 2 as the "message stream of last resort", we should probably not allow it to run with fd 2 closed. The problem is that in this case fd 2 may become associated with a very important file contents of which you don't want to see replaced with a python error message. |
That's an interesting point. Do you know of places where we use fd 2 instead of sys.stderr? |
We normally don't. One reason is that buffering inside sys.stderr can |
On Thu, Feb 3, 2011 at 2:44 PM, Antoine Pitrou <report@bugs.python.org> wrote:
This is the case that I had in mind. What does non-debug build do on |
Hmm, grep "fprintf(stderr," returned 122 hits in the py3k branch. |
It uses fprintf(stderr, ...). That's the only thing it can do (there's
I think you can guess the answer :)
I don't think so. One more important use case is when running a Unix Perhaps we should provide a sys function to fclose() C std{in,out,err}. |
Le jeudi 03 février 2011 à 19:59 +0000, Alexander Belopolsky a écrit :
"grep -C2" seems to say most of them are. I haven't done a survey. |
On Thu, Feb 3, 2011 at 11:56 AM, Alexander Belopolsky
I don't think closing stderr via the command line is an important use |
I just took a look at http://pypi.python.org/pypi/python-daemon/, and it |
On Thu, Feb 3, 2011 at 12:18 PM, Antoine Pitrou <report@bugs.python.org> wrote:
I'm more worried about the case where a daemon launches python. At startup, could we check that 2 and 3 are valid file descriptors, |
Attached patch allows Python to run even if no standard stream is available. I use dup() to detect whether a fd is valid. |
(No Rietveld link): +is_valid_fd(int fd) Why not use fstat() instead (does Windows have fstat()? And dup()?). + @unittest.skipIf(os.name == 'nt', "test needs POSIX semantics") It would maybe be more direct with skipUnless(os.name == 'posix'). Finally, it's not that important, but it could maybe be possible to factorize the code, i.e. make a helper function that takes a list of streams and defines the preexec() function and code to test those streams, and then just call: def test_no_stdin(self):
out, err = self._test_with_closed_streams(['stdin'])
[...]
def test_no_streams(self):
out, err = self._test_with_closed_streams(['stdin', 'stdout', 'stderr'])
[...] |
Windows has dup(), but no fstat().
Hmm, indeed.
Ah, indeed perhaps. |
Updated patch. |
LGTM. |
New changeset f15943505db0 by Antoine Pitrou in branch '3.2': New changeset c86fb10eaf68 by Antoine Pitrou in branch 'default': |
Thanks, committed. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: