Fix DAP frame bug with older versions of Python

Tom de Vries pointed out that one DAP test failed on Python 3.6
because gdb.Frame is not hashable.

This patch fixes the problem by using a list to hold the frames.  This
is less efficient but there normally won't be that many frames.

Tested-by: Tom de Vries <tdevries@suse.de>
This commit is contained in:
Tom Tromey 2023-03-14 07:05:13 -06:00
parent 97b75c421f
commit 85c72d708e

View file

@ -18,20 +18,17 @@ import gdb
from .startup import in_gdb_thread from .startup import in_gdb_thread
# Map from frame (thread,level) pair to frame ID numbers. Note we # A list of all the frames we've reported. A frame's index in the
# can't use the frame itself here as it is not hashable. # list is its ID. We don't use a hash here because frames are not
_frame_ids = {} # hashable.
_all_frames = []
# Map from frame ID number back to frames.
_id_to_frame = {}
# Clear all the frame IDs. # Clear all the frame IDs.
@in_gdb_thread @in_gdb_thread
def _clear_frame_ids(evt): def _clear_frame_ids(evt):
global _frame_ids, _id_to_frame global _all_frames
_frame_ids = {} _all_frames = []
_id_to_frame = {}
# Clear the frame ID map whenever the inferior runs. # Clear the frame ID map whenever the inferior runs.
@ -41,17 +38,17 @@ gdb.events.cont.connect(_clear_frame_ids)
@in_gdb_thread @in_gdb_thread
def frame_id(frame): def frame_id(frame):
"""Return the frame identifier for FRAME.""" """Return the frame identifier for FRAME."""
global _frame_ids, _id_to_frame global _all_frames
pair = (gdb.selected_thread().global_num, frame.level) for i in range(0, len(_all_frames)):
if pair not in _frame_ids: if _all_frames[i] == frame:
id = len(_frame_ids) return i
_frame_ids[pair] = id result = len(_all_frames)
_id_to_frame[id] = frame _all_frames.append(frame)
return _frame_ids[pair] return result
@in_gdb_thread @in_gdb_thread
def frame_for_id(id): def frame_for_id(id):
"""Given a frame identifier ID, return the corresponding frame.""" """Given a frame identifier ID, return the corresponding frame."""
global _id_to_frame global _all_frames
return _id_to_frame[id] return _all_frames[id]