Skip to content

Commit

Permalink
loader: disable import tracing if sys.stderr is unavailable
Browse files Browse the repository at this point in the history
The `FrozenImporter` in `pymod03_importers` uses `trace()` function
if `sys.flags.verbose` is enabled to trace the imports to `sys.stderr`.
This results in the following error:
  `Failed to execute script pyiboot01_bootstrap`
when `sys.stderr` is unavailable (is `None`), which happens on
Windows when windowed bootloader is used in combination with
`sys.flags.verbose` enabled (i.e., `--debug imports` or
`--debug all` is passed on the command-line).

The problem is that while `pyiboot01_bootstrap` does install its
`NullWriter` for `sys.stderr` when the latter is unavailable, that
happens too late; there is an `import os` that happens between the
end of bootstrap process (the `pyimod03_importers.install()` call)
and monkey-patching `NullWriter()` into `sys.stderr`.

While the problem could also be fixed by moving the `NullWriter`
initialization before the offending import, simply disabling the
`trace()` function seems a better option.

Fixes pyinstaller#4213.
  • Loading branch information
rokm committed Jan 17, 2021
1 parent f95eb3e commit e724adb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion PyInstaller/loader/pyimod03_importers.py
Expand Up @@ -33,7 +33,7 @@
# with using type() function:
imp_new_module = type(sys)

if sys.flags.verbose:
if sys.flags.verbose and sys.stderr:
def trace(msg, *a):
sys.stderr.write(msg % a)
sys.stderr.write("\n")
Expand Down
4 changes: 4 additions & 0 deletions news/4213.bugfix.rst
@@ -0,0 +1,4 @@
(Windows) Fix the frozen program crashing immediately with
``Failed to execute script pyiboot01_bootstrap`` message when built in
``noconsole`` mode and with import logging enabled (either via
``--debug imports`` or ``--debug all`` command-line switch).

0 comments on commit e724adb

Please sign in to comment.