From f3b5e534dd9f856f9dde92270b3031a651f76b54 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Wobst?= Date: Thu, 7 Jul 2011 20:47:17 +0000 Subject: [PATCH] fix subprocess call on MS Windows (closefds not functional not also not required at all) git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@3180 069f4177-920e-0410-937b-c2a4a81bcd90 --- CHANGES | 3 +++ pyx/pycompat.py | 17 +++++++++++++++++ pyx/text.py | 24 ++++++------------------ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/CHANGES b/CHANGES index bf47a319..72eb03d6 100644 --- a/CHANGES +++ b/CHANGES @@ -104,6 +104,9 @@ TODO: - fix raise condition while generating bitmap for PDF inclusion - filelocator module: - fix text mode line ending issue for MS Windows + - text module: + - fix subprocess call on MS Windows (closefds not functional not also + not required at all) - added the mpost (MetaPost) module: - create smooth paths from a series of points (TODO: documentation) diff --git a/pyx/pycompat.py b/pyx/pycompat.py index 95432b7f..957bae8d 100644 --- a/pyx/pycompat.py +++ b/pyx/pycompat.py @@ -60,6 +60,23 @@ def popen(cmd, mode="r", bufsize=_marker): else: return os.popen(cmd, mode, bufsize) +def popen4(cmd, mode="t", bufsize=_marker): + try: + import subprocess + kwargs = {"stdin": subprocess.PIPE, + "stdout": subprocess.PIPE, + "stderr": subprocess.STDOUT} + if bufsize is not _marker: + kwargs["bufsize"] = bufsize + pipes = subprocess.Popen(cmd, shell=True, **kwargs) + return pipes.stdin, pipes.stdout + except ImportError: + import os + if bufsize is _marker: + return os.popen4(cmd, mode) + else: + return os.popen4(cmd, mode, bufsize) + try: any = any except NameError: diff --git a/pyx/text.py b/pyx/text.py index 516c475a..f3027ed7 100644 --- a/pyx/text.py +++ b/pyx/text.py @@ -22,17 +22,10 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import errno, glob, os, threading, Queue, re, tempfile, atexit, time, warnings -import config, unit, box, canvas, trafo, version, attr, style, filelocator +import config, unit, box, canvas, trafo, version, attr, style, filelocator, pycompat from pyx.dvi import dvifile import bbox as bboxmodule -try: - import subprocess -except ImportError: - have_subprocess = False -else: - have_subprocess = True - class PyXTeXWarning(UserWarning): pass warnings.filterwarnings('always', category=PyXTeXWarning) @@ -905,16 +898,11 @@ class texrunner: ipcflag = " --ipc" else: ipcflag = "" - if have_subprocess: - p = subprocess.Popen("%s%s %s" % (self.mode, ipcflag, self.texfilename), shell=True, bufsize=0, - stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) - self.texinput, self.texoutput = p.stdin, p.stdout - else: - try: - self.texinput, self.texoutput = os.popen4("%s%s %s" % (self.mode, ipcflag, self.texfilename), "t", 0) - except ValueError: - # XXX: workaround for MS Windows (bufsize = 0 makes trouble!?) - self.texinput, self.texoutput = os.popen4("%s%s %s" % (self.mode, ipcflag, self.texfilename), "t") + try: + self.texinput, self.texoutput = pycompat.popen4("%s%s %s" % (self.mode, ipcflag, self.texfilename), "t", 0) + except ValueError: + # workaround: bufsize = 0 is not supported on MS windows for os.open4 (Python 2.4 and below, i.e. where subprocess is not available) + self.texinput, self.texoutput = pycompat.popen4("%s%s %s" % (self.mode, ipcflag, self.texfilename), "t") atexit.register(_cleantmp, self) self.expectqueue = Queue.Queue(1) # allow for a single entry only -> keeps the next InputMarker to be wait for self.gotevent = threading.Event() # keeps the got inputmarker event -- 2.11.4.GIT