forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix issues when running without debugging and debugged code terminates #249
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ecc1ca9
Fix Microsoft/vscode#37627 (#1368)
octref 7c5778c
Version 0.7.0 of extension (#1381)
DonJayamanne 9d1bf82
Update README.md
DonJayamanne ffba179
Update README.md
DonJayamanne 905c713
sync fork with upstream
DonJayamanne acc2109
fix readme
DonJayamanne d470523
Merge branch 'master' of https://github.com/Microsoft/vscode-python
DonJayamanne d32a546
run without debugging
DonJayamanne 3f14d0a
send terminated event when process ends and fix linters
DonJayamanne f37803f
gracefully handle program termination
DonJayamanne d69940e
use vscode infrastructure to launch code in terminals
DonJayamanne d392e8b
merged upstream
DonJayamanne 11c9514
merged
DonJayamanne 38befc0
ensure to check if launchArgs is not null
DonJayamanne bea91fc
ensure to check if launchArgs is not null
DonJayamanne 0bb9054
ensure to check if launchArgs is not null
DonJayamanne a105a20
fix code review comments
DonJayamanne 8b2fd7c
fixed code review comments
DonJayamanne 5a7d21e
Merge branch 'master' into RunWithoutDebug
DonJayamanne acd90c5
Merge branch 'master' into RunWithoutDebug
DonJayamanne 09b3359
fixes #250, do not copy all env variables
DonJayamanne 9616eef
bug fix, launch using vscode and then fallback
DonJayamanne f9fe2a8
fix linter issue
DonJayamanne 79aa7c1
fix code review issues
DonJayamanne 984602e
running without debugging without workspaces
DonJayamanne d65eece
Merge branch 'master' into RunWithoutDebug
DonJayamanne 650ec17
Merge branch 'master' into RunWithoutDebug
DonJayamanne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
146 changes: 146 additions & 0 deletions
146
pythonFiles/PythonTools/visualstudio_py_launcher_nodebug.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
"""Run a block of code or Python file.""" | ||
|
||
import sys | ||
import os.path | ||
import traceback | ||
import time | ||
import socket | ||
try: | ||
import visualstudio_py_util as _vspu | ||
except: | ||
traceback.print_exc() | ||
print("""Internal error detected. Please copy the above traceback and report at | ||
https://github.com/Microsoft/vscode-python/issues""") | ||
sys.exit(1) | ||
|
||
LAST = _vspu.to_bytes('LAST') | ||
OUTP = _vspu.to_bytes('OUTP') | ||
LOAD = _vspu.to_bytes('LOAD') | ||
|
||
def parse_argv(): | ||
"""Parses arguments for use with the launcher. | ||
Arguments are: | ||
1. Working directory. | ||
2. VS debugger port to connect to. | ||
3. GUID for the debug session. | ||
4. Debug options (not used). | ||
5. '-m' or '-c' to override the default run-as mode. [optional]. | ||
6. Startup script name. | ||
7. Script arguments. | ||
""" | ||
|
||
# Change to directory we expected to start from. | ||
os.chdir(sys.argv[1]) | ||
|
||
port_num = int(sys.argv[2]) | ||
debug_id = sys.argv[3] | ||
|
||
del sys.argv[:5] | ||
|
||
# Set run_as mode appropriately | ||
run_as = 'script' | ||
if sys.argv and sys.argv[0] == '-m': | ||
run_as = 'module' | ||
del sys.argv[0] | ||
elif sys.argv and sys.argv[0] == '-c': | ||
run_as = 'code' | ||
del sys.argv[0] | ||
|
||
# Preserve filename before we del sys. | ||
filename = sys.argv[0] | ||
|
||
# Fix sys.path to be the script file dir. | ||
sys.path[0] = '' | ||
|
||
pid = os.getpid() | ||
|
||
return (filename, port_num, debug_id, pid, run_as) | ||
|
||
def run(file, port_num, debug_id, pid, run_as='script'): | ||
attach_process(port_num, pid, debug_id) | ||
|
||
# Now execute main file. | ||
globals_obj = {'__name__': '__main__'} | ||
|
||
try: | ||
if run_as == 'module': | ||
_vspu.exec_module(file, globals_obj) | ||
elif run_as == 'code': | ||
_vspu.exec_code(file, '<string>', globals_obj) | ||
else: | ||
_vspu.exec_file(file, globals_obj) | ||
except: | ||
exc_type, exc_value, exc_tb = sys.exc_info() | ||
handle_exception(exc_type, exc_value, exc_tb) | ||
|
||
_vspu.write_bytes(conn, LAST) | ||
# Wait for message to be received by debugger. | ||
time.sleep(0.5) | ||
|
||
|
||
def attach_process(port_num, pid, debug_id): | ||
global conn | ||
for i in xrange(50): | ||
try: | ||
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
conn.connect(('127.0.0.1', port_num)) | ||
# Initial handshake. | ||
_vspu.write_string(conn, debug_id) | ||
_vspu.write_int(conn, 0) | ||
_vspu.write_int(conn, pid) | ||
|
||
# Notify debugger that process has launched. | ||
_vspu.write_bytes(conn, LOAD) | ||
_vspu.write_int(conn, 0) | ||
break | ||
except: | ||
time.sleep(50./1000) | ||
else: | ||
raise Exception('failed to attach') | ||
|
||
|
||
def handle_exception(exc_type, exc_value, exc_tb): | ||
# Specifies list of files not to display in stack trace. | ||
do_not_debug = [__file__, _vspu.__file__] | ||
if sys.version_info >= (3, 3): | ||
do_not_debug.append('<frozen importlib._bootstrap>') | ||
if sys.version_info >= (3, 5): | ||
do_not_debug.append('<frozen importlib._bootstrap_external>') | ||
|
||
# Remove debugger frames from the top and bottom of the traceback. | ||
tb = traceback.extract_tb(exc_tb) | ||
for i in [0, -1]: | ||
while tb: | ||
frame_file = path.normcase(tb[i][0]) | ||
if not any(is_same_py_file(frame_file, f) for f in do_not_debug): | ||
break | ||
del tb[i] | ||
|
||
# Print the traceback. | ||
if tb: | ||
sys.stderr.write('Traceback (most recent call last):') | ||
for out in traceback.format_list(tb): | ||
sys.stderr.write(out) | ||
sys.stderr.flush() | ||
|
||
# Print the exception. | ||
for out in traceback.format_exception_only(exc_type, exc_value): | ||
sys.stderr.write(out) | ||
sys.stderr.flush() | ||
|
||
|
||
def is_same_py_file(file_1, file_2): | ||
"""Compares 2 filenames accounting for .pyc files.""" | ||
if file_1.endswith('.pyc') or file_1.endswith('.pyo'): | ||
file_1 = file_1[:-1] | ||
if file_2.endswith('.pyc') or file_2.endswith('.pyo'): | ||
file_2 = file_2[:-1] | ||
|
||
return file_1 == file_2 | ||
|
||
if __name__ == '__main__': | ||
filename, port_num, debug_id, pid, run_as = parse_argv() | ||
run(filename, port_num, debug_id, pid, run_as) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Run a block of code or Python file."""