This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: subprocess __exit__ attribute missing
Type: Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: LambertDW, eric.araujo
Priority: normal Keywords:

Created on 2011-10-17 15:03 by LambertDW, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg145707 - (view) Author: David W. Lambert (LambertDW) Date: 2011-10-17 15:03
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 <module>
    print(p.stdout.readline())
KeyboardInterrupt
$
$ python p.py
Traceback (most recent call last):
  File "p.py", line 3, in <module>
    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.
>>>
msg145708 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-10-17 15:06
Context management support was added in 3.2.  It is not supported in 2.7.
msg145721 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-10-17 16:03
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.
History
Date User Action Args
2022-04-11 14:57:22adminsetgithub: 57411
2011-10-17 16:03:30eric.araujosetstatus: open -> closed
resolution: not a bug
messages: + msg145721

stage: resolved
2011-10-17 15:06:17eric.araujosetnosy: + eric.araujo
messages: + msg145708
2011-10-17 15:03:16LambertDWcreate