FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-python-devicesimulator/src/debug_user_code.py at dev · microsoft/vscode-python-devicesimulator · GitHub
This repository was archived by the owner on Dec 23, 2021. It is now read-only.
microsoft
/
vscode-python-devicesimulator
Public archive
Notifications
You must be signed in to change notification settings
Fork
49
Star
177
Code
Issues
21
Pull requests
10
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-python-devicesimulator
/
src
/
debug_user_code.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
95 lines (73 loc) · 3.01 KB
Breadcrumbs
vscode-python-devicesimulator
/
src
/
debug_user_code.py
Copy path
File metadata and controls
95 lines (73 loc) · 3.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import
os
import
sys
import
traceback
from
pathlib
import
Path
import
python_constants
as
CONSTANTS
import
check_python_dependencies
from
common
import
utils
# will propagate errors if dependencies aren't sufficient
check_python_dependencies
.
check_for_dependencies
()
abs_path_to_parent_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
# Insert absolute path to Adafruit library for CPX into sys.path
abs_path_to_adafruit_lib
=
os
.
path
.
join
(
abs_path_to_parent_dir
,
CONSTANTS
.
ADAFRUIT_LIBRARY_NAME
)
sys
.
path
.
insert
(
0
,
abs_path_to_adafruit_lib
)
# Insert absolute path to Micropython libraries for micro:bit into sys.path
abs_path_to_micropython_lib
=
os
.
path
.
join
(
abs_path_to_parent_dir
,
CONSTANTS
.
MICROPYTHON_LIBRARY_NAME
)
sys
.
path
.
insert
(
0
,
abs_path_to_micropython_lib
)
# Insert absolute path to library for CLUE into sys.path
sys
.
path
.
insert
(
0
,
os
.
path
.
join
(
abs_path_to_parent_dir
,
CONSTANTS
.
CLUE
))
# Insert absolute path to Circuitpython libraries for CLUE into sys.path
sys
.
path
.
insert
(
0
,
os
.
path
.
join
(
abs_path_to_parent_dir
,
CONSTANTS
.
CIRCUITPYTHON
))
# This import must happen after the sys.path is modified
from
common
import
debugger_communication_client
# get board so we can get terminal handle
import
board
# get handle to terminal for clue
curr_terminal
=
board
.
DISPLAY
.
terminal
## Execute User Code ##
# Get user's code path
abs_path_to_code_file
=
""
if
len
(
sys
.
argv
)
>
1
and
sys
.
argv
[
1
]:
abs_path_to_code_file
=
sys
.
argv
[
1
]
else
:
raise
FileNotFoundError
(
CONSTANTS
.
ERROR_NO_FILE
)
# Get Debugger Server Port
server_port
=
CONSTANTS
.
DEFAULT_PORT
if
len
(
sys
.
argv
)
>
2
:
server_port
=
sys
.
argv
[
2
]
# Init Communication
debugger_communication_client
.
init_connection
(
server_port
)
# Init API variables
utils
.
abs_path_to_user_file
=
abs_path_to_code_file
utils
.
debug_mode
=
True
# overriding print function so that it shows on clue terminal
def
print_decorator
(
func
):
global
curr_terminal
def
wrapped_func
(
*
args
,
**
kwargs
):
curr_terminal
.
add_str_to_terminal
(
""
.
join
(
str
(
e
)
for
e
in
args
))
return
func
(
*
args
,
**
kwargs
)
return
wrapped_func
print
=
print_decorator
(
print
)
# Execute the user's code file
with
open
(
abs_path_to_code_file
,
encoding
=
"utf8"
)
as
user_code_file
:
curr_terminal
.
add_str_to_terminal
(
CONSTANTS
.
CODE_START_MSG_CLUE
)
user_code
=
user_code_file
.
read
()
try
:
codeObj
=
compile
(
user_code
,
abs_path_to_code_file
,
CONSTANTS
.
EXEC_COMMAND
)
exec
(
codeObj
, {
"print"
:
print
})
sys
.
stdout
.
flush
()
except
Exception
as
e
:
exc_type
,
exc_value
,
exc_traceback
=
sys
.
exc_info
()
errorMessage
=
CONSTANTS
.
ERROR_TRACEBACK
stackTrace
=
traceback
.
format_exception
(
exc_type
,
exc_value
,
exc_traceback
)
for
frameIndex
in
range
(
2
,
len
(
stackTrace
)
-
1
):
errorMessage
+=
"
\t
"
+
str
(
stackTrace
[
frameIndex
])
print
(
e
,
errorMessage
,
file
=
sys
.
stderr
,
flush
=
True
)
curr_terminal
.
add_str_to_terminal
(
CONSTANTS
.
CODE_FINISHED_MSG_CLUE
)
board
.
DISPLAY
.
show
(
None
)
Back
|
FazBrowse Home
|
New Git URL