Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.
Merged
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
24 changes: 23 additions & 1 deletion src/debug_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@
# 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))

# get board so we can get terminal handle
import board

# This import must happen after the sys.path is modified
from common import debugger_communication_client

# get handle to terminal for clue
curr_terminal = board.DISPLAY.terminal

## Execute User Code ##

# Get user's code path
Expand All @@ -56,12 +62,26 @@
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, {})
exec(codeObj, {"print": print})
sys.stdout.flush()
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand All @@ -71,3 +91,5 @@
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)