Issue 13202: subprocess exit attribute missing (original) (raw)
There are a number of issues with subprocess and exit , 12494 status fixed among them.
Program (which doesn't work as I had hoped, but that's not the issue):
'file p.py'
import subprocess as S
with S.Popen(('cat','-n',),shell=False,stdin=S.PIPE,stdout=S.PIPE,bufsize=1,universal_newlines=True) as p: p.stdin.write('these\n') print(p.stdout.readline())
Use:
$ python3 p.py Traceback (most recent call last): File "p.py", line 5, in print(p.stdout.readline()) KeyboardInterrupt $ $ python p.py Traceback (most recent call last): File "p.py", line 3, in with S.Popen(('cat','-n',),shell=False,stdin=S.PIPE,stdout=S.PIPE,bufsize=1,universal_newlines=True) as p: AttributeError: exit $ $ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
To clarify: Python 2.5 and higher support the with statement syntax, but not all classes that could benefit from it have the enter and exit methods. So you don’t get a SyntaxError in Python 2.7 when you write “with Popen(...)”, but you get an AttributeError because Popen in 2.7 does not do its part to support context management. The built-in open function, on the contrary, supports with in 2.5 and higher.