Python scripts executed from commandline don't have __main__ module's dict as their globals on Windows #51444

Closed
opened 2017-05-09 15:54:13 +02:00 by Kerim Borchaev · 7 comments

System Information
Windows 7 & W8100

Blender Version
Broken: 2.78c

Short description of error
When a python file is executed with --python it's namespace is different from __main__ module's namespace. This, for example, prevents unittest.main() from collecting tests(unittest.main collects tests by importing __main__ module). So any tests scripts(including Blender's python tests) relying on unittest.main behavior are now passing, having collected 0 Tests.

Exact steps for others to reproduce the error
Here's the test file, that shows that namespace is not __main__'s - test.py
running it (blender.exe --background --python test.py) gives this:

['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'f', 'ns']

----------------------------------------------------------------------
Ran 0 tests in 0.000s

Cause and fix

On Windows globals() is copied for some reason before being passed to exec. Removing copy() fixes the problem:

---
 source/blender/python/intern/bpy_interface.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 55e477b0214..9cfa98b12b1 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -483,7 +483,7 @@ static bool python_script_exec(
             * So now we load the script file data to a buffer */
            {
                const char *pystring =
-                       "ns = globals().copy()\n"
+                       "ns = globals()\n"
                        "with open(__file__, 'rb') as f: exec(compile(f.read(), __file__, 'exec'), ns)";
 
                fclose(fp);
-- 

**System Information** Windows 7 & W8100 **Blender Version** Broken: 2.78c **Short description of error** When a python file is executed with --python it's namespace is different from `__main__` module's namespace. This, for example, prevents unittest.main() from collecting tests(unittest.main collects tests by importing `__main__` module). So any tests scripts(including Blender's python tests) relying on unittest.main behavior are now passing, having collected **0 Tests**. **Exact steps for others to reproduce the error** Here's the test file, that shows that namespace is not `__main__`'s - [test.py](https://archive.blender.org/developer/F593316/test.py) running it (`blender.exe --background --python test.py`) gives this: ``` ['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'f', 'ns'] ---------------------------------------------------------------------- Ran 0 tests in 0.000s ``` **Cause and fix** On Windows globals() is copied for some reason before being passed to `exec`. Removing copy() fixes the problem: ``` --- source/blender/python/intern/bpy_interface.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 55e477b0214..9cfa98b12b1 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -483,7 +483,7 @@ static bool python_script_exec( * So now we load the script file data to a buffer */ { const char *pystring = - "ns = globals().copy()\n" + "ns = globals()\n" "with open(__file__, 'rb') as f: exec(compile(f.read(), __file__, 'exec'), ns)"; fclose(fp); -- ```
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @KerimBorchaev

Added subscriber: @KerimBorchaev

Added subscribers: @ideasman42, @Sergey

Added subscribers: @ideasman42, @Sergey
Campbell Barton was assigned by Sergey Sharybin 2017-05-15 12:38:14 +02:00

@ideasman42, afraid you are the only one to knwo why globals are being copied. Mind having a look here? :)

@ideasman42, afraid you are the only one to knwo why globals are being copied. Mind having a look here? :)

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'

This issue was referenced by 24e3a930f1

This issue was referenced by 24e3a930f14a7bfc1ea16350b4e2c9585aa0f7c3

The problem was caused by unittest module inspecting sys.modules[__name__] - executing with globals().copy() for globals meant that classes defined in the code weren't added to sys.modules[__name__] for unittest to discover.

Committed this change with a minor kludge to avoid f variable being declared for every script being executed.

The problem was caused by `unittest` module inspecting `sys.modules[__name__]` - executing with `globals().copy()` for globals meant that classes defined in the code weren't added to `sys.modules[__name__]` for `unittest` to discover. Committed this change with a minor kludge to avoid `f` variable being declared for every script being executed.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#51444
No description provided.