[issue45237] Python subprocess not honoring append mode for stdout on Windows
wolfgang kuehn
report at bugs.python.org
Fri Sep 17 17:11:43 EDT 2021
New submission from wolfgang kuehn <wolfgang-kuehn at decatur.de>:
On Windows, if you pass an existing file object in append mode to a subprocess, the subprocess does **not** really append to the file:
1. A file object with `Hello World` content is passed to the subprocess
2. The content is erased
3. The subprocess writes to the file
4. The expected output does not contain `Hello World`
Demo:
import subprocess, time, pathlib, sys
print(f'Caller {sys.platform=} {sys.version=}')
pathlib.Path('sub.py').write_text("""import sys, time
time.sleep(1)
print(f'Callee {sys.stdout.buffer.mode=}')""")
file = pathlib.Path('dummy.txt')
file.write_text('Hello World')
popen = subprocess.Popen([sys.executable, 'sub.py'], stdout=file.open(mode='a'))
file.write_text('')
time.sleep(2)
print(file.read_text())
Expected output on Linux
Caller sys.platform='linux' sys.version='3.8.6'
Callee sys.stdout.buffer.mode='wb'
Unexpected bad output on Windows
Caller sys.platform='win32' sys.version='3.8.6'
NULNULNULNULNULNULNULNULNULNULNULCallee sys.stdout.buffer.mode='wb'
Note that the expected output is given on Windows if the file is opened in the subprocess via `sys.stdout = open('dummy.txt', 'a')`. So it is definitely a subprocess thing.
----------
components: IO
messages: 402096
nosy: wolfgang-kuehn
priority: normal
severity: normal
status: open
title: Python subprocess not honoring append mode for stdout on Windows
type: behavior
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45237>
_______________________________________
More information about the Python-bugs-list
mailing list