diff options
Diffstat (limited to 'macros/latex/contrib/pythonimmediate')
-rw-r--r-- | macros/latex/contrib/pythonimmediate/README | 5 | ||||
-rw-r--r-- | macros/latex/contrib/pythonimmediate/pythonimmediate.pdf | bin | 432126 -> 431593 bytes | |||
-rw-r--r-- | macros/latex/contrib/pythonimmediate/pythonimmediate.sty | 56 | ||||
-rw-r--r-- | macros/latex/contrib/pythonimmediate/pythonimmediate.tex | 17 | ||||
-rw-r--r-- | macros/latex/contrib/pythonimmediate/pythonimmediate_helper.lua | 20 |
5 files changed, 73 insertions, 25 deletions
diff --git a/macros/latex/contrib/pythonimmediate/README b/macros/latex/contrib/pythonimmediate/README index be4026bf43..43c413e62c 100644 --- a/macros/latex/contrib/pythonimmediate/README +++ b/macros/latex/contrib/pythonimmediate/README @@ -1,10 +1,9 @@ pythonimmediate -- Library to run Python code - \fileinfo. Released under the LaTeX Project Public License v1.3c or later See http://www.latex-project.org/lppl.txt -Report bugs at https://github.com/user202729/TeXlib +Report bugs at https://github.com/user202729/pythonimmediate-tex ======== @@ -21,4 +20,4 @@ This work has the LPPL maintenance status `maintained'. The Current Maintainer of this work is user202729. -This work consists of the files pythonimmediate.sty. +This work consists of the files pythonimmediate.sty, pythonimmediate_helper.lua. diff --git a/macros/latex/contrib/pythonimmediate/pythonimmediate.pdf b/macros/latex/contrib/pythonimmediate/pythonimmediate.pdf Binary files differindex c9bf716c59..5b8bf34442 100644 --- a/macros/latex/contrib/pythonimmediate/pythonimmediate.pdf +++ b/macros/latex/contrib/pythonimmediate/pythonimmediate.pdf diff --git a/macros/latex/contrib/pythonimmediate/pythonimmediate.sty b/macros/latex/contrib/pythonimmediate/pythonimmediate.sty index 5a92dd88a6..4556973831 100644 --- a/macros/latex/contrib/pythonimmediate/pythonimmediate.sty +++ b/macros/latex/contrib/pythonimmediate/pythonimmediate.sty @@ -12,7 +12,7 @@ % % The Current Maintainer of this work is user202729. -\ProvidesExplPackage{pythonimmediate}{2023/01/09}{0.1.0}{Library to run Python code} +\ProvidesExplPackage{pythonimmediate}{2023/03/07}{0.4.0}{Library to run Python code} \RequirePackage{saveenv} \RequirePackage{currfile} @@ -44,9 +44,9 @@ % e.g. \__begingroup_setup_estr: \exp_args:NNx \endgroup \__function:n {⟨text⟩} % will eventually execute to \__function:n {⟨estr-expansion of text⟩} -\str_set:Nn \_pythonimmediate_args{--mode=multiprocessing-network} -\str_set:Nn \_pythonimmediate_python_executable{python3} -\str_set:Nn \_pythonimmediate_python_flags{} +\str_gset:Nn \_pythonimmediate_args{} +\str_gset:Nn \_pythonimmediate_python_executable{python3} +\str_gset:Nn \_pythonimmediate_python_flags{} \keys_define:nn{pythonimmediate}{ args.tl_gset:N =\_pythonimmediate_args, python-executable.tl_gset:N=\_pythonimmediate_python_executable, @@ -61,8 +61,9 @@ \str_gset:Nx \_pythonimmediate_python_flags { \_pythonimmediate_python_flags } \endgroup -\msg_new:nnn {pythonimmediate} {shell-fail} {Please~enable~unrestricted~shell~scape!} -\msg_new:nnn {pythonimmediate} {cannot-read-line} {Cannot~read~line!} +\msg_new:nnn {pythonimmediate} {shell-fail} {Please~enable~unrestricted~shell~escape!} +\msg_new:nnn {pythonimmediate} {process-start-error} {Cannot~start~Python~process!~ + Make~sure~package~options~are~correct~and~the~Python~package~is~installed.} \msg_new:nnn {pythonimmediate} {internal-error} {Internal~error!} \sys_if_shell_unrestricted:F { @@ -96,21 +97,52 @@ % ======== setup write file ======== -\newwrite \_pythonimmediate_write_file +\cs_new_protected:Npn \_pythonimmediate_send_content:e #1 { + \immediate\write \_pythonimmediate_write_file { #1 } +} + +\cs_new_protected:Npn \_pythonimmediate_close_write: { + \immediate\closeout \_pythonimmediate_write_file + % safeguard, in some mode e.g. nonstopmode after a Python error TeX might continue + \cs_gset_eq:Nc \_pythonimmediate_write_file {m@ne} + \cs_gset_protected:Npn \_pythonimmediate_send_content:e ##1 {} +} + \bool_if:NTF \_pythonimmediate_child_process { + \newwrite \_pythonimmediate_write_file %\immediate\openout \_pythonimmediate_write_file=symlink-to-stderr.txt~ % tried this method, it seems to work except that it's buffered when stderr is redirected to a file... \immediate\openout \_pythonimmediate_write_file=|"\_pythonimmediate_python_executable \space \_pythonimmediate_python_flags \space -m ~ pythonimmediate.copy_to_stderr"~ + + } { - \immediate\openout \_pythonimmediate_write_file=|"\_pythonimmediate_python_executable \space \_pythonimmediate_python_flags \space -m ~ pythonimmediate.textopy"~ - % note that openout, even to shell, will append .tex if there's no dot in the command - % so we artificially add a dot here in `.textopy` + + \sys_if_engine_luatex:TF { + % use lua's io.popen + % the other method works as well, but this one allows explicit flushing because on some TeX distribution it does not work + + \directlua{ + (require "pythonimmediate_helper")( + "\luaescapestring{ \_pythonimmediate_python_executable \space \_pythonimmediate_python_flags \space -m ~ pythonimmediate.textopy ~ \_pythonimmediate_args }" + ) + } + } { + \newwrite \_pythonimmediate_write_file + + \immediate\openout \_pythonimmediate_write_file=|"\_pythonimmediate_python_executable \space \_pythonimmediate_python_flags \space -m ~ pythonimmediate.textopy ~ \_pythonimmediate_args"~ + % note that openout, even to shell, will append .tex if there's no dot in the command + % so we artificially add a dot here in `.textopy` + } + % the command-line arguments passed to textopy above are only used initially before establishing a connection + % after connection is made then the configuration object is sent over + + % both processes must be before the \readline below so that the 2 Python processes are started "in parallel" \_pythonimmediate_str_get:N \_pythonimmediate_line % read one line from pytotex half, forward to textopy half \str_if_eq:VnT \_pythonimmediate_line {} { - \msg_error:nn {pythonimmediate} {cannot-read-line} + \msg_error:nn {pythonimmediate} {process-start-error} } \str_const:Nn \_pythonimmediate_engine_mark_pdftex {p} @@ -120,7 +152,7 @@ \str_const:Nn \_pythonimmediate_engine_mark_xetex {x} \str_const:Nn \_pythonimmediate_engine_mark_luatex {l} - \immediate\write\_pythonimmediate_write_file { + \_pythonimmediate_send_content:e { \use:c {_pythonimmediate_engine_mark_ \c_sys_engine_str} \_pythonimmediate_line } diff --git a/macros/latex/contrib/pythonimmediate/pythonimmediate.tex b/macros/latex/contrib/pythonimmediate/pythonimmediate.tex index eacffa24f6..539a770fc2 100644 --- a/macros/latex/contrib/pythonimmediate/pythonimmediate.tex +++ b/macros/latex/contrib/pythonimmediate/pythonimmediate.tex @@ -1,4 +1,4 @@ -\ProvidesFile{pythonimmediate.tex}[2023/01/09 0.1.0 Library to run Python code] +\ProvidesFile{pythonimmediate.tex}[2023/03/07 0.4.0 Library to run Python code] \RequirePackage{fvextra} \documentclass{l3doc} \usepackage{tikz} @@ -32,10 +32,6 @@ \maketitle -\begin{abstract} - \fileinfo. -\end{abstract} - \section{Motivation} Just like \pkg{Perl\TeX} or \pkg{PyLua\TeX} (and unlike \pkg{Python\TeX} or \pkg{lt3luabridge}), @@ -57,7 +53,7 @@ In addition to the \LaTeX\ package, you need the Python \pkg{pythonimmediate-tex installation instruction can be found at \url{https://pypi.org/project/pythonimmediate-tex/}. Note that not all \TeX\ package versions are compatible with all Python package versions. -This \TeX\ package is compatible with Python package version |0.1.2|. +This \TeX\ package is compatible with Python package version |0.4.0|. Remember to enable unrestricted\footnote{ There's little point in supporting restricted mode, since it's possible to execute arbitrary shell commands in Python anyway. @@ -80,9 +76,10 @@ Instruction: \item \file{saveenv.sty} \item \file{precattl.sty} \item \file{pythonimmediate.sty} - \item \file{pythonimmediate-tex-0.1.2.zip} + \item \file{pythonimmediate_helper.lua} + \item \file{pythonimmediate-tex-0.4.0.zip} \end{itemize} - The |0.1.2| part should be replaced with the desired version + The |0.4.0| part should be replaced with the desired version of the Python package. The |.sty| files can be downloaded from CTAN at @@ -97,9 +94,9 @@ Instruction: \item Write the following in the preamble: \begin{verbatim} \usepackage[abspath]{currfile} -\usepackage[python-executable={PYTHONPATH=pythonimmediate-tex-0.1.2.zip/pythonimmediate-tex-0.1.2/ python3},args={--mode=unnamed-pipe}]{pythonimmediate} +\usepackage[python-executable={PYTHONPATH=pythonimmediate-tex-0.4.0.zip/pythonimmediate-tex-0.4.0/ python3},args={--mode=unnamed-pipe}]{pythonimmediate} \end{verbatim} -As above, replace both occurrences of |0.1.2| with the downloaded version specified in the zip file above. +As above, replace both occurrences of |0.4.0| with the downloaded version specified in the zip file above. Refer to \ref{troubleshoot-source-file-not-found} for explanation of the |abspath| option. \end{itemize} diff --git a/macros/latex/contrib/pythonimmediate/pythonimmediate_helper.lua b/macros/latex/contrib/pythonimmediate/pythonimmediate_helper.lua new file mode 100644 index 0000000000..8590233242 --- /dev/null +++ b/macros/latex/contrib/pythonimmediate/pythonimmediate_helper.lua @@ -0,0 +1,20 @@ +return function(cmd) + local process = io.popen(cmd, "w") + + local function_table=lua.get_functions_table() + + -- https://tex.stackexchange.com/questions/632408/how-can-i-exclude-tex-macros-when-counting-a-strings-characters-in-lua/632464?noredirect=1#comment1623008_632464 this only work in Lua 5.3 or assume it's allocated sequentially + local send_content_index=#function_table+1 + function_table[send_content_index]=function() + process:write(token.scan_string()) + process:write("\n") + process:flush() + end + + local close_index=#function_table+1 + function_table[close_index]=function() + process:close() + end + + tex.print([[\protected \luadef \_pythonimmediate_send_content:e ]] .. send_content_index .. [[\protected \luadef \_pythonimmediate_close_write: ]] .. close_index .. [[\relax]]) + end |