From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 9 Sep 2019 17:33:18 +0000 (-0700) Subject: bpo-35803: Document and test dir=PathLike for tempfile (GH-11644) X-Git-Tag: v3.7.5rc1~118 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b4591ad33a727873c0a07d084211295bf4f5b892;p=thirdparty%2FPython%2Fcpython.git bpo-35803: Document and test dir=PathLike for tempfile (GH-11644) Co-Authored-By: Ammar Askar (cherry picked from commit 370138ba9c29595df773cc057d17ea26fb6f21bd) Co-authored-by: Anthony Sottile --- diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst index 746adb1eee48..dd24a1c6f4ff 100644 --- a/Doc/library/tempfile.rst +++ b/Doc/library/tempfile.rst @@ -174,6 +174,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) @@ -195,6 +198,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() diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 710756bde64c..931312831616 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -3,6 +3,7 @@ import tempfile import errno import io import os +import pathlib import signal import sys import re @@ -61,6 +62,9 @@ class TestLowLevelInternals(unittest.TestCase): 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. @@ -84,8 +88,13 @@ class BaseTestCase(unittest.TestCase): 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") @@ -430,6 +439,7 @@ class TestMkstempInner(TestBadTempdir, BaseTestCase): 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) @@ -666,6 +676,7 @@ class TestMkstemp(BaseTestCase): dir = tempfile.mkdtemp() try: self.do_create(dir=dir) + self.do_create(dir=pathlib.Path(dir)) finally: os.rmdir(dir) @@ -735,6 +746,7 @@ class TestMkdtemp(TestBadTempdir, BaseTestCase): dir = tempfile.mkdtemp() try: os.rmdir(self.do_create(dir=dir)) + os.rmdir(self.do_create(dir=pathlib.Path(dir))) finally: os.rmdir(dir) diff --git a/Misc/NEWS.d/next/Documentation/2019-01-21-14-30-59.bpo-35803.yae6Lq.rst b/Misc/NEWS.d/next/Documentation/2019-01-21-14-30-59.bpo-35803.yae6Lq.rst new file mode 100644 index 000000000000..b8394560e548 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-01-21-14-30-59.bpo-35803.yae6Lq.rst @@ -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.