|
| 1 | +from os.path import expandvars |
| 2 | +from itertools import chain |
| 3 | +from glob import glob |
| 4 | +from pathlib import Path |
| 5 | +from functools import lru_cache |
| 6 | +import contextlib |
| 7 | +import logging |
| 8 | +import sys |
| 9 | + |
| 10 | +_log = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +@contextlib.contextmanager |
| 14 | +def _restore_sys_modules(): |
| 15 | + # Take a copy of sys.modules and sys.path |
| 16 | + sys_modules = dict(sys.modules) |
| 17 | + sys_path = list(sys.path) |
| 18 | + |
| 19 | + yield |
| 20 | + |
| 21 | + # Restore sys.modules and sys.path |
| 22 | + sys.modules.clear() |
| 23 | + sys.modules.update(sys_modules) |
| 24 | + sys.path.clear() |
| 25 | + sys.path.extend(sys_path) |
| 26 | + |
| 27 | + |
| 28 | +def _path_is_relative_to(a, b): |
| 29 | + # Path.is_relative_to is new in Python 3.9 |
| 30 | + try: |
| 31 | + Path(a).relative_to(b) |
| 32 | + return Path(a).relative_to(b) |
| 33 | + except ValueError: |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +@lru_cache(maxsize=2) |
| 38 | +def _import_pydevd(apply_fix=True): |
| 39 | + """Imports pydevd from the pydevd_pycharm package but leaves sys.modules as if |
| 40 | + this function was never called. |
| 41 | + This works around issues with debugpy also being installed. |
| 42 | + See https://youtrack.jetbrains.com/issue/PY-40661/pydevd-pycharm-conflicts-with-pydevd-package. |
| 43 | + """ |
| 44 | + # If apply_fix is False then simply import and return the modules |
| 45 | + if not apply_fix: |
| 46 | + import pydevd_pycharm |
| 47 | + import pydevd |
| 48 | + |
| 49 | + pydevd_pycharm_dir = Path(pydevd_pycharm.__file__).parent |
| 50 | + pydevd_dir = Path(pydevd.__file__).parent |
| 51 | + if pydevd_pycharm_dir != pydevd_dir: |
| 52 | + _log.warning("Potential pydevd version conflict found. " + |
| 53 | + f"Try uninstalling pydevd from '{pydevd_dir}' and reinstall pydevd_pycharm.") |
| 54 | + |
| 55 | + return pydevd_pycharm, pydevd |
| 56 | + |
| 57 | + # Restore sys.modules and sys.path once we're done |
| 58 | + with _restore_sys_modules(): |
| 59 | + try: |
| 60 | + # Try importing pydevd_pycharm from the default sys.path first |
| 61 | + import pydevd_pycharm |
| 62 | + except ImportError: |
| 63 | + # If that fails look for PyCharm and add it to sys.path |
| 64 | + _log.debug("pydevd_pycharm not found on sys.path. Looking for PyCharm install...") |
| 65 | + pydevd_eggs = list(reversed(sorted(chain.from_iterable( |
| 66 | + glob(expandvars(f"{env_var}\\JetBrains\\PyCharm*\\debug-eggs\\pydevd-pycharm.egg"), recursive=True) |
| 67 | + for env_var in ("${ProgramFiles}", "${ProgramFiles(x86)}"))))) |
| 68 | + |
| 69 | + if pydevd_eggs: |
| 70 | + pydevd_egg = pydevd_eggs[0] |
| 71 | + _log.debug(f"Found pydevd-pycharm in PyCharm install: {pydevd_egg}") |
| 72 | + sys.path.insert(0, pydevd_egg) |
| 73 | + |
| 74 | + # Try to import pydevd_pycharm again (sys.path may have changed) |
| 75 | + import pydevd_pycharm |
| 76 | + |
| 77 | + # Next import pydev |
| 78 | + import pydevd |
| 79 | + |
| 80 | + # pydevd should be distributed as part of pydevd_pycharm |
| 81 | + pydevd_pycharm_dir = Path(pydevd_pycharm.__file__).parent |
| 82 | + pydevd_dir = Path(pydevd.__file__).parent |
| 83 | + if pydevd_pycharm_dir != pydevd_dir: |
| 84 | + _log.debug(f"Incompatible version of pydevd found: {pydevd.__file__}") |
| 85 | + |
| 86 | + # Remove all the existing pydevd modules from sys.modules |
| 87 | + pydev_modules = set() |
| 88 | + for name, module in sys.modules.items(): |
| 89 | + if ((name.startswith("pydev") or name.startswith("_pydev")) |
| 90 | + and (_path_is_relative_to(module.__file__, pydevd_dir) or |
| 91 | + _path_is_relative_to(module.__file__, pydevd_pycharm_dir))): |
| 92 | + pydev_modules.add(name) |
| 93 | + |
| 94 | + for name in pydev_modules: |
| 95 | + del sys.modules[name] |
| 96 | + |
| 97 | + # Re-import pydevd_pycharm and pydevd with the PyCharm path appearing first on sys.path |
| 98 | + sys.path.insert(0, str(pydevd_pycharm_dir)) |
| 99 | + import pydevd_pycharm |
| 100 | + import pydevd |
| 101 | + |
| 102 | + # Check what we've imported is now correct |
| 103 | + pydevd_pycharm_dir = Path(pydevd_pycharm.__file__).parent |
| 104 | + pydevd_dir = Path(pydevd.__file__).parent |
| 105 | + if pydevd_pycharm_dir != pydevd_dir: |
| 106 | + _log.warning("Potential pydevd version conflict found. " + |
| 107 | + f"Try uninstalling pydevd from '{pydevd_dir}' and reinstall pydevd_pycharm.") |
| 108 | + |
| 109 | + return pydevd_pycharm, pydevd |
0 commit comments