From faaf19f3a388fddfbde8c385fc7b412676ccfe35 Mon Sep 17 00:00:00 2001 From: Vidar Tonaas Fauske Date: Tue, 22 Feb 2022 16:39:38 +0000 Subject: [PATCH 1/2] Forward source requests to debugpy as fallback If the source cannot be looked up by the `path` argument, forward it to debugpy as it might be able to resolve it using the sourceReference field. --- ipykernel/debugger.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ipykernel/debugger.py b/ipykernel/debugger.py index dfe5d0a9b..bd50f191c 100644 --- a/ipykernel/debugger.py +++ b/ipykernel/debugger.py @@ -442,9 +442,7 @@ async def source(self, message): 'content': f.read() } else: - reply['success'] = False - reply['message'] = 'source unavailable' - reply['body'] = {} + reply = await self._forward_message(message) return reply From 70bcee67ac461e7e9835af5c64d44bbddba90935 Mon Sep 17 00:00:00 2001 From: Vidar Tonaas Fauske Date: Thu, 24 Feb 2022 14:28:24 +0000 Subject: [PATCH 2/2] Align debugger source handler with DAP spec Give `sourceReference` priority. Not immediately clear which location should be given priority if soruceReference and source.sourceReference are both defined and different values. Spec says that they should be the same, so we can claim that this is not our problem. --- ipykernel/debugger.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ipykernel/debugger.py b/ipykernel/debugger.py index bd50f191c..8a397c475 100644 --- a/ipykernel/debugger.py +++ b/ipykernel/debugger.py @@ -434,15 +434,23 @@ async def source(self, message): 'request_seq': message['seq'], 'command': message['command'] } - source_path = message["arguments"]["source"]["path"] - if os.path.isfile(source_path): - with open(source_path, encoding='utf-8') as f: - reply['success'] = True - reply['body'] = { - 'content': f.read() - } - else: + source_ref = message["arguments"].get("sourceReference", 0) + if source_ref == 0: + message["arguments"].get("source", {}).get("sourceReference", 0) + if source_ref > 0: reply = await self._forward_message(message) + else: + source_path = message["arguments"].get("source", {}).get("path") + if source_path and os.path.isfile(source_path): + with open(source_path, encoding='utf-8') as f: + reply['success'] = True + reply['body'] = { + 'content': f.read() + } + else: + reply['success'] = False + reply['message'] = 'Source unavailable' + reply['body'] = {} return reply