Skip to content

Commit 370138b

Browse files
asottileammaraskar
authored andcommitted
bpo-35803: Document and test dir=PathLike for tempfile (pythonGH-11644)
Co-Authored-By: Ammar Askar <[email protected]>
1 parent 9488a52 commit 370138b

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
@@ -191,6 +191,9 @@ The module defines the following user-callable items:
191191
*suffix* and *prefix* now accept and default to ``None`` to cause
192192
an appropriate default value to be used.
193193

194+
.. versionchanged:: 3.6
195+
The *dir* parameter now accepts a :term:`path-like object`.
196+
194197

195198
.. function:: mkdtemp(suffix=None, prefix=None, dir=None)
196199

@@ -214,6 +217,9 @@ The module defines the following user-callable items:
214217
*suffix* and *prefix* now accept and default to ``None`` to cause
215218
an appropriate default value to be used.
216219

220+
.. versionchanged:: 3.6
221+
The *dir* parameter now accepts a :term:`path-like object`.
222+
217223

218224
.. function:: gettempdir()
219225

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
@@ -56,6 +57,9 @@ def test_infer_return_type_multiples_and_none(self):
5657
with self.assertRaises(TypeError):
5758
tempfile._infer_return_type(b'', None, '')
5859

60+
def test_infer_return_type_pathlib(self):
61+
self.assertIs(str, tempfile._infer_return_type(pathlib.Path('/')))
62+
5963

6064
# Common functionality.
6165

@@ -79,8 +83,13 @@ def nameCheck(self, name, dir, pre, suf):
7983
nsuf = nbase[len(nbase)-len(suf):]
8084

8185
if dir is not None:
82-
self.assertIs(type(name), str if type(dir) is str else bytes,
83-
"unexpected return type")
86+
self.assertIs(
87+
type(name),
88+
str
89+
if type(dir) is str or isinstance(dir, os.PathLike) else
90+
bytes,
91+
"unexpected return type",
92+
)
8493
if pre is not None:
8594
self.assertIs(type(name), str if type(pre) is str else bytes,
8695
"unexpected return type")
@@ -425,6 +434,7 @@ def test_choose_directory(self):
425434
dir = tempfile.mkdtemp()
426435
try:
427436
self.do_create(dir=dir).write(b"blat")
437+
self.do_create(dir=pathlib.Path(dir)).write(b"blat")
428438
finally:
429439
os.rmdir(dir)
430440

@@ -659,6 +669,7 @@ def test_choose_directory(self):
659669
dir = tempfile.mkdtemp()
660670
try:
661671
self.do_create(dir=dir)
672+
self.do_create(dir=pathlib.Path(dir))
662673
finally:
663674
os.rmdir(dir)
664675

@@ -728,6 +739,7 @@ def test_choose_directory(self):
728739
dir = tempfile.mkdtemp()
729740
try:
730741
os.rmdir(self.do_create(dir=dir))
742+
os.rmdir(self.do_create(dir=pathlib.Path(dir)))
731743
finally:
732744
os.rmdir(dir)
733745

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)