bpo-35803: Document and test dir=PathLike for tempfile (GH-11644)
authorAnthony Sottile <asottile@umich.edu>
Mon, 9 Sep 2019 15:54:34 +0000 (08:54 -0700)
committerZachary Ware <zachary.ware@gmail.com>
Mon, 9 Sep 2019 15:54:34 +0000 (10:54 -0500)
Co-Authored-By: Ammar Askar <ammar_askar@hotmail.com>
Doc/library/tempfile.rst
Lib/test/test_tempfile.py
Misc/NEWS.d/next/Documentation/2019-01-21-14-30-59.bpo-35803.yae6Lq.rst [new file with mode: 0644]

index 0793e43df0145c13e970725ef3249d23c1f76a7c..fff7a7a03eb81dde4bdf0d803958880a90a9f0d7 100644 (file)
@@ -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)
 
@@ -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()
 
index bd4db839331b49d3052cb072fd0e3f1c3ecd3fe8..f995f6c9bfaf0062a5b8187a603a20277cf0dd5f 100644 (file)
@@ -3,6 +3,7 @@ import tempfile
 import errno
 import io
 import os
+import pathlib
 import signal
 import sys
 import re
@@ -56,6 +57,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.
 
@@ -79,8 +83,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")
@@ -425,6 +434,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)
 
@@ -659,6 +669,7 @@ class TestMkstemp(BaseTestCase):
         dir = tempfile.mkdtemp()
         try:
             self.do_create(dir=dir)
+            self.do_create(dir=pathlib.Path(dir))
         finally:
             os.rmdir(dir)
 
@@ -728,6 +739,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 (file)
index 0000000..b839456
--- /dev/null
@@ -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.