Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shshzi
Copy link

@shshzi shshzi commented Aug 13, 2019

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.

@manthey
Copy link
Owner

manthey commented Sep 23, 2019

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.

@kbickar
Copy link

kbickar commented Jan 26, 2021

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.

@TheGrandJonsson
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants