Raising exception on STDIN read

Grant Edwards grante at visi.com
Wed Feb 27 16:41:24 EST 2008


On 2008-02-27, Grant Edwards <grante at visi.com> wrote:
> On 2008-02-27, Michael Goerz <newsgroup898sfie at 8439.e4ward.com> wrote:
>> Hi,
>>
>> I would like to raise an exception any time a subprocess tries to read 
>> from STDIN:
>>
>> latexprocess = subprocess.Popen( \
>>          'pdflatex' + " " \
>>           + 'test' + " 2>&1", \
>>          shell=True, \
>>          cwd=os.getcwd(), \
>>          env=os.environ, \
>>          stdin=StdinCatcher() # any ideas here?
>>      )
>>
>> An exception should be raised whenever the pdflatex process
>> reads from STDIN... and I have no idea how to do it. Any suggestions?
>
> If I were you, I'd just run LaTeX in batch mode.  That's what I
> always used to do when automating the running of LaTeX using a
> shell script or makefile.  IIRC, you do something like this:
>
>   ret = os.system("latex \\batchmode\\input %s" % filename)
>
> The return status will be zero for success and non-zero if
> there were any errors.

Ooops.  Just did a quick test, and you need to double up the
backslashes one more time when using os.system().  I think
there might also be a way to supress the chatter coming out of
TeX, but if you're using subprocess, you can just attach stdout
and stderr to a bitbucket. Here's a demo where label1.tex has
no errors and label2.tex has an error:

________________________testit.py____________________________________
import os
ret1 = os.system('latex \\\\batchmode\\\\input %s' % 'label1.tex')
print ret1
ret2 = os.system('latex \\\\batchmode\\\\input %s' % 'label2.tex')
print ret2
_____________________________________________________________________

$ python testit.py

This is pdfeTeX, Version 3.141592-1.30.5-2.2 (Web2C 7.5.5)
entering extended mode
LaTeX2e <2003/12/01>
Babel <v3.8d> and hyphenation patterns for american, french, german, ngerman, b
ahasa, basque, bulgarian, catalan, croatian, czech, danish, dutch, esperanto, e
stonian, finnish, greek, icelandic, irish, italian, latin, magyar, norsk, polis
h, portuges, romanian, russian, serbian, slovak, slovene, spanish, swedish, tur
kish, ukrainian, nohyphenation, loaded.

0
This is pdfeTeX, Version 3.141592-1.30.5-2.2 (Web2C 7.5.5)
entering extended mode
LaTeX2e <2003/12/01>
Babel <v3.8d> and hyphenation patterns for american, french, german, ngerman, b
ahasa, basque, bulgarian, catalan, croatian, czech, danish, dutch, esperanto, e
stonian, finnish, greek, icelandic, irish, italian, latin, magyar, norsk, polis
h, portuges, romanian, russian, serbian, slovak, slovene, spanish, swedish, tur
kish, ukrainian, nohyphenation, loaded.

256


-- 
Grant Edwards                   grante             Yow! Somewhere in DOWNTOWN
                                  at               BURBANK a prostitute is
                               visi.com            OVERCOOKING a LAMB CHOP!!



More information about the Python-list mailing list