From 270c6f55f7b6ffd18d5d0f9eb12944aa0b2e6cf6 Mon Sep 17 00:00:00 2001
From: Tulir Asokan <tulir@maunium.net>
Date: Sat, 31 Aug 2019 20:32:12 +0300
Subject: [PATCH] Minor improvements and fix stdin

---
 base-config.yaml      | 12 ++++++------
 exec/bot.py           | 20 ++++++++++----------
 exec/runner/python.py |  8 +++++---
 3 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/base-config.yaml b/base-config.yaml
index 5f52c85..e82cab8 100644
--- a/base-config.yaml
+++ b/base-config.yaml
@@ -30,10 +30,10 @@ output:
         Return:
         {{ return_value }}
         {% endif %}
-        {% if traceback != None %}
+        {% if exception != None %}
 
-        {% if traceback_header %}{{ traceback_header }}:{% endif %}
-        {{ traceback }}
+        {% if exception_header %}{{ exception_header }}:{% endif %}
+        {{ exception }}
         {% endif %}
         {% if duration != None %}
 
@@ -58,9 +58,9 @@ output:
             <pre>{{ return_value }}</pre>
             {% endif %}
         {% endif %}
-        {% if traceback != None %}
-            {% if traceback_header %}<h4>{{ traceback_header }}</h4>{% endif %}
-            <pre><code class="language-pytb">{{ traceback }}</code></pre>
+        {% if exception != None %}
+            {% if exception_header %}<h4>{{ exception_header }}</h4>{% endif %}
+            <pre><code class="language-pytb">{{ exception }}</code></pre>
         {% endif %}
         {% if duration != None %}
             <h4>Took {{ duration | round(3) }} seconds</h4>
diff --git a/exec/bot.py b/exec/bot.py
index 0a6219c..59eb2e9 100644
--- a/exec/bot.py
+++ b/exec/bot.py
@@ -30,7 +30,7 @@ from .runner import PythonRunner, ShellRunner, OutputType
 
 
 def escape(val: Optional[str]) -> Optional[str]:
-    return escape(val) if val else None
+    return escape_orig(val) if val is not None else None
 
 
 class EntityParser(MatrixParser[EntityString]):
@@ -74,19 +74,19 @@ class ExecBot(Plugin):
         self.html_template = Template(self.config["output.html"], **template_args)
 
     def format_status(self, code: str, language: str, output: str = "", output_html: str = "",
-                      return_value: Any = None, traceback: Optional[str] = None,
-                      traceback_header: Optional[str] = None, duration: Optional[float] = None,
+                      return_value: Any = None, exception_header: Optional[str] = None,
+                      exception: Optional[str] = None, duration: Optional[float] = None,
                       msgtype: MessageType = MessageType.NOTICE) -> TextMessageEventContent:
         return_value = repr(return_value) if return_value is not None else None
         content = TextMessageEventContent(
             msgtype=msgtype, format=Format.HTML,
             body=self.plaintext_template.render(
                 code=code, language=language, output=output, return_value=return_value,
-                duration=duration, traceback=traceback, traceback_header=traceback_header),
+                duration=duration, exception=exception, exception_header=exception_header),
             formatted_body=self.html_template.render(
                 code=escape(code), language=language, output=output_html,
-                return_value=escape(return_value), duration=duration, traceback=escape(traceback),
-                traceback_header=escape(traceback_header)))
+                return_value=escape(return_value), duration=duration, exception=escape(exception),
+                exception_header=escape(exception_header)))
         return content
 
     @event.on(EventType.ROOM_MESSAGE)
@@ -110,7 +110,7 @@ class ExecBot(Plugin):
             if not code:
                 code = value
                 lang = current_lang
-            elif lang == "stdin" or lang == "input":
+            elif current_lang == "stdin" or current_lang == "input":
                 stdin += value
         if not code or not lang:
             return
@@ -139,7 +139,7 @@ class ExecBot(Plugin):
         output = StringIO()
         output_html = StringIO()
         return_value: Any = None
-        traceback, traceback_header = None, None
+        exception_header, exception = None, None
         start_time = time()
         prev_output = start_time
         async for out_type, data in runner.run(code, stdin):
@@ -153,7 +153,7 @@ class ExecBot(Plugin):
                 return_value = data
                 continue
             elif out_type == OutputType.EXCEPTION:
-                traceback, traceback_header = runner.format_exception(data)
+                exception_header, exception = runner.format_exception(data)
                 continue
 
             cur_time = time()
@@ -166,7 +166,7 @@ class ExecBot(Plugin):
         duration = time() - start_time
         print(return_value)
         content = self.format_status(code, lang, output.getvalue(), output_html.getvalue(),
-                                     return_value, traceback, traceback_header, duration,
+                                     return_value, exception_header, exception, duration,
                                      msgtype=msgtype)
         content.set_edit(output_event_id)
         await self.client.send_message(evt.room_id, content)
diff --git a/exec/runner/python.py b/exec/runner/python.py
index 090da0d..1fdf0ad 100644
--- a/exec/runner/python.py
+++ b/exec/runner/python.py
@@ -94,8 +94,10 @@ class PythonRunner(Runner):
         sys.stdout = output.get_writer(OutputType.STDOUT)
         sys.stderr = output.get_writer(OutputType.STDERR)
         sys.stdin = stdin
-        yield output
-        sys.stdout, sys.stderr, sys.stdin = old_stdout, old_stderr, old_stdin
+        try:
+            yield output
+        finally:
+            sys.stdout, sys.stderr, sys.stdin = old_stdout, old_stderr, old_stdin
 
     @staticmethod
     def _format_exc(exception: Exception) -> str:
@@ -136,6 +138,6 @@ class PythonRunner(Runner):
             try:
                 return_value = await task
             except Exception:
-                yield (OutputType.EXCEPTION, sys.exc_info())
+                yield (OutputType.EXCEPTION, ExcInfo(*sys.exc_info()))
             else:
                 yield (OutputType.RETURN, return_value)
-- 
GitLab