I have the following code
from __future__ import print_function
import subprocess
p = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.kill()
p.wait()
print(p.stdin.write(b'some'))
and I ran it with python 2.7 and python 3.3. With python 2.7 an exception is thrown when I try to write to stdin. With python 3.3 this code prints 4. What is the difference between python 2.7 subprocess module and python 3.3?
The difference is in the default value for bufsize. It is 0 (unbuffered) in Python 2.7 therefore write causes EPIPE error. Pipes are fully buffered in Python 3.2+ i.e., until you flush the buffer, no error is detected. From subprocess' docs:
Changed in version 3.3.1:
bufsizenow defaults to-1to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to0which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With