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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Perform os.path.abspath to result of os.fspath, use context manager
  • Loading branch information
0x29a committed Jun 6, 2019
commit 82882ddb700691df408f298f22cf27174e14de8a
7 changes: 5 additions & 2 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,9 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
if hasattr(fileobj, "mode"):
self._mode = fileobj.mode
self._extfileobj = True
self.name = os.fspath(name) if name else None

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

# Init attributes.
Expand Down Expand Up @@ -1938,12 +1940,13 @@ 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

# Skip if somebody tries to archive the archive...
if self.name is not None and os.fspath(name) == self.name:
if self.name is not None and os.path.abspath(name) == self.name:
self._dbg(2, "tarfile: Skipped %r" % name)
return

Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,9 +1422,10 @@ def test_file_mode(self):
os.umask(original_umask)

def test_open_by_path_object(self):
# Test for issue #37144: broken open for stream write by path-like object
tar = tarfile.open(pathlib.Path(tmpname), self.mode)
tar.close()
# 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