pyexe icon indicating copy to clipboard operation
pyexe copied to clipboard

bugfix: using ctypes leads to an exception and premature script exit

Open shshzi opened this issue 5 years ago • 3 comments

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.

shshzi avatar Aug 13 '19 09:08 shshzi

I'd like to have a test that fails before this PR and passes with it. You said that

import platform
print(platform.system())

fails, but it works on both appveyor and locally for me. Perhaps it varies based on Windows version? Or I am missing something.

manthey avatar Sep 23 '19 12:09 manthey

I've been running into this issue as well. It seems to happen under certain contexts so it might work on the same machine in some context, but not in another.

If this is the fix, it'd be great to have it added.

kbickar avatar Jan 26 '21 20:01 kbickar

I've had this fail while using numpy with "py36-64" on Windows 10. I created a script and ran the example from numpy.org.

import numpy as np a = np.arange(15).reshape(3,5)

This produces the same error. I'm on windows 20.04 It would be great to get the fix.

TheGrandJonsson avatar Sep 07 '21 06:09 TheGrandJonsson