Skip to content

Pls consider using subprocess.PIPE instead of out_file and err_file #161

Open

Description

@anerokhi

Something like

--- /home/user/Desktop/pythontex2.py	2019-12-14 04:27:04.718269040 +0300
+++ /usr/share/texlive/texmf-dist/scripts/pythontex/pythontex2.py	2019-12-14 04:15:23.189530900 +0300
@@ -1510,8 +1510,8 @@
     basename = key_run.replace('#', '_')
     out_file_name = os.path.expanduser(os.path.normcase(os.path.join(outputdir, basename + '.out')))
     err_file_name = os.path.expanduser(os.path.normcase(os.path.join(outputdir, basename + '.err')))
-    out_file = open(out_file_name, 'w', encoding=encoding)
-    err_file = open(err_file_name, 'w', encoding=encoding)
+    out_file = subprocess.PIPE
+    err_file = subprocess.PIPE
     script = os.path.expanduser(os.path.normcase(os.path.join(outputdir, basename)))
     if os.path.isabs(script):
         script_full = script
@@ -1550,11 +1550,11 @@
             else:
                 raise
 
-        proc.wait()
+        out_data, err_data = proc.communicate()
         if proc.returncode != 0:
             break
-    out_file.close()
-    err_file.close()
+    with open(out_file_name, 'wb') as out_file: out_file.write(out_data)
+    with open(err_file_name, 'wb') as err_file: err_file.write(err_data)
 
     # Process saved stdout into file(s) that are included in the TeX document.
     #

Anyways, open(..., encoding=encoding) is meaningless, because the file-like object is then passed to subprocess.Popen which is only interested in .fileno() of this file-like object.

Thanks.

Activity

  1. gpoore commented on Dec 14, 2019

    @gpoore
    Owner

    Please clarify how this would be an improvement for you compared to the current system. What prompted this issue?

  2. anerokhi commented on Dec 14, 2019

    @anerokhi
    Author

    Maybe you've seen software which doesn't know how to output to stdout and can only output to files. Sometimes you can pass /dev/stdout and this even works. I would like to do this the middle of python code. But, this piece of software opens /dev/stdout in w mode and since /dev/stdout points to ###.out regular file, the file gets truncated and there is no longer =>PYTHONTEX:STDOUT#{0}#code# in the beginning, which confuses PythonTeX.

    The second reason is simply that open(..., encoding=encoding) doesn't do what you expect it to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions