From 2eea1709a03ca5a0333ea144ad92259491f3ce82 Mon Sep 17 00:00:00 2001 From: Kyungmin Lee Date: Tue, 14 Sep 2021 14:48:10 +0900 Subject: [PATCH 1/4] bpo-45192: Fix a bug that infers the type of an os.PathLike[bytes] object as str An object implementing the os.PathLike protocol can represent a file system path as a str or bytes object. Therefore, _infer_return_type function should infer os.PathLike[str] object as str type and os.PathLike[bytes] object as bytes type. --- Lib/tempfile.py | 4 ++++ Lib/test/test_tempfile.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 8570c3ba0627fa..531cbf32f1283f 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -88,6 +88,10 @@ def _infer_return_type(*args): for arg in args: if arg is None: continue + + if isinstance(arg, _os.PathLike): + arg = _os.fspath(arg) + if isinstance(arg, bytes): if return_type is str: raise TypeError("Can't mix bytes and non-bytes in " diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index f1d483733e2675..dac30dbceaa6d3 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -62,6 +62,25 @@ def test_infer_return_type_multiples_and_none(self): def test_infer_return_type_pathlib(self): self.assertIs(str, tempfile._infer_return_type(pathlib.Path('/'))) + def test_infer_return_type_pathlike(self): + class Path: + def __init__(self, path): + self.path = path + + def __fspath__(self): + return self.path + + self.assertIs(str, tempfile._infer_return_type(Path('/'))) + self.assertIs(bytes, tempfile._infer_return_type(Path(b'/'))) + self.assertIs(str, tempfile._infer_return_type('', Path(''))) + self.assertIs(bytes, tempfile._infer_return_type(b'', Path(b''))) + self.assertIs(bytes, tempfile._infer_return_type(None, Path(b''))) + self.assertIs(str, tempfile._infer_return_type(None, Path(''))) + + with self.assertRaises(TypeError): + tempfile._infer_return_type('', Path(b'')) + with self.assertRaises(TypeError): + tempfile._infer_return_type(b'', Path('')) # Common functionality. From 7cf154b15819cc4a346057b63ba143d8b74e3660 Mon Sep 17 00:00:00 2001 From: Kyungmin Lee Date: Tue, 14 Sep 2021 15:54:33 +0900 Subject: [PATCH 2/4] bpo-45192: Add Misc/NEWS.d --- .../next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst diff --git a/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst b/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst new file mode 100644 index 00000000000000..1c46aeb8fb1ab1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst @@ -0,0 +1,5 @@ +Fix the ``tempfile.__infer_return_type`` function so that the ``dir`` +argument of the :mod:`tempfile` functions accepts an object implementing the +``os.PathLike[bytes]`` protocol. + +Patch by Kyungmin Lee. From e05e01678f9f43da22926e72871c40a8357dad6a Mon Sep 17 00:00:00 2001 From: Kyungmin Lee Date: Fri, 17 Sep 2021 09:15:13 +0900 Subject: [PATCH 3/4] bpo-45192: Fix typo --- .../next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst b/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst index 1c46aeb8fb1ab1..b2fbcc8c0c4dd6 100644 --- a/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst +++ b/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst @@ -1,4 +1,4 @@ -Fix the ``tempfile.__infer_return_type`` function so that the ``dir`` +Fix the ``tempfile._infer_return_type`` function so that the ``dir`` argument of the :mod:`tempfile` functions accepts an object implementing the ``os.PathLike[bytes]`` protocol. From 427da5674e9c734763d12a9e7126f817d0f62acd Mon Sep 17 00:00:00 2001 From: Kyungmin Lee Date: Fri, 17 Sep 2021 09:46:30 +0900 Subject: [PATCH 4/4] bpo-45192: Update Misc/NEWS.d --- .../next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst b/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst index b2fbcc8c0c4dd6..7dd9795aaa1708 100644 --- a/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst +++ b/Misc/NEWS.d/next/Library/2021-09-14-15-52-47.bpo-45192.DjA-BI.rst @@ -1,5 +1,5 @@ Fix the ``tempfile._infer_return_type`` function so that the ``dir`` argument of the :mod:`tempfile` functions accepts an object implementing the -``os.PathLike[bytes]`` protocol. +``os.PathLike`` protocol. Patch by Kyungmin Lee.