You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2017-04-29.08:21:08.187>labels= ['type-bug']
title='AttributeError in Popen.communicate()'updated_at=<Date2017-04-29.08:24:46.146>user='https://bugs.python.org/LukeCampagnola'
In my application, calling communicate() on a Popen instance is giving the following exception:
. . .
File "/usr/lib/python3.5/subprocess.py", line 1072, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1693, in _communicate
stdout = self._fileobj2output[self.stdout]
AttributeError: 'Popen' object has no attribute '_fileobj2output'
I have not been able to reproduce this in a simple example, but I imagine this could happen if Popen._communicate() raises an exception in the first 20 lines or so--this would cause _communication_started to be set True, even though _fileobj2output has not been initialized.
I suggest setting self._fileobj2output = None in Popen.__init__() and changing the relevant code in _communicate() from
Update: this appears to be the prior exception that causes _communication_started and _fileobj2ouput to be out of sync:
File "/usr/lib/python3.5/subprocess.py", line 1072, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1672, in _communicate
self.stdin.flush()
ValueError: flush of closed file
I can reproduce with the following code, in version 3.12, which involves closing stdin before first communicate() so that you get the exception in _communicate(), and then try to communicate again when communicate again when _communication_started=True but the fileobj attr was not set because of the exception.
I'm not sure that's a realistic example that needs a fix. If someone can demonstrate that it IS a realistic scenario, the fix would be easy, along the lines OP suggested.
File "C:\Users\Henry\Desktop\XYBot\pywxdll\pywxdll.py", line 54, in windows_start_wechat_and_inject
result = ''.join(result.communicate())
AttributeError: 'CompletedProcess' object has no attribute 'communicate'
AttributeError: 'CompletedProcess' object has no attribute 'communicate'
@HenryXiaoYang your issue looks different and doesn't match what your code snippets say. result would only be of CompletedProcess type if you had assigned it to a result = subprocess.run(...), not = subprocess.Popen(...) which would be the instance you can call communicate() on.
Activity
LukeCampagnola commentedon Apr 29, 2017
In my application, calling communicate() on a Popen instance is giving the following exception:
. . .
File "/usr/lib/python3.5/subprocess.py", line 1072, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1693, in _communicate
stdout = self._fileobj2output[self.stdout]
AttributeError: 'Popen' object has no attribute '_fileobj2output'
I have not been able to reproduce this in a simple example, but I imagine this could happen if Popen._communicate() raises an exception in the first 20 lines or so--this would cause _communication_started to be set True, even though _fileobj2output has not been initialized.
I suggest setting self._fileobj2output = None in Popen.__init__() and changing the relevant code in _communicate() from
to:
LukeCampagnola commentedon Apr 29, 2017
Update: this appears to be the prior exception that causes _communication_started and _fileobj2ouput to be out of sync:
File "/usr/lib/python3.5/subprocess.py", line 1072, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1672, in _communicate
self.stdin.flush()
ValueError: flush of closed file
akulakov commentedon Oct 10, 2022
I can reproduce with the following code, in version 3.12, which involves closing
stdinbefore first communicate() so that you get the exception in _communicate(), and then try to communicate again when communicate again when _communication_started=True but the fileobj attr was not set because of the exception.I'm not sure that's a realistic example that needs a fix. If someone can demonstrate that it IS a realistic scenario, the fix would be easy, along the lines OP suggested.
HenryXiaoYang commentedon Jul 7, 2024
Encountered this issue, any idea?
gpshead commentedon Nov 29, 2025
@HenryXiaoYang your issue looks different and doesn't match what your code snippets say. result would only be of
CompletedProcesstype if you had assigned it to aresult = subprocess.run(...), not= subprocess.Popen(...)which would be the instance you can call communicate() on.pythongh-70560: pythongh-74389: subprocess.Popen.communicate() now ig…
pythongh-70560: pythongh-74389: subprocess.Popen.communicate() now ig…
gh-74389: gh-70560: subprocess.Popen.communicate() now ignores stdin.…
pythongh-74389: pythongh-70560: subprocess.Popen.communicate() now ig…
pythongh-74389: pythongh-70560: subprocess.Popen.communicate() now ig…
[3.14] gh-74389: gh-70560: subprocess.Popen.communicate() now ignores…
[3.13] gh-74389: gh-70560: subprocess.Popen.communicate() now ignores…
Fix memoryview and closed stdin handling in _communicate_streams_posix
Factor out _flush_stdin() and _make_input_view() helpers
pythongh-74389: pythongh-70560: subprocess.Popen.communicate() now ig…