[New-bugs-announce] [issue45585] Gzipping subprocess output produces invalid .gz file

Michael Herrmann report at bugs.python.org
Sat Oct 23 02:13:57 EDT 2021


New submission from Michael Herrmann <mherrmann.at at gmail.com>:

Consider the following:

import gzip
import subprocess

with gzip.open('test.gz', 'wb') as f:
    subprocess.run(['echo', 'hi'], stdout=f)

with gzip.open('test.gz', 'rb') as f:
    print(f.read())

I'd expect "hi" to appear in my console. Instead, I'm getting "OSError: Not a gzipped file (b'hi')". I am attaching test.gz.

This appears for me on Debian 10 / Python 3.7 and Debian 11 / Python 3.9. I have not yet tested on other OSs and Python versions.

The reason why I expect the above to work is that the subprocess documentation states that the stdout parameter may be "an existing file object" and that on the other hand the documentation for gzip.open(...) states that it returns a file object.

Maybe this is related to #40885?

----------
files: test.gz
messages: 404853
nosy: mherrmann.at
priority: normal
severity: normal
status: open
title: Gzipping subprocess output produces invalid .gz file
type: behavior
versions: Python 3.7, Python 3.9
Added file: https://bugs.python.org/file50391/test.gz

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


More information about the New-bugs-announce mailing list