Detect whether debugger is already running and skip connecting/listening in that case (#1657)

This commit is contained in:
Othman El Hammouchi 2024-08-27 22:02:25 +02:00 коммит произвёл GitHub
Родитель fb6158a059
Коммит a2f80817a1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 32 добавлений и 17 удалений

Просмотреть файл

@ -191,6 +191,7 @@ switches = [
]
# fmt: on
# Consume all the args from argv
def consume_argv():
while len(sys.argv) >= 2:
@ -198,9 +199,10 @@ def consume_argv():
del sys.argv[1]
yield value
# Consume all the args from a given list
def consume_args(args: list):
if (args is sys.argv):
if args is sys.argv:
yield from consume_argv()
else:
while args:
@ -208,6 +210,7 @@ def consume_args(args: list):
del args[0]
yield value
# Parse the args from the command line, then from the environment.
# Args from the environment are only used if they are not already set from the command line.
def parse_args():
@ -232,12 +235,14 @@ def parse_args():
assert options.target_kind is not None
assert options.address is not None
def parse_args_from_command_line(seen: set):
parse_args_helper(sys.argv, seen)
def parse_args_from_environment(seenFromCommandLine: set):
args = os.environ.get("DEBUGPY_EXTRA_ARGV")
if (not args):
if not args:
return
argsList = args.split()
@ -245,7 +250,13 @@ def parse_args_from_environment(seenFromCommandLine: set):
seenFromEnvironment = set()
parse_args_helper(argsList, seenFromCommandLine, seenFromEnvironment, True)
def parse_args_helper(args: list, seenFromCommandLine: set, seenFromEnvironment: set = set(), isFromEnvironment=False):
def parse_args_helper(
args: list,
seenFromCommandLine: set,
seenFromEnvironment: set = set(),
isFromEnvironment=False,
):
iterator = consume_args(args)
while True:
@ -264,17 +275,17 @@ def parse_args_helper(args: list, seenFromCommandLine: set, seenFromEnvironment:
raise ValueError("unrecognized switch " + switch)
# if we're parsing from the command line, and we've already seen the switch on the command line, this is an error
if (not isFromEnvironment and switch in seenFromCommandLine):
if not isFromEnvironment and switch in seenFromCommandLine:
raise ValueError("duplicate switch on command line: " + switch)
# if we're parsing from the environment, and we've already seen the switch in the environment, this is an error
elif (isFromEnvironment and switch in seenFromEnvironment):
elif isFromEnvironment and switch in seenFromEnvironment:
raise ValueError("duplicate switch from environment: " + switch)
# if we're parsing from the environment, and we've already seen the switch on the command line, skip it, since command line takes precedence
elif (isFromEnvironment and switch in seenFromCommandLine):
elif isFromEnvironment and switch in seenFromCommandLine:
continue
# otherwise, the switch is new, so add it to the appropriate set
else:
if (isFromEnvironment):
if isFromEnvironment:
seenFromEnvironment.add(switch)
else:
seenFromCommandLine.add(switch)
@ -291,9 +302,10 @@ def parse_args_helper(args: list, seenFromCommandLine: set, seenFromEnvironment:
# If we're parsing the command line, we're done after we've processed the target
# Otherwise, we need to keep parsing until all args are consumed, since the target may be set from the command line
# already, but there might be additional args in the environment that we want to process.
if (not isFromEnvironment and options.target is not None):
if not isFromEnvironment and options.target is not None:
break
def start_debugging(argv_0):
# We need to set up sys.argv[0] before invoking either listen() or connect(),
# because they use it to report the "process" event. Thus, we can't rely on
@ -304,6 +316,7 @@ def start_debugging(argv_0):
debugpy.configure(options.config)
if os.environ.get("DEBUGPY_RUNNING", "false") != "true":
if options.mode == "listen" and options.address is not None:
debugpy.listen(options.address)
elif options.mode == "connect" and options.address is not None:
@ -314,6 +327,8 @@ def start_debugging(argv_0):
if options.wait_for_client:
debugpy.wait_for_client()
os.environ["DEBUGPY_RUNNING"] = "true"
def run_file():
target = options.target