473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Raising exception on STDIN read

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?

Thanks,
Michael
Feb 27 '08 #1
6 1691
En Wed, 27 Feb 2008 15:06:36 -0200, Ian Clark <ic****@mail.ewu.edu>
escribi�:
On 2008-02-27, Michael Goerz <ne**************@8439.e4ward.comwrote:
>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?
How about with a file-like object? I haven't tested this with subprocess
so you might want to read the manual on files if it doesn't work[1].
Won't work for an external process, as pdflatex (and the OS) knows nothing
about Python objects. The arguments to subprocess.Popen must be actual
files having real OS file descriptors.

Try with stdin=open("/dev/full") or stdin=open("/dev/null")

--
Gabriel Genellina

Feb 27 '08 #2
Gabriel Genellina wrote, on 02/27/2008 03:26 PM:
En Wed, 27 Feb 2008 15:06:36 -0200, Ian Clark <ic****@mail.ewu.edu>
escribi�:
>On 2008-02-27, Michael Goerz <ne**************@8439.e4ward.comwrote:
>>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?
>How about with a file-like object? I haven't tested this with subprocess
so you might want to read the manual on files if it doesn't work[1].

Won't work for an external process, as pdflatex (and the OS) knows
nothing about Python objects. The arguments to subprocess.Popen must be
actual files having real OS file descriptors.

Try with stdin=open("/dev/full") or stdin=open("/dev/null")
using /dev/null works in my specific case (in my posted minimal example
I still have to press Enter, but in the real program it causes pdflatex
to fail, like I want it to). However, the solution is limited do Linux,
as on Windows there's no /dev/null. Is there a platform independent
solution?
Michael
Feb 27 '08 #3
On 2008-02-27, Michael Goerz <ne**************@8439.e4ward.comwrote:
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.

--
Grant Edwards grante Yow! Remember, in 2039,
at MOUSSE & PASTA will
visi.com be available ONLY by
prescription!!
Feb 27 '08 #4
Grant Edwards wrote, on 02/27/2008 04:34 PM:
On 2008-02-27, Michael Goerz <ne**************@8439.e4ward.comwrote:
>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)
Yeah, I'm doing that by default. The problem in my actual program is
that the user can change the latex command and the compiler options, so
I can't guarantee that the latex compiler is being run in batchmode or
nonstopmode (The user might screw up). So, ideally, I want to enforce
that the subprocess is non-interactive by trowing an exception if it
does ask for input.

I have to use subprocess instead of os.system because I want to filter
the output (colorize it, parse for warnings, etc.)

Thanks,
Michael
Feb 27 '08 #5
En Wed, 27 Feb 2008 18:59:59 -0200, Michael Goerz
<ne**************@8439.e4ward.comescribi�:
>Try with stdin=open("/dev/full") or stdin=open("/dev/null")
using /dev/null works in my specific case (in my posted minimal example
I still have to press Enter, but in the real program it causes pdflatex
to fail, like I want it to). However, the solution is limited do Linux,
as on Windows there's no /dev/null. Is there a platform independent
solution?
It's spelled "NUL" on Windows; os.devnull returns the right thing
depending on the platform.

--
Gabriel Genellina

Feb 27 '08 #6
On 2008-02-28, Gabriel Genellina <ga*******@yahoo.com.arwrote:
I hope it's more clear now. Python objects are only meaningful *inside* a
Python program, but an *external* program can't use them directly.
Yes, sorry. This is what I was getting hung up on. My thinking was that
subprocess was orchestrating it such that any reads would be funneled
back into the Python object. Granted, that was my gut reaction to it.
Thinking (and playing) with it a bit more I now realize that that makes
no sense. Thank you for reminding me about the seperation of OS and
Python files.

Ian

Feb 29 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
4
by: David Hirschfield | last post by:
I know this should be obvious, but how does one raise a specific type of OSError? When I attempt to perform a file operation on a non-existent file, I get an OSError: , but what if I want to raise...
0
by: Apu Nahasapeemapetilon | last post by:
Suggestions on cross-posting this issue would be appreciated. I've got a C# .NET DLL (CLR v1.1) that raises events into its container. When the container is a .NET application, raising the...
3
by: Thomas Dybdahl Ahle | last post by:
Hi, I have a function, which looks like the following: connecting = False def func (): global connecting connecting = True try: # Do lot of network stuff except Exception, e: connecting =...
28
by: Christoph Zwerschke | last post by:
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every...
4
by: Silfheed | last post by:
Heyas So this probably highlights my lack of understanding of how naming works in python, but I'm currently using FailUnlessRaises in a unit test and raising exceptions with a string exception. ...
1
by: Michael Hines | last post by:
Hello, I have a class factory that supports single inheritance but it is an error if the base appears twice. e.g class Foo(hclass(h.Vector), hclass(h.List)): should raise an exception since...
7
by: Brendon Costa | last post by:
Hi all, I have a small python project i am working on. Basically i always have two threads. A "Read" thread that sits in a loop reading a line at a time from some input (Usually stdin) and then...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.