diff options
Diffstat (limited to 'Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty')
-rw-r--r-- | Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty | 56 |
1 files changed, 44 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 } |