From 5c835cb7fc402dcaf0f66aad522767cb5d078237 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sat, 6 Nov 2021 20:40:00 +0000 Subject: pyluatex (6nov21) git-svn-id: svn://tug.org/texlive/trunk@60977 c570f23f-e606-0410-a88d-b1316a301751 --- .../tex/lualatex/pyluatex/pyluatex-interpreter.py | 37 ++++++++++- .../texmf-dist/tex/lualatex/pyluatex/pyluatex.lua | 73 +++++++++++++++++----- .../texmf-dist/tex/lualatex/pyluatex/pyluatex.sty | 29 ++++++--- 3 files changed, 114 insertions(+), 25 deletions(-) (limited to 'Master/texmf-dist/tex/lualatex/pyluatex') diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py index 4a17b5f5b0f..0e8b32035fd 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py +++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex-interpreter.py @@ -30,8 +30,36 @@ import socketserver import json import textwrap from collections import defaultdict +import re class Interpreter(InteractiveInterpreter): + def execute_repl(self, code, ignore_errors): + self.success = True + output = '' + incomplete = False + for line in re.split('\r?\n', code): + output += ('... ' if incomplete else '>>> ') + line + '\n' + if incomplete: + buffer += '\n' + line + else: + buffer = line + with StringIO() as out, redirect_stdout(out), redirect_stderr(out): + try: + code_obj = compile_command(buffer) + if code_obj is not None: + incomplete = False + self.runcode(code_obj) + else: + incomplete = True + except: + incomplete = False + traceback.print_exc(limit=0) + self.success = False + output += out.getvalue() + if not ignore_errors and not self.success: + return False, output + return self.success, output + def execute(self, code): with StringIO() as out, redirect_stdout(out), redirect_stderr(out): self.success = True @@ -61,7 +89,14 @@ class Handler(socketserver.StreamRequestHandler): data = json.loads(data) interpreter = interpreters[data['session']] - success, output = interpreter.execute(textwrap.dedent(data['code'])) + code = textwrap.dedent(data['code']) + if data['repl_mode']: + success, output = interpreter.execute_repl( + code, + data['ignore_errors'] + ) + else: + success, output = interpreter.execute(code) response = { 'success': success, 'output': output } self.wfile.write((json.dumps(response) + '\n').encode('utf-8')) diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua index d7e27bc6cb9..6e9c6abb631 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua +++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua @@ -26,6 +26,7 @@ local json = require("pyluatex-json") local socket = require("socket") pyluatex = pyluatex or { + ignore_errors = false, verbose = false, session = "default" } @@ -38,10 +39,15 @@ local tcp = nil local env_end = nil local env_lines = nil local parent_env = nil +local env_repl_mode = false local last_code = nil local last_output = nil +local function trim(s) + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + local function err_cmd(message) return "\\PackageError{PyLuaTeX}{" .. message .. "}{}" end @@ -94,7 +100,7 @@ local function split_lines(str) return t end -function pyluatex.execute(code, auto_print, write) +function pyluatex.execute(code, auto_print, write, repl_mode) local full_code if auto_print then full_code = "print(str(" .. code .. "), end='')" @@ -104,12 +110,19 @@ function pyluatex.execute(code, auto_print, write) if pyluatex.verbose then log_input(full_code) end - local success, output = request({ session = pyluatex.session, code = full_code }) + local success, output = request( + { + session = pyluatex.session, + code = full_code, + repl_mode = repl_mode, + ignore_errors = pyluatex.ignore_errors + } + ) last_code = split_lines(code) last_output = split_lines(output) - if success then - if pyluatex.verbose then log_output(output) end + if success or pyluatex.ignore_errors then + if pyluatex.verbose or not success then log_output(output) end if write then tex.print(last_output) @@ -135,10 +148,14 @@ local function record_line(line) local s, e = line:find(env_end, 1, true) if s ~= nil then luatexbase.remove_from_callback("process_input_buffer", "pyluatex_record_line") - table.insert(env_lines, line:sub(1, s - 1)) + local code_in_line = line:sub(1, s - 1) + if trim(code_in_line):len() > 0 then + -- only include this line if it contains non-whitespace characters + table.insert(env_lines, code_in_line) + end local code = table.concat(env_lines, "\n") - local success = pyluatex.execute(code, false, false) - if success then + local success = pyluatex.execute(code, false, false, env_repl_mode) + if success or pyluatex.ignore_errors then return line:sub(s) else return env_end .. err_cmd("Python error (see above)") .. line:sub(e + 1) @@ -149,20 +166,14 @@ local function record_line(line) end end -function pyluatex.record_env(quiet) - local name +function pyluatex.record_env(name, repl_mode) if parent_env ~= nil then name = parent_env parent_env = nil - else - if quiet then - name = "pythonq" - else - name = "python" - end end env_end = "\\end{" .. name .. "}" env_lines = {} + env_repl_mode = repl_mode luatexbase.add_to_callback("process_input_buffer", record_line, "pyluatex_record_line") end @@ -172,12 +183,18 @@ function pyluatex.set_parent_env(name) end end -function pyluatex.run_file(path, write) +function pyluatex.run_file(path, write, repl_mode) local f = io.open(path, "r") if f then local code = f:read("*a") f:close() - pyluatex.execute(code, false, write) + -- ignore trailing new line if present + if code:sub(-2) == "\r\n" then + code = code:sub(0, -3) + elseif code:sub(-1) == "\n" then + code = code:sub(0, -2) + end + pyluatex.execute(code, false, write, repl_mode) else tex.sprint(err_cmd("File not found: " .. path)) end @@ -191,4 +208,26 @@ function pyluatex.get_last_output() return last_output end +local function parse_bool(name, value) + if value == "true" then + return true + elseif value == "false" then + return false + else + tex.sprint( + err_cmd("Invalid value '" .. value .. "' for option " .. name) + ) + end +end + +function pyluatex.set_option(name, value) + if name == "ignoreerrors" then + pyluatex.ignore_errors = parse_bool(name, value) + elseif name == "verbose" then + pyluatex.verbose = parse_bool(name, value) + else + tex.sprint(err_cmd("Unknown option '" .. name .. "'")) + end +end + return pyluatex diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty index 81fa87ea827..ab015d9638d 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty +++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty @@ -9,7 +9,7 @@ %% version 2005/12/01 or later. \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{pyluatex}[2021/08/07 v0.3.0 Execute Python code on the fly] +\ProvidesPackage{pyluatex}[2021/11/05 v0.4.0 Execute Python code on the fly] \RequirePackage{expl3} \ExplSyntaxOn @@ -25,19 +25,32 @@ \RequirePackage{kvoptions} \DeclareStringOption[python3]{executable} -\DeclareVoidOption{verbose}{\directlua{pyluatex.verbose = true}} +\DeclareBoolOption{ignoreerrors} +\DeclareBoolOption{verbose} \ProcessKeyvalOptions* +\ifpyluatex@ignoreerrors + \directlua{pyluatex.ignore_errors = true} +\else + \directlua{pyluatex.ignore_errors = false} +\fi +\ifpyluatex@verbose + \directlua{pyluatex.verbose = true} +\else + \directlua{pyluatex.verbose = false} +\fi \directlua{pyluatex.start([==[\pyluatex@executable]==])} \newcommand*{\PyLTVerbatimEnv}{\directlua{pyluatex.set_parent_env([==[\@currenvir]==])}} -\newenvironment{python}{\directlua{pyluatex.record_env(false)}}% +\newenvironment{python}{\directlua{pyluatex.record_env("python", false)}}% {\directlua{pyluatex.print_env()}} -\newenvironment{pythonq}{\directlua{pyluatex.record_env(true)}}{} +\newenvironment{pythonq}{\directlua{pyluatex.record_env("pythonq", false)}}{} -\newcommand*{\pyluatex@inline}[3]{\directlua{pyluatex.execute([==[#1]==], #2, #3)}} +\newenvironment{pythonrepl}{\directlua{pyluatex.record_env("pythonrepl", true)}}{} + +\newcommand*{\pyluatex@inline}[3]{\directlua{pyluatex.execute([==[#1]==], #2, #3, false)}} \newcommand*{\py}[1]{\pyluatex@inline{#1}{true}{true}} \newcommand*{\pyq}[1]{\pyluatex@inline{#1}{true}{false}} @@ -46,8 +59,10 @@ \newcommand*{\pycq}[1]{\pyluatex@inline{#1}{false}{false}} \newcommand*{\pysession}[1]{\directlua{pyluatex.session = [==[#1]==]}} +\newcommand*{\pyoption}[2]{\directlua{pyluatex.set_option([==[#1]==], [==[#2]==])}} -\newcommand*{\pyfile}[1]{\directlua{pyluatex.run_file([==[#1]==], true)}} -\newcommand*{\pyfileq}[1]{\directlua{pyluatex.run_file([==[#1]==], false)}} +\newcommand*{\pyfile}[1]{\directlua{pyluatex.run_file([==[#1]==], true, false)}} +\newcommand*{\pyfileq}[1]{\directlua{pyluatex.run_file([==[#1]==], false, false)}} +\newcommand*{\pyfilerepl}[1]{\directlua{pyluatex.run_file([==[#1]==], false, true)}} \endinput -- cgit v1.2.3