From 38f85f789eb5540ea0ed7302b89919e04014e1f5 Mon Sep 17 00:00:00 2001 From: andreamah Date: Mon, 20 Apr 2020 17:27:04 -0700 Subject: [PATCH 1/2] fixed debug_user_code.py to capture print statements --- src/debug_user_code.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/debug_user_code.py b/src/debug_user_code.py index 7e534f479..009deb031 100644 --- a/src/debug_user_code.py +++ b/src/debug_user_code.py @@ -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 @@ -56,12 +62,23 @@ 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(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: + 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() From d28630c126c9315b4c6f8ad09df616b6e4138fbe Mon Sep 17 00:00:00 2001 From: andreamah Date: Mon, 20 Apr 2020 17:43:25 -0700 Subject: [PATCH 2/2] added starting and ending print statements --- src/debug_user_code.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/debug_user_code.py b/src/debug_user_code.py index 009deb031..8be4792fb 100644 --- a/src/debug_user_code.py +++ b/src/debug_user_code.py @@ -65,20 +65,23 @@ # 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(args)) - return func(*args,**kwargs) + + 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}) + exec(codeObj, {"print": print}) sys.stdout.flush() except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() @@ -88,3 +91,5 @@ def wrapped_func(*args,**kwargs): 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)