Skip to content

bpo-37144: Convert path-like object to regular path #13817

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 8 commits 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
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 @@ -1454,6 +1454,7 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
self.mode = mode
self._mode = modes[mode]

name = os.fspath(name) if name else None
if not fileobj:
if self.mode == "a" and not os.path.exists(name):
# Create nonexistent files in append mode.
Expand All @@ -1468,6 +1469,7 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None,
if hasattr(fileobj, "mode"):
self._mode = fileobj.mode
self._extfileobj = True

Copy link
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

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
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
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
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