python 2.7 and 3.3 subprocess module difference

python, python-2.7, python-3.3, python-3.x, subprocess

Solution

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: `bufsize` now defaults to `-1` to 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 to `0` which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.

Problem

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?

Original source