[issue41406] BufferedReader causes Popen.communicate losing the remaining output.

Frost Ming report at bugs.python.org
Mon Jul 27 04:26:25 EDT 2020


New submission from Frost Ming <mianghong at gmail.com>:

The following snippet behaves differently between Windows and POSIX.

import subprocess
import time


p = subprocess.Popen("ls -l", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

print(p.stdout.read(1))   # read 1 byte
print(p.communicate())    # Returns empty output

It works fine on Windows and Python 2.x(communicate() returning the remaining output). So from the best guess it should be the expected behavior.

The reason behind this is that Popen.stdout is a BufferedReader. It stores all output in the buffer when calling read(). However, communicate() and the lower API _communicate() use a lower level method os.read() to get the output, which does not respect the underlying buffer. When an empty output is retrieved the file object is closed then.

First time to submit a bug report and pardon me if I am getting anything wrong.

----------
components: 2to3 (2.x to 3.x conversion tool), IO, Library (Lib)
messages: 374366
nosy: Frost Ming, brett.cannon, vstinner
priority: normal
severity: normal
status: open
title: BufferedReader causes Popen.communicate losing the remaining output.
versions: Python 3.10, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41406>
_______________________________________


More information about the Python-bugs-list mailing list