Skip to content

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Apply suggestions from code review
Co-authored-by: Brett Cannon <brett@python.org>
  • Loading branch information
barneygale and brettcannon authored Feb 21, 2024
commit 1a82b097c0b9cb9d7f4350df3dcdd0ab1a79d6c7
4 changes: 2 additions & 2 deletions Doc/library/tempfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ The module defines the following user-callable items:
Added *delete_on_close* parameter.

.. versionchanged:: 3.13
Returns a :term:`path-like object`.
Added support for the :term:`path-like object` protocol.


.. class:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
Expand Down Expand Up @@ -221,7 +221,7 @@ The module defines the following user-callable items:
Added the *delete* parameter.

.. versionchanged:: 3.13
Returns a :term:`path-like object`.
Added support for the :term:`path-like object` protocol.


.. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False)
Expand Down
8 changes: 2 additions & 6 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,7 @@ def func_wrapper(*args, **kwargs):
return a

def __fspath__(self):
"""
Return the filesystem path of this temporary file.
"""
"""Return the filesystem path of the temporary file."""
return self.name

# The underlying __enter__ method returns the wrong object
Expand Down Expand Up @@ -945,9 +943,7 @@ def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)

def __fspath__(self):
"""
Return the filesystem path of this temporary directory.
"""
"""Return the filesystem path of the temporary directory."""
return self.name

def __enter__(self):
Expand Down
8 changes: 2 additions & 6 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,7 @@ def do_create(self, dir=None, pre="", suf="", delete=True):

def test_pathlike(self):
tmp = self.do_create()
self.assertTrue(isinstance(tmp, os.PathLike))
cls = type(tmp)
self.assertTrue(issubclass(cls, os.PathLike))
self.assertIsInstance(tmp, os.PathLike)

def test_basic(self):
# NamedTemporaryFile can create files
Expand Down Expand Up @@ -1592,9 +1590,7 @@ def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1,

def test_pathlike(self):
tmp = self.do_create()
self.assertTrue(isinstance(tmp, os.PathLike))
cls = type(tmp)
self.assertTrue(issubclass(cls, os.PathLike))
self.assertIsInstance(tmp, os.PathLike)

def do_create2(self, path, recurse=1, dirs=1, files=1):
# Create subdirectories and some files
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy