From 9dc8574ee807c5c3a88d07e2fdb295d1453139ef Mon Sep 17 00:00:00 2001 From: chrispe Date: Sun, 28 Feb 2021 11:04:03 +0200 Subject: [PATCH 1/9] Add usage of tempfile --- pandas/tests/io/test_common.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index db742fb69dd10..6ed5cf328e5f1 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -9,7 +9,9 @@ import mmap import os from pathlib import Path +import tempfile +import ntpath import pytest from pandas.compat import is_platform_windows @@ -119,10 +121,17 @@ def test_infer_compression_from_path(self, extension, expected, path_type): @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path]) def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest - filename = path_type("~/sometest") - with icom.get_handle(filename, "w") as handles: - assert os.path.isabs(handles.handle.name) - assert os.path.expanduser(filename) == handles.handle.name + try: + tmp = tempfile.NamedTemporaryFile( + dir=os.path.expanduser("~/"), delete=False + ) + filename = "~/" + ntpath.basename(tmp.name) + tmp.close() + with icom.get_handle(filename, "w") as handles: + assert os.path.isabs(handles.handle.name) + assert os.path.expanduser(filename) == handles.handle.name + finally: + os.unlink(tmp.name) def test_get_handle_with_buffer(self): input_buffer = StringIO() From 3b0ba53d65af3c00d4c8592d39b372e5d83852ce Mon Sep 17 00:00:00 2001 From: chrispe Date: Sun, 28 Feb 2021 11:28:28 +0200 Subject: [PATCH 2/9] Use 'with' for tmp and use read mode for handle --- pandas/tests/io/test_common.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 6ed5cf328e5f1..ad14c9b5020fb 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -7,11 +7,11 @@ StringIO, ) import mmap +import ntpath import os from pathlib import Path import tempfile -import ntpath import pytest from pandas.compat import is_platform_windows @@ -121,17 +121,11 @@ def test_infer_compression_from_path(self, extension, expected, path_type): @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path]) def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest - try: - tmp = tempfile.NamedTemporaryFile( - dir=os.path.expanduser("~/"), delete=False - ) + with tempfile.NamedTemporaryFile(dir=os.path.expanduser("~/")) as tmp: filename = "~/" + ntpath.basename(tmp.name) - tmp.close() - with icom.get_handle(filename, "w") as handles: + with icom.get_handle(filename, "r") as handles: assert os.path.isabs(handles.handle.name) assert os.path.expanduser(filename) == handles.handle.name - finally: - os.unlink(tmp.name) def test_get_handle_with_buffer(self): input_buffer = StringIO() From 2c68947c7365ae3e9c5f854965e91691cd2fd89f Mon Sep 17 00:00:00 2001 From: chrispe Date: Mon, 1 Mar 2021 20:20:27 +0200 Subject: [PATCH 3/9] Merge branch 'master' of https://github.com/pandas-dev/pandas into fix-issue-40091 From 1714313cd8c524ec438e5115a43f14aaf0c39e8f Mon Sep 17 00:00:00 2001 From: chrispe Date: Mon, 1 Mar 2021 20:23:18 +0200 Subject: [PATCH 4/9] Use pathlib.Path instead of os.path --- pandas/tests/io/test_common.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index ad14c9b5020fb..62a55cbe134bb 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -7,7 +7,6 @@ StringIO, ) import mmap -import ntpath import os from pathlib import Path import tempfile @@ -121,11 +120,11 @@ def test_infer_compression_from_path(self, extension, expected, path_type): @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path]) def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest - with tempfile.NamedTemporaryFile(dir=os.path.expanduser("~/")) as tmp: - filename = "~/" + ntpath.basename(tmp.name) + with tempfile.NamedTemporaryFile(dir=Path.home()) as tmp: + filename = Path.home() / Path(tmp.name).name with icom.get_handle(filename, "r") as handles: - assert os.path.isabs(handles.handle.name) - assert os.path.expanduser(filename) == handles.handle.name + assert Path(handles.handle.name).is_absolute() + assert str(filename) == handles.handle.name def test_get_handle_with_buffer(self): input_buffer = StringIO() From 087d946c092388430e4dabe29345502f2efaf484 Mon Sep 17 00:00:00 2001 From: chrispe Date: Mon, 1 Mar 2021 20:33:08 +0200 Subject: [PATCH 5/9] Use hardcoded path for filename Since we want to test for hardcoded case of "~", we should not use the Path.home() --- pandas/tests/io/test_common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 62a55cbe134bb..334ff8c7085a5 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -121,10 +121,10 @@ def test_infer_compression_from_path(self, extension, expected, path_type): def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest with tempfile.NamedTemporaryFile(dir=Path.home()) as tmp: - filename = Path.home() / Path(tmp.name).name + filename = "~/" + Path(tmp.name).name with icom.get_handle(filename, "r") as handles: assert Path(handles.handle.name).is_absolute() - assert str(filename) == handles.handle.name + assert str(Path(filename).expanduser()) == handles.handle.name def test_get_handle_with_buffer(self): input_buffer = StringIO() From af68767ccd920042c7b97e45724bf02c29559165 Mon Sep 17 00:00:00 2001 From: chrispe Date: Mon, 1 Mar 2021 22:02:30 +0200 Subject: [PATCH 6/9] Cast filename with path_type --- pandas/tests/io/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 334ff8c7085a5..b975280a41b5b 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -121,7 +121,7 @@ def test_infer_compression_from_path(self, extension, expected, path_type): def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest with tempfile.NamedTemporaryFile(dir=Path.home()) as tmp: - filename = "~/" + Path(tmp.name).name + filename = path_type("~/" + Path(tmp.name).name) with icom.get_handle(filename, "r") as handles: assert Path(handles.handle.name).is_absolute() assert str(Path(filename).expanduser()) == handles.handle.name From d1e983344817ccdddcd5e886f5d8aafbbd26a754 Mon Sep 17 00:00:00 2001 From: chrispe Date: Mon, 1 Mar 2021 22:36:17 +0200 Subject: [PATCH 7/9] 1st attempt to solve windows permissions error --- pandas/tests/io/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index b975280a41b5b..f431beea2fffa 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -120,7 +120,7 @@ def test_infer_compression_from_path(self, extension, expected, path_type): @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path]) def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest - with tempfile.NamedTemporaryFile(dir=Path.home()) as tmp: + with tempfile.NamedTemporaryFile(dir=Path.home(), mode="w+") as tmp: filename = path_type("~/" + Path(tmp.name).name) with icom.get_handle(filename, "r") as handles: assert Path(handles.handle.name).is_absolute() From 3e5373f41602112a7b98bf99b0037b058ef3551a Mon Sep 17 00:00:00 2001 From: chrispe Date: Tue, 2 Mar 2021 00:07:00 +0200 Subject: [PATCH 8/9] Create temp dir to avoid race condition on windows --- pandas/tests/io/test_common.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index f431beea2fffa..d4d2d75baa7f5 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -120,9 +120,9 @@ def test_infer_compression_from_path(self, extension, expected, path_type): @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path]) def test_get_handle_with_path(self, path_type): # ignore LocalPath: it creates strange paths: /absolute/~/sometest - with tempfile.NamedTemporaryFile(dir=Path.home(), mode="w+") as tmp: - filename = path_type("~/" + Path(tmp.name).name) - with icom.get_handle(filename, "r") as handles: + with tempfile.TemporaryDirectory(dir=Path.home()) as tmp: + filename = path_type("~/" + Path(tmp).name + "/sometest") + with icom.get_handle(filename, "w") as handles: assert Path(handles.handle.name).is_absolute() assert str(Path(filename).expanduser()) == handles.handle.name From 22b3f3ffa02eae9f579897729febe9b601209943 Mon Sep 17 00:00:00 2001 From: chrispe Date: Tue, 2 Mar 2021 00:56:17 +0200 Subject: [PATCH 9/9] Reverted back to assert with os.path --- pandas/tests/io/test_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index d4d2d75baa7f5..e1dcec56913f9 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -124,7 +124,7 @@ def test_get_handle_with_path(self, path_type): filename = path_type("~/" + Path(tmp).name + "/sometest") with icom.get_handle(filename, "w") as handles: assert Path(handles.handle.name).is_absolute() - assert str(Path(filename).expanduser()) == handles.handle.name + assert os.path.expanduser(filename) == handles.handle.name def test_get_handle_with_buffer(self): input_buffer = StringIO()