|
95 | 95 | import selectors |
96 | 96 | import _colorize |
97 | 97 |
|
| 98 | +from contextlib import ExitStack |
98 | 99 | from contextlib import closing |
99 | 100 | from contextlib import contextmanager |
100 | 101 | from rlcompleter import Completer |
@@ -3250,40 +3251,50 @@ def _connect(host, port, frame, commands, version): |
3250 | 3251 |
|
3251 | 3252 | def attach(pid, commands=()): |
3252 | 3253 | """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 | + |
3254 | 3259 | port = server.getsockname()[1] |
3255 | 3260 |
|
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()}, |
3269 | 3275 | ) |
| 3276 | + """ |
3270 | 3277 | ) |
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() |
3285 | 3296 |
|
3286 | | - _PdbClient(pid, client_sock, interrupt_script.name).cmdloop() |
| 3297 | + _PdbClient(pid, client_sock, interrupt_script.name).cmdloop() |
3287 | 3298 |
|
3288 | 3299 |
|
3289 | 3300 | # Post-Mortem interface |
|
0 commit comments