Commit 370138ba authored by Anthony Sottile's avatar Anthony Sottile Committed by Zachary Ware

bpo-35803: Document and test dir=PathLike for tempfile (GH-11644)

Co-Authored-By: default avatarAmmar Askar <ammar_askar@hotmail.com>
parent 9488a528
...@@ -191,6 +191,9 @@ The module defines the following user-callable items: ...@@ -191,6 +191,9 @@ The module defines the following user-callable items:
*suffix* and *prefix* now accept and default to ``None`` to cause *suffix* and *prefix* now accept and default to ``None`` to cause
an appropriate default value to be used. 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) .. function:: mkdtemp(suffix=None, prefix=None, dir=None)
...@@ -214,6 +217,9 @@ The module defines the following user-callable items: ...@@ -214,6 +217,9 @@ The module defines the following user-callable items:
*suffix* and *prefix* now accept and default to ``None`` to cause *suffix* and *prefix* now accept and default to ``None`` to cause
an appropriate default value to be used. an appropriate default value to be used.
.. versionchanged:: 3.6
The *dir* parameter now accepts a :term:`path-like object`.
.. function:: gettempdir() .. function:: gettempdir()
......
...@@ -3,6 +3,7 @@ import tempfile ...@@ -3,6 +3,7 @@ import tempfile
import errno import errno
import io import io
import os import os
import pathlib
import signal import signal
import sys import sys
import re import re
...@@ -56,6 +57,9 @@ class TestLowLevelInternals(unittest.TestCase): ...@@ -56,6 +57,9 @@ class TestLowLevelInternals(unittest.TestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
tempfile._infer_return_type(b'', None, '') tempfile._infer_return_type(b'', None, '')
def test_infer_return_type_pathlib(self):
self.assertIs(str, tempfile._infer_return_type(pathlib.Path('/')))
# Common functionality. # Common functionality.
...@@ -79,8 +83,13 @@ class BaseTestCase(unittest.TestCase): ...@@ -79,8 +83,13 @@ class BaseTestCase(unittest.TestCase):
nsuf = nbase[len(nbase)-len(suf):] nsuf = nbase[len(nbase)-len(suf):]
if dir is not None: if dir is not None:
self.assertIs(type(name), str if type(dir) is str else bytes, self.assertIs(
"unexpected return type") type(name),
str
if type(dir) is str or isinstance(dir, os.PathLike) else
bytes,
"unexpected return type",
)
if pre is not None: if pre is not None:
self.assertIs(type(name), str if type(pre) is str else bytes, self.assertIs(type(name), str if type(pre) is str else bytes,
"unexpected return type") "unexpected return type")
...@@ -425,6 +434,7 @@ class TestMkstempInner(TestBadTempdir, BaseTestCase): ...@@ -425,6 +434,7 @@ class TestMkstempInner(TestBadTempdir, BaseTestCase):
dir = tempfile.mkdtemp() dir = tempfile.mkdtemp()
try: try:
self.do_create(dir=dir).write(b"blat") self.do_create(dir=dir).write(b"blat")
self.do_create(dir=pathlib.Path(dir)).write(b"blat")
finally: finally:
os.rmdir(dir) os.rmdir(dir)
...@@ -659,6 +669,7 @@ class TestMkstemp(BaseTestCase): ...@@ -659,6 +669,7 @@ class TestMkstemp(BaseTestCase):
dir = tempfile.mkdtemp() dir = tempfile.mkdtemp()
try: try:
self.do_create(dir=dir) self.do_create(dir=dir)
self.do_create(dir=pathlib.Path(dir))
finally: finally:
os.rmdir(dir) os.rmdir(dir)
...@@ -728,6 +739,7 @@ class TestMkdtemp(TestBadTempdir, BaseTestCase): ...@@ -728,6 +739,7 @@ class TestMkdtemp(TestBadTempdir, BaseTestCase):
dir = tempfile.mkdtemp() dir = tempfile.mkdtemp()
try: try:
os.rmdir(self.do_create(dir=dir)) os.rmdir(self.do_create(dir=dir))
os.rmdir(self.do_create(dir=pathlib.Path(dir)))
finally: finally:
os.rmdir(dir) os.rmdir(dir)
......
Document and test that ``tempfile`` functions may accept a
:term:`path-like object` for the ``dir`` argument. Patch by Anthony Sottile.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment