Skip to content

Commit b4591ad

Browse files
bpo-35803: Document and test dir=PathLike for tempfile (GH-11644)
Co-Authored-By: Ammar Askar <[email protected]> (cherry picked from commit 370138b) Co-authored-by: Anthony Sottile <[email protected]>
1 parent b7bf632 commit b4591ad

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Doc/library/tempfile.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ The module defines the following user-callable items:
174174
*suffix* and *prefix* now accept and default to ``None`` to cause
175175
an appropriate default value to be used.
176176

177+
.. versionchanged:: 3.6
178+
The *dir* parameter now accepts a :term:`path-like object`.
179+
177180

178181
.. function:: mkdtemp(suffix=None, prefix=None, dir=None)
179182

@@ -195,6 +198,9 @@ The module defines the following user-callable items:
195198
*suffix* and *prefix* now accept and default to ``None`` to cause
196199
an appropriate default value to be used.
197200

201+
.. versionchanged:: 3.6
202+
The *dir* parameter now accepts a :term:`path-like object`.
203+
198204

199205
.. function:: gettempdir()
200206

Lib/test/test_tempfile.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import errno
44
import io
55
import os
6+
import pathlib
67
import signal
78
import sys
89
import re
@@ -61,6 +62,9 @@ def test_infer_return_type_multiples_and_none(self):
6162
with self.assertRaises(TypeError):
6263
tempfile._infer_return_type(b'', None, '')
6364

65+
def test_infer_return_type_pathlib(self):
66+
self.assertIs(str, tempfile._infer_return_type(pathlib.Path('/')))
67+
6468

6569
# Common functionality.
6670

@@ -84,8 +88,13 @@ def nameCheck(self, name, dir, pre, suf):
8488
nsuf = nbase[len(nbase)-len(suf):]
8589

8690
if dir is not None:
87-
self.assertIs(type(name), str if type(dir) is str else bytes,
88-
"unexpected return type")
91+
self.assertIs(
92+
type(name),
93+
str
94+
if type(dir) is str or isinstance(dir, os.PathLike) else
95+
bytes,
96+
"unexpected return type",
97+
)
8998
if pre is not None:
9099
self.assertIs(type(name), str if type(pre) is str else bytes,
91100
"unexpected return type")
@@ -430,6 +439,7 @@ def test_choose_directory(self):
430439
dir = tempfile.mkdtemp()
431440
try:
432441
self.do_create(dir=dir).write(b"blat")
442+
self.do_create(dir=pathlib.Path(dir)).write(b"blat")
433443
finally:
434444
os.rmdir(dir)
435445

@@ -666,6 +676,7 @@ def test_choose_directory(self):
666676
dir = tempfile.mkdtemp()
667677
try:
668678
self.do_create(dir=dir)
679+
self.do_create(dir=pathlib.Path(dir))
669680
finally:
670681
os.rmdir(dir)
671682

@@ -735,6 +746,7 @@ def test_choose_directory(self):
735746
dir = tempfile.mkdtemp()
736747
try:
737748
os.rmdir(self.do_create(dir=dir))
749+
os.rmdir(self.do_create(dir=pathlib.Path(dir)))
738750
finally:
739751
os.rmdir(dir)
740752

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Document and test that ``tempfile`` functions may accept a
2+
:term:`path-like object` for the ``dir`` argument. Patch by Anthony Sottile.

0 commit comments

Comments
 (0)