Skip to content

Commit e2283d6

Browse files
authored
Merge pull request ipython#12680 from deep-jkl/pathlib-for-utils-tempdir
Use pathlib in utils/tempdir
2 parents b482e64 + abfaafd commit e2283d6

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

IPython/utils/tempdir.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import os as _os
8+
from pathlib import Path
89
from tempfile import TemporaryDirectory
910

1011

@@ -22,7 +23,7 @@ def __init__(self, filename, mode='w+b', bufsize=-1, **kwds):
2223
2324
"""
2425
self._tmpdir = TemporaryDirectory(**kwds)
25-
path = _os.path.join(self._tmpdir.name, filename)
26+
path = Path(self._tmpdir.name) / filename
2627
self.file = open(path, mode, bufsize)
2728

2829
def cleanup(self):
@@ -48,7 +49,7 @@ class TemporaryWorkingDirectory(TemporaryDirectory):
4849
...
4950
"""
5051
def __enter__(self):
51-
self.old_wd = _os.getcwd()
52+
self.old_wd = Path.cwd()
5253
_os.chdir(self.name)
5354
return super(TemporaryWorkingDirectory, self).__enter__()
5455

IPython/utils/tests/test_tempdir.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# the file COPYING, distributed as part of this software.
66
#-----------------------------------------------------------------------------
77

8-
import os
8+
from pathlib import Path
99

1010
from IPython.utils.tempdir import NamedFileInTemporaryDirectory
1111
from IPython.utils.tempdir import TemporaryWorkingDirectory
@@ -15,14 +15,15 @@ def test_named_file_in_temporary_directory():
1515
with NamedFileInTemporaryDirectory('filename') as file:
1616
name = file.name
1717
assert not file.closed
18-
assert os.path.exists(name)
18+
assert Path(name).exists()
1919
file.write(b'test')
2020
assert file.closed
21-
assert not os.path.exists(name)
21+
assert not Path(name).exists()
2222

2323
def test_temporary_working_directory():
24-
with TemporaryWorkingDirectory() as dir:
25-
assert os.path.exists(dir)
26-
assert os.path.realpath(os.curdir) == os.path.realpath(dir)
27-
assert not os.path.exists(dir)
28-
assert os.path.abspath(os.curdir) != dir
24+
with TemporaryWorkingDirectory() as directory:
25+
directory_path = Path(directory)
26+
assert directory_path.exists()
27+
assert Path.cwd() == directory_path.resolve()
28+
assert not directory_path.exists()
29+
assert Path.cwd() != directory_path.resolve()

0 commit comments

Comments
 (0)