summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2023-03-07 22:21:22 +0000
committerKarl Berry <karl@freefriends.org>2023-03-07 22:21:22 +0000
commit402087280b756fd1a5df45aec31ddf0ac337c428 (patch)
treea147d24df237deecdd26ad9ae5bf0f921a05d403
parentdc2bf9071c2f7283d4964addac6bbe2c93e11773 (diff)
pythonimmediate (7mar23)
git-svn-id: svn://tug.org/texlive/trunk@66438 c570f23f-e606-0410-a88d-b1316a301751
-rw-r--r--Master/texmf-dist/doc/latex/pythonimmediate/README5
-rw-r--r--Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdfbin432126 -> 431593 bytes
-rw-r--r--Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex17
-rw-r--r--Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty56
-rw-r--r--Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua20
5 files changed, 73 insertions, 25 deletions
diff --git a/Master/texmf-dist/doc/latex/pythonimmediate/README b/Master/texmf-dist/doc/latex/pythonimmediate/README
index be4026bf438..43c413e62c2 100644
--- a/Master/texmf-dist/doc/latex/pythonimmediate/README
+++ b/Master/texmf-dist/doc/latex/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/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdf b/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdf
index c9bf716c597..5b8bf34442d 100644
--- a/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdf
+++ b/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdf
Binary files differ
diff --git a/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex b/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex
index eacffa24f66..539a770fc29 100644
--- a/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex
+++ b/Master/texmf-dist/doc/latex/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/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty b/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty
index 5a92dd88a6c..45569738312 100644
--- a/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty
+++ b/Master/texmf-dist/tex/latex/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/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua b/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua
new file mode 100644
index 00000000000..8590233242c
--- /dev/null
+++ b/Master/texmf-dist/tex/latex/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