From f74d7ddea9ad9864440ac074df4493b5a0834f60 Mon Sep 17 00:00:00 2001 From: Rok Mandeljc Date: Thu, 28 Jan 2021 01:22:49 +0100 Subject: [PATCH] loader: disable import tracing if sys.stderr is unavailable (#5477) 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 #4213. --- PyInstaller/loader/pyimod03_importers.py | 2 +- news/4213.bugfix.rst | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 news/4213.bugfix.rst diff --git a/PyInstaller/loader/pyimod03_importers.py b/PyInstaller/loader/pyimod03_importers.py index 948bd9c780..40931371f8 100644 --- a/PyInstaller/loader/pyimod03_importers.py +++ b/PyInstaller/loader/pyimod03_importers.py @@ -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") diff --git a/news/4213.bugfix.rst b/news/4213.bugfix.rst new file mode 100644 index 0000000000..14a101416c --- /dev/null +++ b/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).