Skip to content

Commit 5d8b7da

Browse files
Copilotrchiodo
andcommitted
Address code review: use = None instead of del in finally blocks, improve test
Changed del frame/del arg to frame = None/arg = None in finally blocks to avoid potential UnboundLocalError. Improved test to only get frame when needed. Co-authored-by: rchiodo <19672699+rchiodo@users.noreply.github.com>
1 parent 347cdb2 commit 5d8b7da

4 files changed

Lines changed: 61 additions & 73 deletions

File tree

src/debugpy/_vendored/pydevd/_pydevd_sys_monitoring/_pydevd_sys_monitoring.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,8 @@ def _unwind_event(code, instruction, exc):
930930
# Clear frame and arg references to avoid preventing garbage collection
931931
# of objects referenced from the frame's locals. The arg tuple also
932932
# contains the traceback which may hold frame references.
933-
del frame
934-
del arg
933+
frame = None
934+
arg = None
935935

936936

937937
# fmt: off
@@ -994,8 +994,8 @@ def _raise_event(code, instruction, exc):
994994
# Clear frame and arg references to avoid preventing garbage collection
995995
# of objects referenced from the frame's locals. The arg tuple also
996996
# contains the traceback which may hold frame references.
997-
del frame
998-
del arg
997+
frame = None
998+
arg = None
999999

10001000

10011001
# fmt: off

src/debugpy/_vendored/pydevd/_pydevd_sys_monitoring/_pydevd_sys_monitoring_cython.c

Lines changed: 48 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/debugpy/_vendored/pydevd/_pydevd_sys_monitoring/_pydevd_sys_monitoring_cython.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ cdef _unwind_event(code, instruction, exc):
936936
# Clear frame and arg references to avoid preventing garbage collection
937937
# of objects referenced from the frame's locals. The arg tuple also
938938
# contains the traceback which may hold frame references.
939-
del frame
940-
del arg
939+
frame = None
940+
arg = None
941941

942942

943943
# fmt: off
@@ -1000,8 +1000,8 @@ cdef _raise_event(code, instruction, exc):
10001000
# Clear frame and arg references to avoid preventing garbage collection
10011001
# of objects referenced from the frame's locals. The arg tuple also
10021002
# contains the traceback which may hold frame references.
1003-
del frame
1004-
del arg
1003+
frame = None
1004+
arg = None
10051005

10061006

10071007
# fmt: off

src/debugpy/_vendored/pydevd/tests_python/test_sys_monitoring.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,15 @@ def __init__(self, name):
314314
refs = []
315315

316316
def _start_method(code, offset):
317-
# Gets frame only if needed (simulating the improved pattern).
318-
frame = sys._getframe(1)
317+
# Only obtain the frame when actually needed (e.g., to enable local events).
319318
if "test_sys_monitoring" in code.co_filename:
319+
frame = sys._getframe(1)
320320
monitor.set_local_events(DEBUGGER_ID, code, monitor.events.PY_RETURN)
321321

322322
def _return_method(code, offset, retval):
323-
# In the fixed code, the frame is only obtained when step_cmd != -1.
324-
# Here we simulate the common path where no stepping is happening.
325-
pass # No frame obtained when not needed
323+
# Simulate the common path where no stepping is happening:
324+
# the frame should NOT be obtained when not needed.
325+
pass
326326

327327
monitor.set_events(DEBUGGER_ID, monitor.events.PY_START)
328328
monitor.register_callback(DEBUGGER_ID, monitor.events.PY_START, _start_method)

0 commit comments

Comments
 (0)