[Bf-blender-cvs] [9d134104e47] blender-v2.93-release: Fix T88899: `__file__` not set for `text.as_module()`

Campbell Barton noreply at git.blender.org
Wed Jun 9 08:54:45 CEST 2021


Commit: 9d134104e476fbaf41617971bbacd4cf820881d1
Author: Campbell Barton
Date:   Mon Jun 7 14:04:26 2021 +1000
Branches: blender-v2.93-release
https://developer.blender.org/rB9d134104e476fbaf41617971bbacd4cf820881d1

Fix T88899: `__file__` not set for `text.as_module()`

===================================================================

M	release/scripts/modules/bpy_types.py

===================================================================

diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py
index 4ea8c88e8d9..6222692cf38 100644
--- a/release/scripts/modules/bpy_types.py
+++ b/release/scripts/modules/bpy_types.py
@@ -560,9 +560,17 @@ class Text(bpy_types.ID):
         self.write(string)
 
     def as_module(self):
-        from os.path import splitext
+        import bpy
+        from os.path import splitext, join
         from types import ModuleType
-        mod = ModuleType(splitext(self.name)[0])
+        name = self.name
+        mod = ModuleType(splitext(name)[0])
+        # This is a fake file-path, set this since some scripts check `__file__`,
+        # error messages may include this as well.
+        # NOTE: the file path may be a blank string if the file hasn't been saved.
+        mod.__dict__.update({
+            "__file__": join(bpy.data.filepath, name),
+        })
         # TODO: We could use Text.compiled (C struct member)
         # if this is called often it will be much faster.
         exec(self.as_string(), mod.__dict__)



More information about the Bf-blender-cvs mailing list