Fix T51444: Unit tests don't run on Windows

This commit is contained in:
Campbell Barton 2017-05-25 18:56:00 +10:00
parent bbce6ce249
commit 24e3a930f1
Notes: blender-bot 2023-02-14 06:59:12 +01:00
Referenced by issue #51444, Python scripts executed from commandline don't have __main__ module's dict as their globals on Windows
1 changed files with 12 additions and 3 deletions

View File

@ -480,11 +480,20 @@ static bool python_script_exec(
* object, but as written in the Python/C API Ref Manual, chapter 2,
* 'FILE structs for different C libraries can be different and
* incompatible'.
* So now we load the script file data to a buffer */
* So now we load the script file data to a buffer.
*
* Note on use of 'globals()', it's important not copy the dictionary because
* tools may inspect 'sys.modules["__main__"]' for variables defined in the code
* where using a copy of 'globals()' causes code execution
* to leave the main namespace untouched. see: T51444
*
* This leaves us with the problem of variables being included,
* currently this is worked around using 'dict.__del__' it's ugly but works.
*/
{
const char *pystring =
"ns = globals().copy()\n"
"with open(__file__, 'rb') as f: exec(compile(f.read(), __file__, 'exec'), ns)";
"with open(__file__, 'rb') as f:"
"exec(compile(f.read(), __file__, 'exec'), globals().__delitem__('f') or globals())";
fclose(fp);