Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2012-12-19 11:27:23 +0400
committerCampbell Barton <ideasman42@gmail.com>2012-12-19 11:27:23 +0400
commitef665b3d1899669ca680400bbb0045706d784f2a (patch)
treebc5f317e9668ed4298e87c96f021a9e18173a612 /release/scripts/modules/addon_utils.py
parent5837c7f09220396f5833b7f3007a585c4962888b (diff)
dissallow access to the context while addons import and register.
Since the window manager is needed for keymaps this is kept as an exception. some addons will need updating, but in every case I've seen addons should not be accessing the context while registering. (bad stuff! - declaring the scene as a global variable - which crashes when the users loads a new file, manipulating the active object or scene... tsk tsk)
Diffstat (limited to 'release/scripts/modules/addon_utils.py')
-rw-r--r--release/scripts/modules/addon_utils.py26
1 files changed, 22 insertions, 4 deletions
diff --git a/release/scripts/modules/addon_utils.py b/release/scripts/modules/addon_utils.py
index 6bf81d73f8b..3d705a0cf79 100644
--- a/release/scripts/modules/addon_utils.py
+++ b/release/scripts/modules/addon_utils.py
@@ -29,6 +29,14 @@ __all__ = (
)
import bpy as _bpy
+_user_preferences = _bpy.context.user_preferences
+
+class _RestrictedContext():
+ __slots__ = ()
+ @property
+ def window_manager(self):
+ return _bpy.data.window_managers[0]
+_ctx_restricted = _RestrictedContext()
error_duplicates = False
@@ -201,7 +209,7 @@ def check(module_name):
:rtype: tuple of booleans
"""
import sys
- loaded_default = module_name in _bpy.context.user_preferences.addons
+ loaded_default = module_name in _user_preferences.addons
mod = sys.modules.get(module_name)
loaded_state = mod and getattr(mod, "__addon_enabled__", Ellipsis)
@@ -259,6 +267,11 @@ def enable(module_name, default_set=True, persistent=False):
# Split registering up into 3 steps so we can undo
# if it fails par way through.
+ # first disable the context, using the context at all is
+ # really bad while loading an addon, don't do it!
+ ctx = _bpy.context
+ _bpy.context = _ctx_restricted
+
# 1) try import
try:
mod = __import__(module_name)
@@ -266,6 +279,7 @@ def enable(module_name, default_set=True, persistent=False):
mod.__addon_enabled__ = False
except:
handle_error()
+ _bpy.context = ctx
return None
# 2) try register collected modules
@@ -279,14 +293,18 @@ def enable(module_name, default_set=True, persistent=False):
getattr(mod, "__file__", module_name))
handle_error()
del sys.modules[module_name]
+ _bpy.context = ctx
return None
+ # finally restore the context
+ _bpy.context = ctx
+
# * OK loaded successfully! *
if default_set:
# just in case its enabled already
- ext = _bpy.context.user_preferences.addons.get(module_name)
+ ext = _user_preferences.addons.get(module_name)
if not ext:
- ext = _bpy.context.user_preferences.addons.new()
+ ext = _user_preferences.addons.new()
ext.module = module_name
mod.__addon_enabled__ = True
@@ -327,7 +345,7 @@ def disable(module_name, default_set=True):
(module_name, "disabled" if mod is None else "loaded"))
# could be in more then once, unlikely but better do this just in case.
- addons = _bpy.context.user_preferences.addons
+ addons = _user_preferences.addons
if default_set:
while module_name in addons: