summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex
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 /Master/texmf-dist/tex
parentdc2bf9071c2f7283d4964addac6bbe2c93e11773 (diff)
pythonimmediate (7mar23)
git-svn-id: svn://tug.org/texlive/trunk@66438 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex')
-rw-r--r--Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty56
-rw-r--r--Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua20
2 files changed, 64 insertions, 12 deletions
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