Skip to content

Commit 4b1a80d

Browse files
committed
Adjust injector module
1. ensure python 3.13 compatibility with regards to `__file__` vs. `__spec__` 2. ensure module produces same behavior on all available plugin_hosts 3. remove writing `__spec__` as it has no effect. Final value always contains information from original injector module. 4. as no sublime API is required, install injector module at import time so it is eventually available earlier. 5. ensure injector module is also re-installed in cases, the original one causes import errors in package_disabler module, auto-repair so to say.
1 parent 76ddb06 commit 4b1a80d

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

package_control/bootstrap.py

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ def bootstrap():
4646
by `plugin_loaded()` hook.
4747
"""
4848

49-
_install_injectors()
50-
5149
if not os.path.exists(LOADER_PACKAGE_PATH):
5250
# Start shortly after Sublime starts so package renames don't cause errors
5351
# with key bindings, settings, etc. disappearing in the middle of parsing
@@ -131,25 +129,28 @@ def _install_injectors():
131129
Makes sure the module injectors are in place
132130
"""
133131

134-
injector_code = R"""
132+
injector_code = R'''
133+
"""
134+
Public Package Control API
135+
"""
135136
import os
136137
import sys
137138
import zipfile
138139
139140
import sublime_plugin
140141
141-
__data_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
142-
142+
# python 3.13 may no longer provide __file__
143+
__data_path = os.path.dirname(os.path.dirname(os.path.dirname(
144+
__spec__.origin if hasattr(globals(), '__spec__') else __file__))
145+
)
143146
__pkg_path = os.path.join(__data_path, 'Packages', 'Package Control', 'package_control')
144147
__zip_path = os.path.join(__data_path, 'Installed Packages', 'Package Control.sublime-package')
145-
146148
__code = None
147149
148150
# We check the .sublime-package first, since the sublime_plugin.ZipLoader deals with overrides
149151
if os.path.exists(__zip_path):
150152
__pkg_path = os.path.join(__zip_path, 'package_control')
151153
__file_path = os.path.join(__pkg_path, '__init__.py')
152-
153154
__loader__ = sublime_plugin.ZipLoader(__zip_path)
154155
155156
try:
@@ -162,7 +163,7 @@ def _install_injectors():
162163
except (OSError, KeyError):
163164
pass
164165
165-
# may be required before Package Control has been loaded
166+
# required for events to be available on plugin_host Package Control is not running on
166167
events = sys.modules.get('package_control.events')
167168
if events is None:
168169
events = __loader__.load_module("Package Control.package_control.events")
@@ -174,7 +175,6 @@ def _install_injectors():
174175
from importlib.machinery import SourceFileLoader
175176
176177
__file_path = os.path.join(__pkg_path, '__init__.py')
177-
178178
__loader__ = SourceFileLoader('package_control', __file_path)
179179
180180
try:
@@ -183,7 +183,7 @@ def _install_injectors():
183183
except (OSError):
184184
pass
185185
186-
# may be required before Package Control has been loaded
186+
# required for events to be available on plugin_host Package Control is not running on
187187
events = sys.modules.get('package_control.events')
188188
if events is None:
189189
events = SourceFileLoader('events', os.path.join(__pkg_path, 'events.py')).load_module()
@@ -198,36 +198,25 @@ def _install_injectors():
198198
199199
__file__ = __file_path
200200
__package__ = 'package_control'
201-
__path__ = [__pkg_path]
201+
__path__ = []
202+
__data = {}
203+
exec(__code, __data)
204+
globals().update(__data)
202205
203-
# initial cleanup
206+
# cleanup temporary globals
207+
del globals()['__cached__']
208+
del globals()['__code']
209+
del globals()['__data']
210+
del globals()['__data_path']
204211
del globals()['__f']
205212
del globals()['__file_path']
206-
del globals()['__zip_path']
207213
del globals()['__pkg_path']
208-
del globals()['__data_path']
214+
del globals()['__zip_path']
215+
del globals()['os']
209216
del globals()['sublime_plugin']
210-
del globals()['zipfile']
211217
del globals()['sys']
212-
del globals()['os']
213-
214-
__data = {}
215-
exec(__code, __data)
216-
globals().update(__data)
217-
218-
# Python 3.3 doesn't have __spec__
219-
if hasattr(globals(), '__spec__'):
220-
__spec__.loader = __loader__
221-
__spec__.origin = __file__
222-
__spec__.submodule_search_locations = __path__
223-
__spec__.cached = None
224-
225-
# final cleanup
226-
del globals()['__data']
227-
del globals()['__code']
228-
# out-dated internals
229-
del globals()['__cached__']
230-
"""
218+
del globals()['zipfile']
219+
'''
231220

232221
injector_code = dedent(injector_code).lstrip()
233222
injector_code = injector_code.encode('utf-8')
@@ -249,3 +238,5 @@ def _install_injectors():
249238
pass
250239
except OSError as e:
251240
console_write('Unable to write injector to "%s" - %s' % (injector_path, e))
241+
242+
_install_injectors()

package_control/package_disabler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Relative import does not work here due to hard loading events
99
# into global package_control (see bootstrap.py)!
1010
from package_control import events
11-
except ImportError:
11+
except Exception:
1212
# use relative import, if bootstrapping has not yet been completed
1313
from . import events
1414

0 commit comments

Comments
 (0)