|
2 | 2 | # Licensed under the MIT License. See LICENSE in the project root |
3 | 3 | # for license information. |
4 | 4 |
|
| 5 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 6 | + |
| 7 | +from importlib import import_module |
| 8 | +import os |
| 9 | + |
5 | 10 | # "force_pydevd" must be imported first to ensure (via side effects) |
6 | 11 | # that the debugpy-vendored copy of pydevd gets used. |
7 | | -import debugpy._vendored.force_pydevd # noqa |
| 12 | +import debugpy |
| 13 | + |
| 14 | +# If debugpy logging is enabled, enable it for pydevd as well |
| 15 | +if "DEBUGPY_LOG_DIR" in os.environ: |
| 16 | + os.environ[str("PYDEVD_DEBUG")] = str("True") |
| 17 | + os.environ[str("PYDEVD_DEBUG_FILE")] = os.environ["DEBUGPY_LOG_DIR"] + str( |
| 18 | + "/debugpy.pydevd.log" |
| 19 | + ) |
| 20 | + |
| 21 | +# Disable pydevd frame-eval optimizations only if unset, to allow opt-in. |
| 22 | +if "PYDEVD_USE_FRAME_EVAL" not in os.environ: |
| 23 | + os.environ[str("PYDEVD_USE_FRAME_EVAL")] = str("NO") |
| 24 | + |
| 25 | +BUNDLE_DEBUGPY = bool(os.getenv("BUNDLE_DEBUGPY")) |
| 26 | + |
| 27 | +# Constants must be set before importing any other pydevd module |
| 28 | +# due to heavy use of "from" in them. |
| 29 | +if BUNDLE_DEBUGPY: |
| 30 | + try: |
| 31 | + import debugpy._vendored.force_pydevd # noqa |
| 32 | + except Exception as e: |
| 33 | + raise e |
| 34 | +else: |
| 35 | + pydevd_constants = import_module("_pydevd_bundle.pydevd_constants") |
| 36 | + |
| 37 | +# We limit representation size in our representation provider when needed. |
| 38 | +pydevd_constants.MAXIMUM_VARIABLE_REPRESENTATION_SIZE = 2**32 |
| 39 | + |
| 40 | +# When pydevd is imported it sets the breakpoint behavior, but it needs to be |
| 41 | +# overridden because by default pydevd will connect to the remote debugger using |
| 42 | +# its own custom protocol rather than DAP. |
| 43 | +import pydevd # noqa |
| 44 | +import debugpy # noqa |
| 45 | + |
| 46 | + |
| 47 | +def debugpy_breakpointhook(): |
| 48 | + debugpy.breakpoint() |
| 49 | + |
| 50 | + |
| 51 | +pydevd.install_breakpointhook(debugpy_breakpointhook) |
| 52 | + |
| 53 | +# Ensure that pydevd uses JSON protocol |
| 54 | +from _pydevd_bundle import pydevd_constants |
| 55 | +from _pydevd_bundle import pydevd_defaults |
| 56 | + |
| 57 | +pydevd_defaults.PydevdCustomization.DEFAULT_PROTOCOL = ( |
| 58 | + pydevd_constants.HTTP_JSON_PROTOCOL |
| 59 | +) |
| 60 | + |
| 61 | +# Enable some defaults related to debugpy such as sending a single notification when |
| 62 | +# threads pause and stopping on any exception. |
| 63 | +pydevd_defaults.PydevdCustomization.DEBUG_MODE = "debugpy-dap" |
| 64 | + |
| 65 | +# This is important when pydevd attaches automatically to a subprocess. In this case, we have to |
| 66 | +# make sure that debugpy is properly put back in the game for users to be able to use it. |
| 67 | +if not BUNDLE_DEBUGPY: |
| 68 | + pydevd_defaults.PydevdCustomization.PREIMPORT = "%s;%s" % ( |
| 69 | + os.path.dirname(os.path.dirname(debugpy.__file__)), |
| 70 | + "debugpy._vendored.force_pydevd", |
| 71 | + ) |
0 commit comments