From abfaafd712c817ff7d32a46ae555860f0758099e 2020-10-27 13:37:04 From: Jakub Klus Date: 2020-10-27 13:37:04 Subject: [PATCH] Use pathlib in utils/tempdir. --- diff --git a/IPython/utils/tempdir.py b/IPython/utils/tempdir.py index 98f6aeb..bbfffe9 100644 --- a/IPython/utils/tempdir.py +++ b/IPython/utils/tempdir.py @@ -5,6 +5,7 @@ creating a context manager for the working directory which is also temporary. """ import os as _os +from pathlib import Path from tempfile import TemporaryDirectory @@ -22,7 +23,7 @@ class NamedFileInTemporaryDirectory(object): """ self._tmpdir = TemporaryDirectory(**kwds) - path = _os.path.join(self._tmpdir.name, filename) + path = Path(self._tmpdir.name) / filename self.file = open(path, mode, bufsize) def cleanup(self): @@ -48,7 +49,7 @@ class TemporaryWorkingDirectory(TemporaryDirectory): ... """ def __enter__(self): - self.old_wd = _os.getcwd() + self.old_wd = Path.cwd() _os.chdir(self.name) return super(TemporaryWorkingDirectory, self).__enter__() diff --git a/IPython/utils/tests/test_tempdir.py b/IPython/utils/tests/test_tempdir.py index 18e94da..68a3e03 100644 --- a/IPython/utils/tests/test_tempdir.py +++ b/IPython/utils/tests/test_tempdir.py @@ -5,7 +5,7 @@ # the file COPYING, distributed as part of this software. #----------------------------------------------------------------------------- -import os +from pathlib import Path from IPython.utils.tempdir import NamedFileInTemporaryDirectory from IPython.utils.tempdir import TemporaryWorkingDirectory @@ -15,14 +15,15 @@ def test_named_file_in_temporary_directory(): with NamedFileInTemporaryDirectory('filename') as file: name = file.name assert not file.closed - assert os.path.exists(name) + assert Path(name).exists() file.write(b'test') assert file.closed - assert not os.path.exists(name) + assert not Path(name).exists() def test_temporary_working_directory(): - with TemporaryWorkingDirectory() as dir: - assert os.path.exists(dir) - assert os.path.realpath(os.curdir) == os.path.realpath(dir) - assert not os.path.exists(dir) - assert os.path.abspath(os.curdir) != dir + with TemporaryWorkingDirectory() as directory: + directory_path = Path(directory) + assert directory_path.exists() + assert Path.cwd() == directory_path.resolve() + assert not directory_path.exists() + assert Path.cwd() != directory_path.resolve()