Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward source requests to debugpy as fallback #875

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions ipykernel/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,17 +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()
}
source_ref = message["arguments"].get("sourceReference", 0)
if source_ref == 0:
message["arguments"].get("source", {}).get("sourceReference", 0)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
message["arguments"].get("source", {}).get("sourceReference", 0)
source_ref = message["arguments"].get("source", {}).get("sourceReference", 0)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this change and it worked with and without the suggested change (as VS Code passes the sourceReference in both places, SourceRequest.sourceReference and sourceRequest.source.sourceReference)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@r3m0t Thanks for testing. This change is meant to solve this for non-vscode usages as well 👍

if source_ref > 0:
reply = await self._forward_message(message)
Copy link
Contributor

@JohanMabille JohanMabille Feb 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment; I think the right implementation should be to handle the request in the kernel if and only if the attribute sourceReference is 0. Notice that the source attribute is optional, according to the DAP, therefore this implementation is not totally compliant.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had another go at this, trying to follow the spec more closely this time.

else:
reply['success'] = False
reply['message'] = 'source unavailable'
reply['body'] = {}
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

Expand Down