Skip to content

bpo-39121: write gzip header OS information #17682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

READ, WRITE = 1, 2

# OS field values as specified in RFC 1952
_OS_CODES = {
'unix': b'\x03',
'ntfs': b'\x0b',
'unknown': b'\xff'
}
_OS_UNIX = ('linux', 'freebsd', 'netbsd', 'openbsd', 'darwin', 'sunos', 'aix')
_OS_NTFS = ('win32')

_COMPRESS_LEVEL_FAST = 1
_COMPRESS_LEVEL_TRADEOFF = 6
_COMPRESS_LEVEL_BEST = 9
Expand Down Expand Up @@ -264,7 +273,12 @@ def _write_gzip_header(self, compresslevel):
else:
xfl = b'\000'
self.fileobj.write(xfl)
self.fileobj.write(b'\377')
if sys.platform.startswith(_OS_UNIX):
self.fileobj.write(_OS_CODES['unix'])
elif sys.platform.startswith(_OS_NTFS):
self.fileobj.write(_OS_CODES['ntfs'])
else:
self.fileobj.write(_OS_CODES['unknown'])
if fname:
self.fileobj.write(fname + b'\000')

Expand Down
9 changes: 7 additions & 2 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ def test_mtime(self):

def test_metadata(self):
mtime = 123456789

if sys.platform.startswith(gzip._OS_UNIX):
os_code = gzip._OS_CODES['unix']
elif sys.platform.startswith(gzip._OS_NTFS):
os_code = gzip._OS_CODES['ntfs']
else:
os_code = gzip._OS_CODES['unknown']
with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite:
fWrite.write(data1)

Expand All @@ -338,7 +343,7 @@ def test_metadata(self):
self.assertEqual(xflByte, b'\x02') # maximum compression

osByte = fRead.read(1)
self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
self.assertEqual(osByte, os_code) # gzip OS code

# Since the FNAME flag is set, the zero-terminated filename follows.
# RFC 1952 specifies that this is the name of the input file, if any.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ Grzegorz Makarewicz
David Malcolm
Greg Malcolm
William Mallard
Robert Mandic
Ken Manheimer
Vladimir Marangozov
Colin Marc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Writing gzip files sets 10th byte of the gzip header (the OS information)
based on :attr:`sys.platform` Patch by Robert Mandic.