Format: text/x-python
⬇ Download File (3.2 KB)
   1  # Copyright (c) Microsoft Corporation. All rights reserved.
   2  # Licensed under the MIT License. See LICENSE in the project root
   3  # for license information.
   4  
   5  from importlib import import_module
   6  import os
   7  import warnings
   8  
   9  from . import check_modules, prefix_matcher, preimport, vendored
  10  
  11  # Ensure that pydevd is our vendored copy.
  12  _unvendored, _ = check_modules('pydevd',
  13                                 prefix_matcher('pydev', '_pydev'))
  14  if _unvendored:
  15      _unvendored = sorted(_unvendored.values())
  16      msg = 'incompatible copy of pydevd already imported'
  17      # raise ImportError(msg)
  18      warnings.warn(msg + ':\n {}'.format('\n  '.join(_unvendored)))
  19  
  20  # If debugpy logging is enabled, enable it for pydevd as well
  21  if "DEBUGPY_LOG_DIR" in os.environ:
  22      os.environ[str("PYDEVD_DEBUG")] = str("True")
  23      os.environ[str("PYDEVD_DEBUG_FILE")] = os.environ["DEBUGPY_LOG_DIR"] + str("/debugpy.pydevd.log")
  24  
  25  # Disable pydevd frame-eval optimizations only if unset, to allow opt-in.
  26  if "PYDEVD_USE_FRAME_EVAL" not in os.environ:
  27      os.environ[str("PYDEVD_USE_FRAME_EVAL")] = str("NO")
  28  
  29  # Constants must be set before importing any other pydevd module
  30  # # due to heavy use of "from" in them.
  31  with warnings.catch_warnings():
  32      warnings.simplefilter("ignore", category=DeprecationWarning)
  33      with vendored('pydevd'):
  34          pydevd_constants = import_module('_pydevd_bundle.pydevd_constants')
  35  # We limit representation size in our representation provider when needed.
  36  pydevd_constants.MAXIMUM_VARIABLE_REPRESENTATION_SIZE = 2 ** 32
  37  
  38  # Now make sure all the top-level modules and packages in pydevd are
  39  # loaded.  Any pydevd modules that aren't loaded at this point, will
  40  # be loaded using their parent package's __path__ (i.e. one of the
  41  # following).
  42  with warnings.catch_warnings():
  43      warnings.simplefilter("ignore", category=DeprecationWarning)
  44      preimport('pydevd', [
  45          '_pydev_bundle',
  46          '_pydev_runfiles',
  47          '_pydevd_bundle',
  48          '_pydevd_frame_eval',
  49          'pydev_ipython',
  50          'pydevd_plugins',
  51          'pydevd',
  52      ])
  53  
  54  # When pydevd is imported it sets the breakpoint behavior, but it needs to be
  55  # overridden because by default pydevd will connect to the remote debugger using
  56  # its own custom protocol rather than DAP.
  57  import pydevd  # noqa
  58  import debugpy  # noqa
  59  
  60  
  61  def debugpy_breakpointhook():
  62      debugpy.breakpoint()
  63  
  64  
  65  pydevd.install_breakpointhook(debugpy_breakpointhook)
  66  
  67  # Ensure that pydevd uses JSON protocol
  68  from _pydevd_bundle import pydevd_constants
  69  from _pydevd_bundle import pydevd_defaults
  70  pydevd_defaults.PydevdCustomization.DEFAULT_PROTOCOL = pydevd_constants.HTTP_JSON_PROTOCOL
  71  
  72  # Enable some defaults related to debugpy such as sending a single notification when
  73  # threads pause and stopping on any exception.
  74  pydevd_defaults.PydevdCustomization.DEBUG_MODE = 'debugpy-dap'
  75  
  76  # This is important when pydevd attaches automatically to a subprocess. In this case, we have to
  77  # make sure that debugpy is properly put back in the game for users to be able to use it.
  78  pydevd_defaults.PydevdCustomization.PREIMPORT = '%s;%s' % (
  79      os.path.dirname(os.path.dirname(debugpy.__file__)), 
  80      'debugpy._vendored.force_pydevd'
  81  )
  82