Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit a2e6c53

Browse files
authored
Fixed print() statements for CLUE display in debug mode (#351)
* fixed debug_user_code.py to capture print statements * added starting and ending print statements
1 parent f6d6122 commit a2e6c53

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/debug_user_code.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@
3232
# Insert absolute path to Circuitpython libraries for CLUE into sys.path
3333
sys.path.insert(0, os.path.join(abs_path_to_parent_dir, CONSTANTS.CIRCUITPYTHON))
3434

35+
# get board so we can get terminal handle
36+
import board
37+
3538
# This import must happen after the sys.path is modified
3639
from common import debugger_communication_client
3740

41+
# get handle to terminal for clue
42+
curr_terminal = board.DISPLAY.terminal
43+
3844
## Execute User Code ##
3945

4046
# Get user's code path
@@ -56,12 +62,26 @@
5662
utils.abs_path_to_user_file = abs_path_to_code_file
5763
utils.debug_mode = True
5864

65+
# overriding print function so that it shows on clue terminal
66+
def print_decorator(func):
67+
global curr_terminal
68+
69+
def wrapped_func(*args, **kwargs):
70+
curr_terminal.add_str_to_terminal("".join(str(e) for e in args))
71+
return func(*args, **kwargs)
72+
73+
return wrapped_func
74+
75+
76+
print = print_decorator(print)
77+
5978
# Execute the user's code file
6079
with open(abs_path_to_code_file, encoding="utf8") as user_code_file:
80+
curr_terminal.add_str_to_terminal(CONSTANTS.CODE_START_MSG_CLUE)
6181
user_code = user_code_file.read()
6282
try:
6383
codeObj = compile(user_code, abs_path_to_code_file, CONSTANTS.EXEC_COMMAND)
64-
exec(codeObj, {})
84+
exec(codeObj, {"print": print})
6585
sys.stdout.flush()
6686
except Exception as e:
6787
exc_type, exc_value, exc_traceback = sys.exc_info()
@@ -71,3 +91,5 @@
7191
for frameIndex in range(2, len(stackTrace) - 1):
7292
errorMessage += "\t" + str(stackTrace[frameIndex])
7393
print(e, errorMessage, file=sys.stderr, flush=True)
94+
curr_terminal.add_str_to_terminal(CONSTANTS.CODE_FINISHED_MSG_CLUE)
95+
board.DISPLAY.show(None)

0 commit comments

Comments
 (0)