-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
Lib/tarfile.py
Outdated
@@ -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.path.abspath(name) if name else "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.fspath
looks like a good solution to me. Please add a test in Lib/test/test_tarfile.py
that opens gzipname
with mode 'w|gz' flag to make sure there is no AttributeError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tirkarthi after opening gzipname
with mode w|gz
all remaining tests, that need to extract data from gzipname
will not pass, because opening rewrites file content.
I added test to StreamWriteTest
class, where I open for write tmpname
, because it's designed for writing and insensitive to content change. Also, since this test inside of StreamWriteTest
, opening by path-like filename will be performed for each mode (gz, lz, bz).
Lib/tarfile.py
Outdated
@@ -1468,7 +1468,7 @@ 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.path.abspath(name) if name else None | |||
self.name = os.fspath(name) if name else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these lines are not equal.
The correct code should be something like:
name = os.fspath(name)
self.name = os.path.abspath(name)
I've skipped the check for empty/None name.
Lib/tarfile.py
Outdated
@@ -1943,7 +1943,7 @@ def add(self, name, arcname=None, recursive=True, *, filter=None): | |||
arcname = name | |||
|
|||
# Skip if somebody tries to archive the archive... | |||
if self.name is not None and os.path.abspath(name) == self.name: | |||
if self.name is not None and os.fspath(name) == self.name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the same
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Lib/test/test_tarfile.py
Outdated
@@ -1421,6 +1421,11 @@ def test_file_mode(self): | |||
finally: | |||
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use a context manager that automatically calls close on the object.
with tarfile.open(pathlib.Path(tmpname), self.mode) as f:
pass
I have made the requested changes; please review again |
Thanks for making the requested changes! @asvetlov: please review the changes made to this pull request. |
Lib/test/test_tarfile.py
Outdated
@@ -1421,6 +1421,11 @@ def test_file_mode(self): | |||
finally: | |||
os.umask(original_umask) | |||
|
|||
def test_open_by_path_object(self): | |||
# Test for issue #37144: broken open for stream write by path-like object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too long line as well.
Please break it into the two-lines comment.
The comment is valuable, thanks
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Lib/tarfile.py
Outdated
@@ -1943,7 +1945,7 @@ def add(self, name, arcname=None, recursive=True, *, filter=None): | |||
arcname = name | |||
|
|||
# Skip if somebody tries to archive the archive... | |||
if self.name is not None and os.path.abspath(name) == self.name: | |||
if self.name is not None and os.path.abspath(os.fspath(name)) == self.name: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move os.fspath(name)
to the very beginning of the function, just after self._check(...)
.
I have made the requested changes; please review again |
Thanks for making the requested changes! @asvetlov: please review the changes made to this pull request. |
Lib/tarfile.py
Outdated
@@ -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 | |||
|
|||
name = os.fspath(name) if name else None |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Now looks much better! After briefly looking in |
@@ -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'}) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
@@ -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 | |||
|
There was a problem hiding this comment.
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
@@ -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'}) |
There was a problem hiding this comment.
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.
@@ -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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove these comments.
@0x29a, please address the code reviews. Thank you! |
Steps to reproduce the issue:
tarfile.open
:Result will be:
https://bugs.python.org/issue37144