texlive[66438] Master/texmf-dist: pythonimmediate (7mar23)

commits+karl at tug.org commits+karl at tug.org
Tue Mar 7 23:21:22 CET 2023


Revision: 66438
          http://tug.org/svn/texlive?view=revision&revision=66438
Author:   karl
Date:     2023-03-07 23:21:22 +0100 (Tue, 07 Mar 2023)
Log Message:
-----------
pythonimmediate (7mar23)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/latex/pythonimmediate/README
    trunk/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdf
    trunk/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex
    trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty

Added Paths:
-----------
    trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua

Modified: trunk/Master/texmf-dist/doc/latex/pythonimmediate/README
===================================================================
--- trunk/Master/texmf-dist/doc/latex/pythonimmediate/README	2023-03-07 22:21:01 UTC (rev 66437)
+++ trunk/Master/texmf-dist/doc/latex/pythonimmediate/README	2023-03-07 22:21:22 UTC (rev 66438)
@@ -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 @@
 
 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.

Modified: trunk/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex
===================================================================
--- trunk/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex	2023-03-07 22:21:01 UTC (rev 66437)
+++ trunk/Master/texmf-dist/doc/latex/pythonimmediate/pythonimmediate.tex	2023-03-07 22:21:22 UTC (rev 66438)
@@ -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 @@
 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 @@
 			\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 @@
 	\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}

Modified: trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty	2023-03-07 22:21:01 UTC (rev 66437)
+++ trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate.sty	2023-03-07 22:21:22 UTC (rev 66438)
@@ -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 at 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
 	}

Added: trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua
===================================================================
--- trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua	                        (rev 0)
+++ trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua	2023-03-07 22:21:22 UTC (rev 66438)
@@ -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


Property changes on: trunk/Master/texmf-dist/tex/latex/pythonimmediate/pythonimmediate_helper.lua
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property


More information about the tex-live-commits mailing list.