[issue45585] Gzipping subprocess output produces invalid .gz file

2021-10-23 Thread Martin Panter


Martin Panter  added the comment:

The subprocess module only uses the file object to get a file handle by calling 
the "fileno" method. See Issue 19992 about documenting this. For Python to 
compress the output of the child process, you would need a pipe.

Gzip file objects provide the "fileno" method, but it just returns the 
underlying file descriptor. Data written to that file descriptor would normally 
already be compressed by Python and goes straight to the OS. There is also 
Issue 24358 opened about whether "fileno" should be implemented.

--
nosy: +martin.panter

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45585] Gzipping subprocess output produces invalid .gz file

2021-10-23 Thread Michael Herrmann


New submission from Michael Herrmann :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com