Skip to content

Commit 05a57e2

Browse files
committed
Prefer __spec__ on modern python
Python 3.1x moves on to replace __file__ and __package__ entirely by __spec__. This commit ensures to keep up and avoid issues with former 2 being no longer maintained or even removed at some point in future.
1 parent 701bd0d commit 05a57e2

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

package_control/sys_path.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
if not os.path.isdir(__default_packages_path):
1717
# Fall back to detecting the path using the location of the module
1818
import Default.sort as default_module
19-
__default_packages_path = os.path.dirname(os.path.dirname(default_module.__file__))
19+
try:
20+
# python 3.8+
21+
__default_packages_path = os.path.dirname(os.path.dirname(default_module.__spec__.origin))
22+
except AttributeError:
23+
# python 3.3
24+
__default_packages_path = os.path.dirname(os.path.dirname(default_module.__file__))
2025

2126
if not os.path.isdir(__default_packages_path):
2227
raise FileNotFoundError('Default Packages')
@@ -28,7 +33,12 @@
2833
# {data_dir}/Installed Packages/Package Control.sublime-package/package_control/sys_path.py
2934
# When loaded as unpacked package, __file__ ends up being
3035
# {data_dir}/Packages/Package Control/package_control/sys_path.py
31-
__data_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
36+
try:
37+
# python 3.8+
38+
__data_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__spec__.origin))))
39+
except AttributeError:
40+
# python 3.3
41+
__data_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
3242

3343
# Determine extracted packages path
3444
__packages_path = os.path.join(__data_path, 'Packages')

0 commit comments

Comments
 (0)