Skip to content

Commit 5d6a344

Browse files
committed
gh-87646: Added .path to tempfile
Added `.path` property to generate pathlib's `Path` objects for NamedTemporaryFile and TemporaryDirectory
1 parent 080a596 commit 5d6a344

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

Doc/library/tempfile.rst

+15
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ The module defines the following user-callable items:
142142
.. versionchanged:: 3.12
143143
Added *delete_on_close* parameter.
144144

145+
.. versionchanged:: 3.13
146+
Added *path* property.
147+
145148

146149
.. class:: SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)
147150

@@ -211,6 +214,9 @@ The module defines the following user-callable items:
211214
.. versionchanged:: 3.12
212215
Added the *delete* parameter.
213216

217+
.. versionchanged:: 3.13
218+
Added *path* property.
219+
214220

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

@@ -421,6 +427,15 @@ Here are some examples of typical usage of the :mod:`tempfile` module::
421427
>>>
422428
# directory and contents have been removed
423429

430+
# create a named temporary file, similar to TemporaryFile
431+
# but location and name is accessible
432+
>>> with tempfile.NamedTemporaryFile as fp:
433+
... print('file name: ', fp.name)
434+
... print('file path: ', fp.path)
435+
file name: '/tmp/tmpfiehg5my'
436+
file path: PosixPath('/tmp/tmpfiehg5my')
437+
438+
424439
.. _tempfile-mktemp-deprecated:
425440

426441
Deprecated functions and variables

Lib/tempfile.py

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import warnings as _warnings
4141
import io as _io
4242
import os as _os
43+
from pathlib import Path as _Path
4344
import shutil as _shutil
4445
import errno as _errno
4546
from random import Random as _Random
@@ -518,6 +519,10 @@ def __iter__(self):
518519
for line in self.file:
519520
yield line
520521

522+
@_functools.cached_property
523+
def path(self):
524+
return _Path(self.name)
525+
521526
def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
522527
newline=None, suffix=None, prefix=None,
523528
dir=None, delete=True, *, errors=None,
@@ -918,6 +923,10 @@ def __exit__(self, exc, value, tb):
918923
if self._delete:
919924
self.cleanup()
920925

926+
@_functools.cached_property
927+
def path(self):
928+
return _Path(self.name)
929+
921930
def cleanup(self):
922931
if self._finalizer.detach() or _os.path.exists(self.name):
923932
self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)

Lib/test/test_tempfile.py

+13
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,12 @@ def test_unexpected_error(self):
11471147
mock_open().close.assert_called()
11481148
self.assertEqual(os.listdir(dir), [])
11491149

1150+
def test_path_method(self):
1151+
d = self.do_create()
1152+
path = d.path
1153+
self.assertTrue(issubclass(type(path), pathlib.Path), "unexpected return type")
1154+
self.assertEqual(str(path), d.name, ".path .name mismatch")
1155+
11501156
# How to test the mode and bufsize parameters?
11511157

11521158
class TestSpooledTemporaryFile(BaseTestCase):
@@ -1853,5 +1859,12 @@ def test_delete_false(self):
18531859
self.assertTrue(os.path.exists(working_dir))
18541860
shutil.rmtree(working_dir)
18551861

1862+
def test_path_method(self):
1863+
d = self.do_create()
1864+
path = d.path
1865+
self.assertTrue(issubclass(type(path), pathlib.Path), "unexpected return type")
1866+
self.assertEqual(str(path), d.name, ".path .name mismatch")
1867+
1868+
18561869
if __name__ == "__main__":
18571870
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`tempfile.NamedTemporaryFile` and :class:`tempfile.TemporaryDirectory`
2+
includes a new ``.path`` property. This provides the temporary file name as a
3+
:class:`pathlib.Path` object

0 commit comments

Comments
 (0)