Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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
5 changes: 4 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
fileobj = _StreamProxy(fileobj)
comptype = fileobj.getcomptype()

self.name = name or ""
self.name = os.fspath(name) if name else ""
self.mode = mode
self.comptype = comptype
self.fileobj = fileobj
Expand Down Expand Up @@ -1468,6 +1468,8 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
if hasattr(fileobj, "mode"):
self._mode = fileobj.mode
self._extfileobj = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line, we have to get a minimalist diff for the merge. Thank you

name = os.fspath(name) if name else None
Copy link
Copy Markdown
Contributor

@asvetlov asvetlov Jun 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this line before if not fileobj: the code calls os.path.exists() and bltn_open() with name.

Both functions acceptpathlib.Path object but let's be consistent in Path -> str conversion, please

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asvetlov
Oh, finally I got It. Updated.

self.name = os.path.abspath(name) if name else None
self.fileobj = fileobj

Expand Down Expand Up @@ -1938,6 +1940,7 @@ def add(self, name, arcname=None, recursive=True, *, filter=None):
excluded from the archive.
"""
self._check("awx")
name = os.fspath(name)

if arcname is None:
arcname = name
Expand Down
8 changes: 7 additions & 1 deletion Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ def write(self, data):

f = BadFile()
with self.assertRaises(exctype):
tar = tarfile.open(tmpname, self.mode, fileobj=f,
tarfile.open(tmpname, self.mode, fileobj=f,
format=tarfile.PAX_FORMAT,
pax_headers={'non': 'empty'})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two arguments should be indented under tmpname.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to keep the same indent.

self.assertFalse(f.closed)
Expand Down Expand Up @@ -1421,6 +1421,12 @@ def test_file_mode(self):
finally:
os.umask(original_umask)

def test_open_by_path_object(self):
# Test for issue #37144:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove these comments.

# broken open for stream write by path-like object
with tarfile.open(pathlib.Path(tmpname), self.mode):
pass

class GzipStreamWriteTest(GzipTest, StreamWriteTest):
pass

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Now `tarfile.open(path, 'w|gz')` works for path-like objects