Add connected_endpoint() and listenin_endpoint() api methods

This commit is contained in:
N. Ben Cohen 2024-03-14 23:08:45 -06:00
parent 42853a99c4
commit 1bebf3deec
2 changed files with 51 additions and 5 deletions

View file

@ -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
@ -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"]

View file

@ -36,6 +36,16 @@ _config_valid_values = {
# 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)
@ -116,7 +126,7 @@ def _starts_debugging(func):
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()
@ -145,6 +155,10 @@ def _starts_debugging(func):
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
@ -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: