summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/latex/pyluatex/pyluatex-interpreter.py')
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex-interpreter.py37
1 files changed, 32 insertions, 5 deletions
diff --git a/macros/luatex/latex/pyluatex/pyluatex-interpreter.py b/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
index 28ec28e12e..341c2ace04 100644
--- a/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
+++ b/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
@@ -34,7 +34,30 @@ import re
import os
import sys
+class PyLTTex:
+ def __init__(self):
+ self._log_buffer = []
+
+ def log(self, *objects, sep=' ', end='\n'):
+ try:
+ objects = [str(v) for v in objects]
+ except Exception as exc:
+ raise RuntimeError(
+ 'The object to log could not be transformed to a string.',
+ ) from exc
+ self._log_buffer.append(sep.join(objects))
+ self._log_buffer.append(end)
+
+ def _log_message(self):
+ msg = ''.join(self._log_buffer)
+ self._log_buffer = []
+ return msg
+
class Interpreter(InteractiveInterpreter):
+ def __init__(self):
+ self.tex = PyLTTex()
+ super().__init__({'tex': self.tex})
+
def execute_repl(self, code, ignore_errors):
self.success = True
output = ''
@@ -60,7 +83,7 @@ class Interpreter(InteractiveInterpreter):
output += out.getvalue()
if not ignore_errors and not self.success:
return False, output
- return self.success, output
+ return self.success, output, self.tex._log_message()
def execute(self, code):
with StringIO() as out, redirect_stdout(out), redirect_stderr(out):
@@ -75,7 +98,7 @@ class Interpreter(InteractiveInterpreter):
except:
traceback.print_exc()
self.success = False
- return self.success, out.getvalue()
+ return self.success, out.getvalue(), self.tex._log_message()
def showtraceback(self):
super().showtraceback()
@@ -95,13 +118,17 @@ class Handler(socketserver.StreamRequestHandler):
interpreter = interpreters[data['session']]
code = textwrap.dedent(data['code'])
if data['repl_mode']:
- success, output = interpreter.execute_repl(
+ success, output, log_msg = interpreter.execute_repl(
code,
data['ignore_errors']
)
else:
- success, output = interpreter.execute(code)
- response = { 'success': success, 'output': output }
+ success, output, log_msg = interpreter.execute(code)
+ response = {
+ 'success': success,
+ 'output': output,
+ 'log_msg': log_msg
+ }
self.wfile.write((json.dumps(response) + '\n').encode('utf-8'))
if __name__ == '__main__':