From 4d4fc58fce93a07a210e98e42c439985486e43c0 Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Wed, 21 Feb 2018 13:18:08 -0800 Subject: [PATCH] Remove sys.path entry for vendored pydevd after loading it. Improve error reporting when pydevd is imported before ptvsd. --- ptvsd/__init__.py | 28 +++++++++++++++++++++++++--- ptvsd/wrapper.py | 3 +-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/ptvsd/__init__.py b/ptvsd/__init__.py index 922fbe7c..9907ffec 100644 --- a/ptvsd/__init__.py +++ b/ptvsd/__init__.py @@ -5,10 +5,32 @@ __author__ = "Microsoft Corporation " __version__ = "4.0.0a1" -# ptvsd must always be imported before pydevd import sys -assert 'pydevd' not in sys.modules +import os.path + +# ptvsd must always be imported before pydevd +if 'pydevd' in sys.modules: + raise ImportError('ptvsd must be imported before pydevd') # Add our vendored pydevd directory to path, so that it gets found first. -import os.path sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pydevd')) + +# Load our wrapper module, which will detour various functionality inside pydevd. +# This must be done before the imports below, otherwise some modules will end up +# with local copies of pre-detour functions. +import ptvsd.wrapper + +# Now make sure all the top-level modules and packages in pydevd are loaded. +import _pydev_bundle +import _pydev_imps +import _pydev_runfiles +import _pydevd_bundle +import _pydevd_frame_eval +import pydev_ipython +import pydevd_concurrency_analyser +import pydevd_plugins +import pydevd + +# Remove sys.path entry added above - any pydevd modules that aren't loaded at +# this point, will be loaded using their parent package's __path__. +del sys.path[0] diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py index f44a3ceb..c43ab861 100644 --- a/ptvsd/wrapper.py +++ b/ptvsd/wrapper.py @@ -2,8 +2,7 @@ # Licensed under the MIT License. See LICENSE in the project root # for license information. -# TODO: with_statement is not needed -from __future__ import print_function, with_statement, absolute_import +from __future__ import print_function, absolute_import import atexit import os