Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/tempfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ The module defines the following user-callable items:
*suffix* and *prefix* now accept and default to ``None`` to cause
an appropriate default value to be used.

.. versionchanged:: 3.6
The *dir* parameter now accepts a :term:`path-like object`.


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

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

.. versionchanged:: 3.6
The *dir* parameter now accepts a :term:`path-like object`.


.. function:: gettempdir()

Expand Down
16 changes: 14 additions & 2 deletions Lib/test/test_tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import errno
import io
import os
import pathlib
import signal
import sys
import re
Expand Down Expand Up @@ -56,6 +57,9 @@ def test_infer_return_type_multiples_and_none(self):
with self.assertRaises(TypeError):
tempfile._infer_return_type(b'', None, '')

def test_infer_return_type_pathlib(self):
self.assertIs(str, tempfile._infer_return_type(pathlib.Path('/')))


# Common functionality.

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

if dir is not None:
self.assertIs(type(name), str if type(dir) is str else bytes,
"unexpected return type")
self.assertIs(
type(name),
str
if type(dir) is str or isinstance(dir, os.PathLike) else
bytes,
"unexpected return type",
)
if pre is not None:
self.assertIs(type(name), str if type(pre) is str else bytes,
"unexpected return type")
Expand Down Expand Up @@ -425,6 +434,7 @@ def test_choose_directory(self):
dir = tempfile.mkdtemp()
try:
self.do_create(dir=dir).write(b"blat")
self.do_create(dir=pathlib.Path(dir)).write(b"blat")
finally:
os.rmdir(dir)

Expand Down Expand Up @@ -659,6 +669,7 @@ def test_choose_directory(self):
dir = tempfile.mkdtemp()
try:
self.do_create(dir=dir)
self.do_create(dir=pathlib.Path(dir))
finally:
os.rmdir(dir)

Expand Down Expand Up @@ -728,6 +739,7 @@ def test_choose_directory(self):
dir = tempfile.mkdtemp()
try:
os.rmdir(self.do_create(dir=dir))
os.rmdir(self.do_create(dir=pathlib.Path(dir)))
finally:
os.rmdir(dir)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Document and test that ``tempfile`` functions may accept a
:term:`path-like object` for the ``dir`` argument. Patch by Anthony Sottile.