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
Next Next commit
GH-87646: Make tempfile.NamedTemporaryFile and TemporaryDirectory
… path-like.

Add `__fspath__()` methods to `tempfile.NamedTemporaryFile` and
`TemporaryDirectory`.
  • Loading branch information
barneygale committed Jan 30, 2024
commit 02e26b6c7ac4da67e6abe2777af7c28bfc9e5142
6 changes: 6 additions & 0 deletions Doc/library/tempfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ The module defines the following user-callable items:
.. versionchanged:: 3.12
Added *delete_on_close* parameter.

.. versionchanged:: 3.13
Returns a :term:`path-like object`.


.. 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 @@ -217,6 +220,9 @@ The module defines the following user-callable items:
.. versionchanged:: 3.12
Added the *delete* parameter.

.. versionchanged:: 3.13
Returns a :term:`path-like object`.


.. function:: mkstemp(suffix=None, prefix=None, dir=None, text=False)

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ sys
This function is not guaranteed to exist in all implementations of Python.
(Contributed by Serhiy Storchaka in :gh:`78573`.)

tempfile
--------

* :func:`tempfile.NamedTemporaryFile` and :func:`~tempfile.TemporaryDirectory`
now return a :term:`path-like object`.
(Contributed by Barney Gale in :gh:`87646`.)

tkinter
-------

Expand Down
12 changes: 12 additions & 0 deletions Lib/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ def func_wrapper(*args, **kwargs):
setattr(self, name, a)
return a

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

# The underlying __enter__ method returns the wrong object
# (self.file) so override it to return the wrapper
def __enter__(self):
Expand Down Expand Up @@ -938,6 +944,12 @@ def _cleanup(cls, name, warn_message, ignore_errors=False, delete=True):
def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)

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

def __enter__(self):
return self.name

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,14 @@ def do_create(self, dir=None, pre="", suf="", delete=True):
delete=delete)

self.nameCheck(file.name, dir, pre, suf)
self.nameCheck(os.fspath(file), dir, pre, suf)
return file

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

def test_basic(self):
# NamedTemporaryFile can create files
Expand Down Expand Up @@ -1580,9 +1586,16 @@ def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1,
dir=dir, prefix=pre, suffix=suf,
ignore_cleanup_errors=ignore_cleanup_errors)
self.nameCheck(tmp.name, dir, pre, suf)
self.nameCheck(os.fspath(tmp), dir, pre, suf)
self.do_create2(tmp.name, recurse, dirs, files)
return tmp

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

def do_create2(self, path, recurse=1, dirs=1, files=1):
# Create subdirectories and some files
if recurse:
Expand Down
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`.)
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