-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-87646: Make tempfile.NamedTemporaryFile and TemporaryDirectory path-like
#114765
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
02e26b6
55adff5
1a82b09
e5c0022
a90b0b4
d71cc23
11bc4b9
1c819c8
fc0ff04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1332,12 +1332,14 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True, | |
| "metadata_encoding is only supported for reading files") | ||
|
|
||
| # Check if we were passed a file-like object | ||
| if isinstance(file, os.PathLike): | ||
| file = os.fspath(file) | ||
| if isinstance(file, str): | ||
| if hasattr(file, 'read') or hasattr(file, 'write'): | ||
| self._filePassed = 1 | ||
| self.fp = file | ||
| self.filename = getattr(file, 'name', None) | ||
| else: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this have to to with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When given an argument that is both file-like and path-like, previously There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect some third-party code will fall into the same trap as |
||
| # No, it's a filename | ||
| self._filePassed = 0 | ||
| self.filename = file | ||
| self.filename = os.fspath(file) | ||
| modeDict = {'r' : 'rb', 'w': 'w+b', 'x': 'x+b', 'a' : 'r+b', | ||
| 'r+b': 'w+b', 'w+b': 'wb', 'x+b': 'xb'} | ||
| filemode = modeDict[mode] | ||
|
|
@@ -1350,10 +1352,6 @@ def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=True, | |
| continue | ||
| raise | ||
| break | ||
| else: | ||
| self._filePassed = 1 | ||
| self.fp = file | ||
| self.filename = getattr(file, 'name', None) | ||
| self._fileRefCnt = 1 | ||
| self._lock = threading.RLock() | ||
| self._seekable = True | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Return a :term:`path-like object` from :func:`tempfile.NamedTemporaryFile` | ||
| and :func:`~tempfile.TemporaryDirectory`. (Contributed by Barney Gale in | ||
| :gh:`87646`.) |
Uh oh!
There was an error while loading. Please reload this page.