Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connected_endpoint() and listenin_endpoint() api methods #1540

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/debugpy/public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ def listen(

`in_process_debug_adapter`: by default a separate python process is
spawned and used to communicate with the client as the debug adapter.
By setting the value of `in_process_debug_adapter` to True a new
python process is not spawned. Note: the con of setting
`in_process_debug_adapter` to True is that subprocesses won't be
By setting the value of `in_process_debug_adapter` to True a new
python process is not spawned. Note: the con of setting
`in_process_debug_adapter` to True is that subprocesses won't be
automatically debugged.

Returns the interface and the port on which the debug adapter is
actually listening, in the same format as `__endpoint`. This may be
different from address if port was 0 in the latter, in which case
Expand Down Expand Up @@ -191,4 +191,26 @@ def trace_this_thread(__should_trace: bool):
"""


@_api()
def listening_endpoint() -> (
typing.Tuple[typing.Optional[Endpoint], typing.Optional[str]]
):
"""When process is operating as a server,
this api exposes the Endpoint and access_token that one would use to connect to this server

When not operating as a server, these values are None
"""


@_api()
def connected_endpoint() -> (
typing.Tuple[typing.Optional[Endpoint], typing.Optional[str]]
):
"""When process is operating as a client and has connected to a server
this api exposes the Endpoint and access_token used to connect to the server

When not connected as a client, these values are None
"""


__version__: str = _version.get_versions()["version"]
26 changes: 25 additions & 1 deletion src/debugpy/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
# https://bugs.python.org/issue37380.
_adapter_process = None

# if operating in server mode, stores connection parameters for the endpoint the process is listening to
# otherwise None
_stored_server_endpoint = None
_stored_server_access_token = None

# if operating in server mode, stores connection parameters for the endpoint the process is connected to
# otherwise None
_stored_client_endpoint = None
_stored_client_access_token = None


def _settrace(*args, **kwargs):
log.debug("pydevd.settrace(*{0!r}, **{1!r})", args, kwargs)
Expand Down Expand Up @@ -116,7 +126,7 @@ def debug(address, **kwargs):
port.__index__() # ensure it's int-like
except Exception:
raise ValueError("expected port or (host, port)")
if not (0 <= port < 2 ** 16):
if not (0 <= port < 2**16):
raise ValueError("invalid port number")

ensure_logging()
Expand Down Expand Up @@ -145,6 +155,10 @@ def debug(address, **kwargs):
return debug


def listening_endpoint():
return _stored_server_endpoint, _stored_server_access_token


@_starts_debugging
def listen(address, settrace_kwargs, in_process_debug_adapter=False):
# Errors below are logged with level="info", because the caller might be catching
Expand Down Expand Up @@ -288,13 +302,23 @@ def listen(address, settrace_kwargs, in_process_debug_adapter=False):
**settrace_kwargs
)
log.info("pydevd is connected to adapter at {0}:{1}", server_host, server_port)
global _stored_server_endpoint, _stored_server_access_token
_stored_server_endpoint = (client_host, client_port)
_stored_server_access_token = server_access_token
return client_host, client_port


def connected_endpoint():
return _stored_client_endpoint, _stored_client_access_token


@_starts_debugging
def connect(address, settrace_kwargs, access_token=None):
host, port = address
_settrace(host=host, port=port, client_access_token=access_token, **settrace_kwargs)
global _stored_client_endpoint, _stored_client_access_token
_stored_client_endpoint = address
_stored_client_access_token = access_token


class wait_for_client:
Expand Down