From e08feef596a36146b5c64a0ddec438aa75e143fc Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 26 Mar 2018 17:36:48 +0000 Subject: [PATCH] Convert import side-effects into explicit calls. --- ptvsd/__main__.py | 7 +++---- ptvsd/debugger.py | 2 +- ptvsd/wrapper.py | 13 ++++++++++--- tests/helpers/pydevd/_live.py | 13 +++++-------- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/ptvsd/__main__.py b/ptvsd/__main__.py index 3abaf755..2a01e094 100644 --- a/ptvsd/__main__.py +++ b/ptvsd/__main__.py @@ -2,14 +2,13 @@ # Licensed under the MIT License. See LICENSE in the project root # for license information. +import ptvsd.wrapper + __author__ = "Microsoft Corporation " __version__ = "4.0.0a5" if __name__ == '__main__': - # import the wrapper first, so that it gets a chance - # to detour pydevd socket functionality. - import ptvsd.wrapper # noqa - import pydevd + pydevd = ptvsd.wrapper.install() pydevd.main() diff --git a/ptvsd/debugger.py b/ptvsd/debugger.py index ca37d27c..eaf9674b 100644 --- a/ptvsd/debugger.py +++ b/ptvsd/debugger.py @@ -48,7 +48,7 @@ def _run_argv(address, filename): def _run(argv): - import pydevd + pydevd = ptvsd.wrapper.install(argv) sys.argv[1:0] = argv try: pydevd.main() diff --git a/ptvsd/wrapper.py b/ptvsd/wrapper.py index 53cac0a1..38bff6ef 100644 --- a/ptvsd/wrapper.py +++ b/ptvsd/wrapper.py @@ -1783,6 +1783,13 @@ def start_client(host, port, addhandlers=True): return pydevd -# These are the functions pydevd invokes to get a socket to the client. -pydevd_comm.start_server = start_server -pydevd_comm.start_client = start_client +def install(start_server=start_server, start_client=start_client): + """Configure pydevd to use our wrapper.""" + # These are the functions pydevd invokes to get a socket to the client. + pydevd_comm.start_server = start_server + pydevd_comm.start_client = start_client + + # Force a fresh pydevd. + sys.modules.pop('pydevd', None) + import pydevd # noqa + return pydevd diff --git a/tests/helpers/pydevd/_live.py b/tests/helpers/pydevd/_live.py index 249f3638..77e96502 100644 --- a/tests/helpers/pydevd/_live.py +++ b/tests/helpers/pydevd/_live.py @@ -1,12 +1,9 @@ import os import os.path -import sys import threading import warnings -import _pydevd_bundle.pydevd_comm as pydevd_comm - -from ptvsd import debugger +from ptvsd import debugger, wrapper from tests.helpers import protocol from ._binder import BinderBase @@ -24,10 +21,10 @@ class Binder(BinderBase): def new_pydevd_sock(*args): self._start_ptvsd() return self.ptvsd.fakesock - pydevd_comm.start_server = new_pydevd_sock - pydevd_comm.start_client = new_pydevd_sock - # Force a fresh pydevd. - sys.modules.pop('pydevd', None) + wrapper.install( + start_server=new_pydevd_sock, + start_client=new_pydevd_sock, + ) if self.module is None: debugger._run_file(self.address, self.filename) else: