bpo-37144: Convert path-like object to regular path#13817
bpo-37144: Convert path-like object to regular path#138170x29a wants to merge 8 commits intopython:mainfrom
Conversation
Lib/tarfile.py
Outdated
| comptype = fileobj.getcomptype() | ||
|
|
||
| self.name = name or "" | ||
| self.name = os.path.abspath(name) if name else "" |
There was a problem hiding this comment.
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.
@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
| 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.
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
|
|
||
| # 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: |
|
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
|
|
||
| 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.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| self._mode = fileobj.mode | ||
| self._extfileobj = True | ||
|
|
||
| name = os.fspath(name) if name else None |
There was a problem hiding this comment.
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.
@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 |
| 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.
These two arguments should be indented under tmpname.
There was a problem hiding this comment.
you need to keep the same indent.
| if hasattr(fileobj, "mode"): | ||
| self._mode = fileobj.mode | ||
| self._extfileobj = True | ||
|
|
There was a problem hiding this comment.
remove this line, we have to get a minimalist diff for the merge. Thank you
| 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.
you need to keep the same indent.
| os.umask(original_umask) | ||
|
|
||
| def test_open_by_path_object(self): | ||
| # Test for issue #37144: |
|
@0x29a, please address the code reviews. Thank you! |
Steps to reproduce the issue:
tarfile.open:Result will be:
https://bugs.python.org/issue37144