Skip to content

Commit 6384803

Browse files
committed
Handle python imports in a plethora of setups
The commit message below is copyed from python-mode#986 and I did some change. And this commit is to make a traceability commit message and updates comment in the code commited in previous commit, for detail, reference PR python-mode#985. There has been many changes in how python import modules across python versions, specially in 3.3, 3.4 and 3.7. This has caused many issues with modules importing in vim, and how vim handles/implements imports from python also have caused many issues. In python-mode we have tried many alternatives ways to fix this issues, but they failed for many different conditions. The main issue in our repo that track the problem solved by this commit is the python-mode#972, there are lot's of comments and references over there. As I comment in code: For python: * since python 3.3, the `imp.find_module()` is deprecated * `importlib.util.find_spec()` is new in python 3.4 For vim: * if it compiled with python 3.7 or newer, then the module 'vim' will use `find_spec` instead of `find_module` and `load_module`. * if it compiled with feature `+python3/dyn`, vim can load a python dynamically. The loaded python maybe has a different version compared with the python used when compiling vim. For these reason, it's hard to say we should use `find_spec` or `find_module`, so here use try/except for the sake of clearness of the logic. @diraol says he would like to thank @racterub, @joergsch, @zcodes, @nineKnight, @lalmeras, @dbeniamine, and everyone else that helped with this issue. (me too) Fix python-mode#972 Close python-mode#985 Close python-mode#955 Close python-mode#986
1 parent a56dde5 commit 6384803

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

pymode/libs/pkg_resources/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,12 +2170,23 @@ def _handle_ns(packageName, path_item):
21702170
return None
21712171

21722172
if PY3:
2173+
# For python:
21732174
# * since python 3.3, the `imp.find_module()` is deprecated
21742175
# * `importlib.util.find_spec()` is new in python 3.4
2175-
# * in vim source, if compiled vim with python 3.7 or new, the vim
2176-
# python 3 interface will use `find_spec` instead of `find_module`
2177-
# and `load_module`. (note: this depends on the python which compiled
2178-
# with vim, not the python loaded by vim at runtime.)
2176+
#
2177+
# For vim:
2178+
# * if it compiled with python 3.7 or newer, then the module 'vim' will
2179+
# use `find_spec` instead of `find_module` and `load_module`.
2180+
# * if it compiled with feature `+python3/dyn`, vim can load a python
2181+
# dynamically. The loaded python maybe has a different version compared
2182+
# with the python used when compiling vim.
2183+
#
2184+
# As these reason, it's hard to say we should use `find_spec` or
2185+
# `find_module`, so here use try/except for the sake of clearness of
2186+
# the logic.
2187+
#
2188+
# As `find_module` is deprecated for newer python, we try `find_spec`
2189+
# first.
21792190
try:
21802191
loader = importer.find_spec(packageName)
21812192
except AttributeError:

0 commit comments

Comments
 (0)