bugfix: using ctypes leads to an exception and premature script exit #47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is fixing a conflict between the scope of variables in PyInstaller's loader (pyiboot01_bootstrap.py) and Pyexe's tweaking of the globals() dictionary.
The problem can be re-produced by the following python script:
import platform
print(platform.system())
which yields the following (on python 2.7):
Traceback (most recent call last):
File "pyexe.py", line 1236, in
File "pyexe.py", line 1187, in main
File "pyexe.py", line 910, in run_file
File "site-packages\six.py", line 709, in exec_
File "", line 1, in
File "", line 6, in
File "platform.py", line 1265, in system
File "platform.py", line 1161, in uname
File "platform.py", line 637, in win32_ver
File "platform.py", line 592, in _get_real_winver
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 170, in init
NameError: global name '_frozen_name' is not defined
[13452] Failed to execute script pyexe
The problem arises because pyiboot01_bootstrap lazy/late binds variables from the global context (for example: _frozen_name() and imports, such as os). In pyexe.py, all of these variables are then removed from the globals() dictionary (in run_file()), so when some code uses ctypes.WinDLL (which is patched by pyiboot01_bootstrap), these variables cannot be resolved and an exception is emitted.
The simple fix to the problem is to encapsulate all of the relevant code to ctypes in pyiboot01_bootstrap inside a function, to avoid binding to the global context.