Skip to content

Commit 4c0b431

Browse files
committed
Use ExitStack to reduce nesting in attach
1 parent 8fa88a5 commit 4c0b431

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

Lib/pdb.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
import selectors
9696
import _colorize
9797

98+
from contextlib import ExitStack
9899
from contextlib import closing
99100
from contextlib import contextmanager
100101
from rlcompleter import Completer
@@ -3250,40 +3251,50 @@ def _connect(host, port, frame, commands, version):
32503251

32513252
def attach(pid, commands=()):
32523253
"""Attach to a running process with the given PID."""
3253-
with closing(socket.create_server(("localhost", 0))) as server:
3254+
with ExitStack() as stack:
3255+
server = stack.enter_context(
3256+
closing(socket.create_server(("localhost", 0)))
3257+
)
3258+
32543259
port = server.getsockname()[1]
32553260

3256-
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as connect_script:
3257-
connect_script.write(
3258-
textwrap.dedent(
3259-
f"""
3260-
import pdb, sys
3261-
pdb._connect(
3262-
host="localhost",
3263-
port={port},
3264-
frame=sys._getframe(1),
3265-
commands={json.dumps("\n".join(commands))},
3266-
version={_PdbServer.protocol_version()},
3267-
)
3268-
"""
3261+
connect_script = stack.enter_context(
3262+
tempfile.NamedTemporaryFile("w", delete_on_close=False)
3263+
)
3264+
3265+
connect_script.write(
3266+
textwrap.dedent(
3267+
f"""
3268+
import pdb, sys
3269+
pdb._connect(
3270+
host="localhost",
3271+
port={port},
3272+
frame=sys._getframe(1),
3273+
commands={json.dumps("\n".join(commands))},
3274+
version={_PdbServer.protocol_version()},
32693275
)
3276+
"""
32703277
)
3271-
connect_script.close()
3272-
sys.remote_exec(pid, connect_script.name)
3273-
3274-
# TODO Add a timeout? Or don't bother since the user can ^C?
3275-
client_sock, _ = server.accept()
3276-
3277-
with closing(client_sock):
3278-
with tempfile.NamedTemporaryFile("w", delete_on_close=False) as interrupt_script:
3279-
interrupt_script.write(
3280-
'import pdb, sys\n'
3281-
'if inst := pdb.Pdb._last_pdb_instance:\n'
3282-
' inst.set_trace(sys._getframe(1))\n'
3283-
)
3284-
interrupt_script.close()
3278+
)
3279+
connect_script.close()
3280+
sys.remote_exec(pid, connect_script.name)
3281+
3282+
# TODO Add a timeout? Or don't bother since the user can ^C?
3283+
client_sock, _ = server.accept()
3284+
3285+
stack.enter_context(closing(client_sock))
3286+
3287+
interrupt_script = stack.enter_context(
3288+
tempfile.NamedTemporaryFile("w", delete_on_close=False)
3289+
)
3290+
interrupt_script.write(
3291+
'import pdb, sys\n'
3292+
'if inst := pdb.Pdb._last_pdb_instance:\n'
3293+
' inst.set_trace(sys._getframe(1))\n'
3294+
)
3295+
interrupt_script.close()
32853296

3286-
_PdbClient(pid, client_sock, interrupt_script.name).cmdloop()
3297+
_PdbClient(pid, client_sock, interrupt_script.name).cmdloop()
32873298

32883299

32893300
# Post-Mortem interface

0 commit comments

Comments
 (0)