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

Miss Islington (bot) webhook-mailer at python.org
Mon Sep 9 13:31:36 EDT 2019


https://github.com/python/cpython/commit/eadf6b8787e979920c4fb6845797c33d270d2729
commit: eadf6b8787e979920c4fb6845797c33d270d2729
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019年09月09日T10:31:31-07:00
summary:
bpo-35803: Document and test dir=PathLike for tempfile (GH-11644)
Co-Authored-By: Ammar Askar <ammar_askar at hotmail.com>
(cherry picked from commit 370138ba9c29595df773cc057d17ea26fb6f21bd)
Co-authored-by: Anthony Sottile <asottile at umich.edu>
files:
A Misc/NEWS.d/next/Documentation/2019-01-21-14-30-59.bpo-35803.yae6Lq.rst
M Doc/library/tempfile.rst
M Lib/test/test_tempfile.py
diff --git a/Doc/library/tempfile.rst b/Doc/library/tempfile.rst
index 0793e43df014..fff7a7a03eb8 100644
--- a/Doc/library/tempfile.rst
+++ b/Doc/library/tempfile.rst
@@ -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()
 
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index bd4db839331b..f995f6c9bfaf 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -3,6 +3,7 @@
 import errno
 import io
 import os
+import pathlib
 import signal
 import sys
 import re
@@ -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.
 
@@ -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")
@@ -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)
 
@@ -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)
 
@@ -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)
 
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.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /