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
Issue 3037 scripts must not share globals #3038
base: develop
Are you sure you want to change the base?
Issue 3037 scripts must not share globals #3038
Conversation
@codewarrior0
This script is the second one (the first one is pyiboot01_bootstrap):
So it looks like the module is not properly set up by |
2a992ba
to
58707f8
Compare
This will ensure each scripts will get its own global variables. - Move the creation of the '__main__' module into the loop and remove this module from sys.modules after the script has been executed. - Use PyImport_ExecCodeModule() for executing the scripts. - No longer set the __file__ attribute (now done by PyImport_ExecCodeModule). Using PyImport_AddModule within the loop did not work: For the second script, '__builtins__' have not been set, so even a simple import failed. (See pull-request pyinstaller#3038 for details.)
Updated the pull-request. What I learned meanwhile: The test passes (will, the one I tried) when adding So I switched to a higher level import function ( After finishing this work, I discovered that Ups, and some other tests fail, too. :-( |
This will ensure each scripts will get its own global variables. - Move the creation of the '__main__' module into the loop and remove this module from sys.modules after the script has been executed. - Use PyImport_ExecCodeModule() for executing the scripts. - No longer set the __file__ attribute (now done by PyImport_ExecCodeModule). Using PyImport_AddModule within the loop did not work: For the second script, '__builtins__' have not been set, so even a simple import failed. (See pull-request pyinstaller#3038 for details.)
This is required since now the bootloader doesn't set the `__file__` attribute from the archive name anymore, but leaves this to `PyImport_ExecCodeModule`, which looks at code.co_filename. Hopefully this will not give us another unicode error.
Given the latest changes, calculating the filename is no longer necessary. Remove it and change the `VS()` call, so the output keeps the same.
Update wording and move to better place.
This was unnoticed since while all scripts shared the same global variables since the import was done by some bootstrap script running before.
Given the latest changes, pyiboot01_bootstrap's globals are gone (more precisely None) after the script has finished. But the hooked classes still reference to them (including 'sys' and 'os'). I decided to make this into a module instead of more complicated code like mix-in-classes and lot of import statements.
58707f8
to
97e200c
Compare
Next iteration: I fixed some other test-cases and found the reason why This will also hit the run-time hooks, so we should think about a different solution. Maybe the script's module should be given a different name in Any ideas? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be overkill for what is trying to be done.
if (!code) { | ||
FATALERROR("Failed to unmarshal code object for %s\n", ptoc->name); | ||
PI_PyErr_Print(); | ||
return -1; | ||
} | ||
/* Run it */ | ||
retval = PI_PyEval_EvalCode(code, main_dict, main_dict); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to remove this function? Seems like each iteration of the loop can be this:
main_dict_copy = PyDict_Copy(main_dict)
Py_INCREF(main_dict_copy)
retval = PI_PyEval_EvalCode(code, main_dict_copy, main_dict_copy);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be an idea to try. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not work either :-( Same effect as with my code.
PI_PyErr_Print(); | ||
/* If the error was SystemExit, PyErr_Print calls exit() without | ||
* returning. So don't print "Failed to execute" on SystemExit. */ | ||
FATALERROR("Failed to execute script %s\n", ptoc->name); | ||
return -1; | ||
} | ||
Py_DECREF(module); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, how can you decref the module and then complain when you can no longer access its attributes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect the code to reference the dict, too. But anyway, I also tried not decrementing the reference count here with no success. But I'll double-check.
@htgoebel status on this? |
@Legorooj THis still needs to be solved :-( |
81c364b
to
3a8eccd
Compare
Move the ctypes hooks from bootstrap script into separate module (pymod04_ctypes). The main motivation for this is to prevent the modifications to global namespace from affecting the hook ctypes hooks code; speficially, if user decides to use `sys` or `os` as variables in the global namespace, that should not effect the ctype hook function _frozen_name(), which treats `sys` and `os` as names modules (which were bound to those names when the bootstrap script was executing). Fixes pyinstaller#5797. This commit is a cleaned-up and modernized version of an earlier commit from pyinstaller#3038 (daf60a6). Co-authored-by: Hartmut Goebel <h.goebel@crazy-compilers.com>
Move the ctypes hooks from bootstrap script into separate module (pymod04_ctypes). The main motivation for this is to prevent the modifications to global namespace from affecting the ctypes hooks code; specifically, if user decides to use `sys` or `os` as variables in the global namespace, that should not effect the ctype hook function _frozen_name(), which treats `sys` and `os` as names of modules (which were bound to those names when the bootstrap script was executed). Fixes pyinstaller#5797. This commit is a cleaned-up and modernized version of an earlier commit from pyinstaller#3038 (daf60a6). Co-authored-by: Hartmut Goebel <h.goebel@crazy-compilers.com>
Move the ctypes hooks from bootstrap script into separate module (pymod04_ctypes). The main motivation for this is to prevent the modifications to global namespace from affecting the ctypes hooks code. Specifically, if user decides to use `sys` or `os` as variables in the global namespace, that should not effect the ctypes hook function _frozen_name(), which treats `sys` and `os` as names of modules (which were bound to those names when the bootstrap script was executed). Fixes pyinstaller#5797. This commit is a cleaned-up and modernized version of corresponding commit from pyinstaller#3038 (daf60a6). Co-authored-by: Hartmut Goebel <h.goebel@crazy-compilers.com>
Move the ctypes hooks from bootstrap script into separate module (pymod04_ctypes). The main motivation for this is to prevent the modifications to global namespace from affecting the ctypes hooks code. Specifically, if user decides to use `sys` or `os` as variables in the global namespace, that should not effect the ctypes hook function _frozen_name(), which treats `sys` and `os` as names of modules (which were bound to those names when the bootstrap script was executed). Fixes pyinstaller#5797. This commit is a cleaned-up and modernized version of corresponding commit from pyinstaller#3038 (daf60a6). Co-authored-by: Hartmut Goebel <h.goebel@crazy-compilers.com>
Move the ctypes hooks from bootstrap script into separate module (pymod04_ctypes). The main motivation for this is to prevent the modifications to global namespace from affecting the ctypes hooks code. Specifically, if user decides to use `sys` or `os` as variables in the global namespace, that should not effect the ctypes hook function _frozen_name(), which treats `sys` and `os` as names of modules (which were bound to those names when the bootstrap script was executed). Fixes #5797. This commit is a cleaned-up and modernized version of corresponding commit from #3038 (daf60a6). Co-authored-by: Hartmut Goebel <h.goebel@crazy-compilers.com>
This is an attempt to solve #3037. This moves the creation of the
__main__
module into the loop and removes this module from sys.modules after the script has been executed.