Skip to content

Commit b3bb1fa

Browse files
committed
fix: resolve TypeError when patching dict.update for bpy.app.translations compatibility
- Replace complex dict.update patching with direct sys.modules manipulation - Add graceful error handling to avoid 'cannot set update attribute of immutable type dict' error - Implement dual-layer protection: sys.modules check + existing object fix - Simplify patch logic while maintaining PySide6/Shiboken compatibility - Tested with Blender 3.1+, 3.6, 4.1 + PySide6 6.5.3 This approach eliminates the TypeError that occurred in restricted Python environments while preserving the compatibility fixes for bpy.app.translations missing attributes.
1 parent 714e915 commit b3bb1fa

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

bqt/__init__.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,34 @@ def __setattr__(self, name, value):
3737
setattr(self._orig, name, value)
3838

3939
def __dir__(self):
40-
orig_attrs = dir(self._orig) if hasattr(self._orig, '__dir__') else []
41-
extra_attrs = ['__name__', '__module__', '__qualname__', '_ne_', '__doc__']
40+
orig_attrs = (dir(self._orig)
41+
if hasattr(self._orig, '__dir__') else [])
42+
extra_attrs = ['__name__', '__module__', '__qualname__',
43+
'_ne_', '__doc__']
4244
return list(set(orig_attrs + extra_attrs))
4345

4446
return TranslationsWrapper(original)
4547

4648

47-
def _patch_sys_modules():
48-
"""Patch sys.modules to intercept bpy.app.translations registration"""
49-
original_update = dict.update
50-
51-
def patched_update(self, *args, **kwargs):
52-
result = original_update(self, *args, **kwargs)
53-
54-
# Check if bpy.app.translations was just registered
55-
if "bpy.app.translations" in self:
56-
translations = self["bpy.app.translations"]
49+
def _patch_bpy_translations_in_sys_modules():
50+
"""Directly patch bpy.app.translations in sys.modules if it exists"""
51+
try:
52+
# Check if bpy.app.translations is already in sys.modules
53+
if "bpy.app.translations" in sys.modules:
54+
translations = sys.modules["bpy.app.translations"]
5755
required_attrs = ['__name__', '__module__', '__qualname__', '_ne_']
5856

5957
# Check if any required attributes are missing
6058
if any(not hasattr(translations, attr) for attr in required_attrs):
6159
try:
62-
self["bpy.app.translations"] = _create_translations_wrapper(translations)
60+
wrapped = _create_translations_wrapper(translations)
61+
sys.modules["bpy.app.translations"] = wrapped
62+
return True
6363
except Exception:
6464
pass
65-
66-
return result
67-
68-
dict.update = patched_update
69-
return original_update
65+
return False
66+
except Exception:
67+
return False
7068

7169

7270
def _fix_existing_translations():
@@ -106,7 +104,7 @@ def _fix_existing_translations():
106104

107105

108106
# Apply patches immediately - order matters!
109-
_patch_sys_modules() # Patch sys.modules first to catch future registrations
107+
_patch_bpy_translations_in_sys_modules() # Check sys.modules first
110108
_fix_existing_translations() # Fix existing translations if available
111109

112110
import bqt

0 commit comments

Comments
 (0)