From 18c0d8803f02fb1558c83447e90e5bb7bdddb1be Mon Sep 17 00:00:00 2001 From: angelasofiaremolinagutierrez Date: Sun, 27 Jun 2021 18:03:26 -0500 Subject: [PATCH 1/3] console redirecting sys.stderr and sys.stdout --- runestone/activecode/js/activecode_brython.js | 79 +++++++++++++++++-- runestone/activecode/test/_sources/index.rst | 2 +- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/runestone/activecode/js/activecode_brython.js b/runestone/activecode/js/activecode_brython.js index c9415cf54..8ece2fb10 100644 --- a/runestone/activecode/js/activecode_brython.js +++ b/runestone/activecode/js/activecode_brython.js @@ -23,13 +23,78 @@ export default class BrythonActiveCode extends ActiveCode { $(this.outDiv).show({ duration: 700, queue: false }); prog = ` - - - - - - + + + + + + + + +
+                
+            
+ + + `; this.output.srcdoc = prog; diff --git a/runestone/activecode/test/_sources/index.rst b/runestone/activecode/test/_sources/index.rst index 1eb9e4158..538b9a68e 100644 --- a/runestone/activecode/test/_sources/index.rst +++ b/runestone/activecode/test/_sources/index.rst @@ -2832,7 +2832,7 @@ Trying Brython as Python 3 interpreter :language: python3 :python3_interpreter: brython - print("You can see this print on the browser console") + print("You can see this print inside the iframe console") from browser import document, alert, html def hello(ev): From 9b85f9f9ae8383caf31e58ef45f13a7af3da5dae Mon Sep 17 00:00:00 2001 From: angelasofiaremolinagutierrez Date: Mon, 28 Jun 2021 18:54:27 -0500 Subject: [PATCH 2/3] improved error formatting --- runestone/activecode/js/activecode_brython.js | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/runestone/activecode/js/activecode_brython.js b/runestone/activecode/js/activecode_brython.js index 8ece2fb10..89e007a39 100644 --- a/runestone/activecode/js/activecode_brython.js +++ b/runestone/activecode/js/activecode_brython.js @@ -44,7 +44,6 @@ export default class BrythonActiveCode extends ActiveCode { From 9c3be22ecf3a7a9766ca321391d4d2bbb1a6e393 Mon Sep 17 00:00:00 2001 From: angelasofiaremolinagutierrez Date: Mon, 28 Jun 2021 23:21:35 -0500 Subject: [PATCH 3/3] fix NameError bug (when no line is specified) --- runestone/activecode/js/activecode_brython.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/runestone/activecode/js/activecode_brython.js b/runestone/activecode/js/activecode_brython.js index 89e007a39..2ede073d9 100644 --- a/runestone/activecode/js/activecode_brython.js +++ b/runestone/activecode/js/activecode_brython.js @@ -60,7 +60,7 @@ import traceback def my_exec(code): try: - exec(code) + exec(code, locals()) preElem = document['consolePre'] preElem.style.visibility = "visible" preElem.style.bottom = "5px" @@ -68,20 +68,25 @@ def my_exec(code): except SyntaxError as err: error_class = err.__class__.__name__ detail = err.args[0] - line_number = err.lineno - except Exception as err: + line_number = f"at line {err.lineno}" + except BaseException as err: error_class = err.__class__.__name__ detail = err.args[0] cl, exc, tb = sys.exc_info() - line_number = traceback.extract_tb(tb)[-1][1] + # When errors don't specify a line + try: + line_number = f"at line {traceback.extract_tb(tb)[-1][1]}" + except: + line_number = "" else: return # This is only done if an Exception was catched - result = f"'{error_class}': {detail} at line {line_number}." + result = f"'{error_class}': {detail} {line_number}" print(result) logger = document['consoleCode'] preElem = document['consolePre'] + # Styling the pre element for error error_header = document.createElement("h3") error_header.innerHTML = "Error" error_header.style.font = "24px 'Arial'"