diff options
author | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-01-18 07:25:58 (GMT) |
---|---|---|
committer | Nadeem Vawda <nadeem.vawda@gmail.com> | 2012-01-18 07:25:58 (GMT) |
commit | 892b0b928df40da72b0d88c5e1a2c879eff543c4 (patch) | |
tree | 7cf5d742852255285657770cc20baaa80baf1db8 /Lib | |
parent | 031605ad999fa33e6da3eae3791c368826bd8311 (diff) | |
download | cpython-892b0b928df40da72b0d88c5e1a2c879eff543c4.zip cpython-892b0b928df40da72b0d88c5e1a2c879eff543c4.tar.gz cpython-892b0b928df40da72b0d88c5e1a2c879eff543c4.tar.bz2 |
Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/gzip.py | 6 | ||||
-rw-r--r-- | Lib/test/test_gzip.py | 8 |
2 files changed, 12 insertions, 2 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index ba2149e..4462187 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -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' diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 2b0ac36..5ae7467 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -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): |