This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Error in sys.excepthook on windows when redirecting output of the script
Type: crash Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, Michal.Molhanec, amaury.forgeotdarc, ezio.melotti, sijinjoseph, ssbarnea, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2010-07-27 12:51 by ssbarnea, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (10)
msg111695 - (view) Author: Sorin Sbarnea (ssbarnea) * Date: 2010-07-27 12:51
create a test.py with this content:
print("test")

run this file from command line by redirecting the output:
test.py >out.log

You get:
----
close failed in file object destructor:                                                                                                     
Error in sys.excepthook:                                                                                                                    
                                                                                                                                            
Original exception was:                
----

This does not happen if you call the script by using "python test.py >out.log"

Also this does not reproduce with Python 3.1 but it does reproduce with latest Python 2.6 and 2.7 under windows.

You can switch the registered python interpreter via registry key:

[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
@="\"C:\\lib\\Python27\\python.exe\" \"%1\" %*"
msg133019 - (view) Author: Michal Molhanec (Michal.Molhanec) Date: 2011-04-05 11:33
I've got the same problem with 2.7.1 (both 32bit and 64bit versions) under W7 SP1 64bit. Under WXP SP3 32bit it works OK. What's worse the output file is empty.

3.2 (tested 64bit version) behaves even worse -- it does not print the error like 2.7 but the resulting file is empty as well!
msg134422 - (view) Author: Sijin Joseph (sijinjoseph) Date: 2011-04-25 21:58
I was not able to reproduce this on Python 2.7 x64 or Python 3.2 x64 on Win 7 SP1.

I am curious what the output is if you just run test.py, do you still get an error?
msg134426 - (view) Author: Michal Molhanec (Michal.Molhanec) Date: 2011-04-25 23:30
Running it without redirecting output, like
c:\p\test.py
works OK
msg134451 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-04-26 11:49
This post: http://stackoverflow.com/questions/3018848/cannot-run-python-script-on-windows-with-output-redirected
suggests that there is a difference between "python test.py > out.log" and "test.py > out.log".
It also suggests a change in the registry that fixed the problem for me some months ago. Can you try it?
msg134459 - (view) Author: Sijin Joseph (sijinjoseph) Date: 2011-04-26 13:01
@Amaury - That sounds exactly like the issue described.

To summarize the error is caused because in some versions of windows there is a bug that causes the handles to stdin/stdout to not be inherited correctly for apps run from the console "via file association".

This is the kb article, http://support.microsoft.com/default.aspx?kbid=321788

In fact looks like this issue was discussed in the Python mailing list in 2004 as well, see http://mail.python.org/pipermail/python-bugs-list/2004-August/024923.html
msg134482 - (view) Author: Michal Molhanec (Michal.Molhanec) Date: 2011-04-26 17:33
Thanks, the fix looks working.
The questions are:
a) can this situation be detected at runtime to provide better error message?
b) can it be detected during the installation so that the installation program can offer to the user to set the flag (or it could be set always? are there any disadvantages in doing so?}
msg134532 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-04-27 08:19
I've seen the same question/answer on other forums, about Perl, Lua and Javascript http://stackoverflow.com/questions/3018848/cannot-run-python-script-on-windows-with-output-redirected
but nobody suggested to set this flag by default. I don't know if it can have unwanted effects.
msg223135 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-15 18:32
I can't reproduce this with 3.4.1 or 3.5.0a0.  I don't have a 2.7.x to test on, can it still be reproduced with later versions of 2.7?  Does the introduction of pylauncher impact all on this wrt registry keys?
msg223196 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-07-16 13:27
This appears to me to be a well-documented Windows bug with a clear, documented fix, and I am not able to reproduce it so I'm not sure if it's even still relevant.  Closing the issue.
History
Date User Action Args
2022-04-11 14:57:04adminsetgithub: 53636
2014-07-16 13:27:28zach.waresetstatus: open -> closed
versions: - Python 2.6
messages: + msg223196

resolution: third party
stage: resolved
2014-07-15 18:32:57BreamoreBoysetnosy: + tim.golden, BreamoreBoy, zach.ware, steve.dower
messages: + msg223135
2011-06-19 15:59:51r.david.murraylinkissue12362 superseder
2011-06-19 15:59:51r.david.murrayunlinkissue12362 dependencies
2011-06-19 15:57:09r.david.murraylinkissue12362 dependencies
2011-04-27 08:19:09amaury.forgeotdarcsetmessages: + msg134532
2011-04-26 17:33:10Michal.Molhanecsetmessages: + msg134482
2011-04-26 13:01:35sijinjosephsetmessages: + msg134459
2011-04-26 11:49:57amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg134451
2011-04-25 23:30:21Michal.Molhanecsetmessages: + msg134426
2011-04-25 21:58:46sijinjosephsetnosy: + sijinjoseph
messages: + msg134422
2011-04-06 04:21:35ezio.melottisetnosy: + ezio.melotti
2011-04-05 11:33:58Michal.Molhanecsetnosy: + Michal.Molhanec
messages: + msg133019
2010-07-27 12:51:25ssbarneacreate