Raising exception on STDIN read

Ian Clark iclark at mail.ewu.edu
Wed Feb 27 12:06:36 EST 2008


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?
>
> Thanks,
> Michael

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].

	import sys 

	class ErrorFile(object):
		def _error(self, *args, **kwargs):
			raise AssertionError("Illegal Access")

		def _noop(self, *args, **kwargs):
			pass

		close = flush = seek = tell = _noop
		next = read = readline = readlines = xreadlines = tuncate = _error
		truncate = write = writelines = _error

	sys.stdin = ErrorFile()
	print raw_input("? ")

Ian

[1] http://docs.python.org/lib/bltin-file-objects.html




More information about the Python-list mailing list