Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects.
This commit is contained in:
parent
031605ad99
commit
892b0b928d
|
@ -156,8 +156,10 @@ class GzipFile(io.BufferedIOBase):
|
|||
if fileobj is None:
|
||||
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
|
||||
if filename is None:
|
||||
if hasattr(fileobj, 'name'): filename = fileobj.name
|
||||
else: filename = ''
|
||||
if hasattr(fileobj, 'name') and isinstance(fileobj.name, str):
|
||||
filename = fileobj.name
|
||||
else:
|
||||
filename = ''
|
||||
if mode is None:
|
||||
if hasattr(fileobj, 'mode'): mode = fileobj.mode
|
||||
else: mode = 'rb'
|
||||
|
|
|
@ -323,6 +323,14 @@ class TestGzip(unittest.TestCase):
|
|||
self.assertEqual(f.read(100), b'')
|
||||
self.assertEqual(nread, len(uncompressed))
|
||||
|
||||
def test_fileobj_from_fdopen(self):
|
||||
# Issue #13781: Opening a GzipFile for writing fails when using a
|
||||
# fileobj created with os.fdopen().
|
||||
fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
|
||||
with os.fdopen(fd, "wb") as f:
|
||||
with gzip.GzipFile(fileobj=f, mode="w") as g:
|
||||
pass
|
||||
|
||||
# Testing compress/decompress shortcut functions
|
||||
|
||||
def test_compress(self):
|
||||
|
|
Loading…
Reference in New Issue