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.

Title: bdist_wininst installers may terminate with "close failed in file object destructor:\nsys.excepthook is missing\nlost sys.stderr"
Type: behavior Stage: resolved
Components: Distutils Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: eric.araujo Nosy List: eric.araujo, loewis, mhammond, python-dev, tarek, vstinner
Priority: normal Keywords:

Created on 2012-07-11 00:08 by mhammond, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg165213 - (view) Author: Mark Hammond (mhammond) * (Python committer) Date: 2012-07-11 00:08
Note the error message in the title is only for Python 2.x - Python 3.x shows an empty string instead, but otherwise seems identical.

This was first brought to my attention via  I can reproduce it (see below) with the pywin32 installer but believe the problem will exist with any such installer.  All tests done on a Win7x64 VM.  Symptoms are:

* Run the installer with UAC enabled - reliably works.  All "print" statements from the post-install script are shown in the final dialog.

* Disable UAC.  The installation reliably ends with the "close failed..." message shown in the final dialog.  No print output is shown.  The installation did actually complete (but is likely to fail if the output fills stdout buffering).  The UAC is probably relevant as when UAC is enabled, the installer re-spawns itself with elevation.

* The problem with UAC disabled can be reproduced running in the VS debugger.  The problem is that Python sees stdout and stderr with a FILE object that has a fileno of -2 (ie, of _NO_CONSOLE_FILENO).  Then, when Python is finalizing it tries to close these handles and fails due to the invalid fileno.  The other messages are a consequence of the fact stderr itself is what is being closed (ie, Python is trying to report a problem closing stderr to stderr.)  Python 3 seems to handle this case better courtesy of issue 1415, but the fact the output is lost remains in both versions.

* Looking over the installer code, I notice that Python is initialized and terminated twice to compile .pyc/.pyo, then initialized a third time in preparation to run the install script - the handles are redirected prior to initializing the 3rd time.  On a hunch, I hacked the code to perform the redirection *before* Python is initialized for the first time.  This solved the problem when run under the debugger but still failed when run stand-alone (*sigh*).  I fiddled with some params to CreateFile in the redirection code but could not get things working.  Note that avoiding the multiple init/finalize calls may be problematic as Py_OptimizeFlag is set differently between runs.

* I looked over the CRT init code to try and determine why _NO_CONSOLE_FILENO was being used.  It appears the CRT attempts to use the process STARTUPINFO and only falls back to GetStdHandle if that STARTUPINFO doesn't provide valid handles.  So I tried both using GetStartupInfo and putting the redirect handles in the result, and explicitly closing the old std handles, but neither of them worked.

Next step is probably to arrange to step through the CRT init code to see what is going on there.  I doubt I will get back to this any time soon, so this is my attempt at detailing what I've discovered to date incase someone else has a look...
msg166310 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-07-24 16:51
New changeset f315cfa22630 by Eli Bendersky in branch 'default':
Issue #15321: update PyPI upload doc to say --no-raw passed to Patch by Chris Jerdonek
msg166311 - (view) Author: Eli Bendersky (eli.bendersky) * (Python committer) Date: 2012-07-24 17:01
Sorry about the commit message. Ignore it, I got the issue number wrong :)
msg167178 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-08-01 23:57
New changeset 014b36383a54 by Eli Bendersky in branch '3.2':
Issue #15321: update PyPI upload doc to say --no-raw passed to
msg384828 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-01-11 12:52
The distutils bdist_wininst command has been removed in Python 3.10: see bpo-42802.
Date User Action Args
2022-04-11 14:57:32adminsetgithub: 59526
2021-01-11 12:52:25vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg384828

resolution: wont fix
stage: resolved
2012-08-01 23:57:42python-devsetmessages: + msg167178
2012-07-24 17:01:15eli.benderskysetnosy: - eli.bendersky
2012-07-24 17:01:09eli.benderskysetnosy: + eli.bendersky
messages: + msg166311
2012-07-24 16:51:48python-devsetnosy: + python-dev
messages: + msg166310
2012-07-11 00:09:28pitrousetnosy: + loewis
2012-07-11 00:08:28mhammondcreate