Skip to content

Commit ea58d89

Browse files
committed
Use pathlib instead of low-level os module
1 parent 2d56c5c commit ea58d89

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

bpython/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def get_key_no_doublebind(command):
203203

204204
return requested_key
205205

206-
struct.config_path = config_path
206+
struct.config_path = Path(config_path).absolute()
207207

208208
struct.dedent_after = config.getint("general", "dedent_after")
209209
struct.tab_length = config.getint("general", "tab_length")

bpython/repl.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
import textwrap
3434
import time
3535
import traceback
36+
from enum import Enum
3637
from itertools import takewhile
38+
from pathlib import Path
3739
from types import ModuleType
38-
from enum import Enum
3940

4041
from pygments.token import Token
4142
from pygments.lexers import Python3Lexer
@@ -435,11 +436,10 @@ def __init__(self, interp, config):
435436
self.closed = False
436437
self.clipboard = get_clipboard()
437438

438-
pythonhist = os.path.expanduser(self.config.hist_file)
439-
if os.path.exists(pythonhist):
439+
if self.config.hist_file.exists():
440440
try:
441441
self.rl_history.load(
442-
pythonhist, getpreferredencoding() or "ascii"
442+
self.config.hist_file, getpreferredencoding() or "ascii"
443443
)
444444
except OSError:
445445
pass
@@ -829,13 +829,13 @@ def write2file(self):
829829
self.interact.notify(_("Save cancelled."))
830830
return
831831

832-
if fn.startswith("~"):
833-
fn = os.path.expanduser(fn)
834-
if not fn.endswith(".py") and self.config.save_append_py:
835-
fn = fn + ".py"
832+
fn = Path(fn).expanduser()
833+
if fn.suffix != ".py" and self.config.save_append_py:
834+
# fn.with_suffix(".py") does not append if fn has a non-empty suffix
835+
fn = Path(f"{fn}.py")
836836

837837
mode = "w"
838-
if os.path.exists(fn):
838+
if fn.exists():
839839
mode = self.interact.file_prompt(
840840
_(
841841
"%s already exists. Do you "
@@ -941,10 +941,9 @@ def push(self, s, insert_into_history=True):
941941
return more
942942

943943
def insert_into_history(self, s):
944-
pythonhist = os.path.expanduser(self.config.hist_file)
945944
try:
946945
self.rl_history.append_reload_and_write(
947-
s, pythonhist, getpreferredencoding()
946+
s, self.config.hist_file, getpreferredencoding()
948947
)
949948
except RuntimeError as e:
950949
self.interact.notify(f"{e}")
@@ -1147,7 +1146,7 @@ def open_in_external_editor(self, filename):
11471146
return subprocess.call(args) == 0
11481147

11491148
def edit_config(self):
1150-
if not os.path.isfile(self.config.config_path):
1149+
if not self.config.config_path.is_file():
11511150
if self.interact.confirm(
11521151
_(
11531152
"Config file does not exist - create "
@@ -1160,17 +1159,15 @@ def edit_config(self):
11601159
)
11611160
# Py3 files need unicode
11621161
default_config = default_config.decode("ascii")
1163-
containing_dir = os.path.dirname(
1164-
os.path.abspath(self.config.config_path)
1165-
)
1166-
if not os.path.exists(containing_dir):
1167-
os.makedirs(containing_dir)
1162+
containing_dir = self.config.config_path.parent
1163+
if not containing_dir.exists():
1164+
containing_dir.mkdir(parents=True)
11681165
with open(self.config.config_path, "w") as f:
11691166
f.write(default_config)
11701167
except OSError as e:
11711168
self.interact.notify(
11721169
_("Error writing file '%s': %s")
1173-
% (self.config.config.path, e)
1170+
% (self.config.config_path, e)
11741171
)
11751172
return False
11761173
else:

bpython/test/test_repl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import unittest
99

1010
from itertools import islice
11+
from pathlib import Path
1112
from unittest import mock
1213

1314
from bpython import config, repl, cli, autocomplete
@@ -332,7 +333,7 @@ def setUp(self):
332333
def test_create_config(self):
333334
tmp_dir = tempfile.mkdtemp()
334335
try:
335-
config_path = os.path.join(tmp_dir, "newdir", "config")
336+
config_path = Path(tmp_dir) / "newdir" / "config"
336337
self.repl.config.config_path = config_path
337338
self.repl.edit_config()
338339
self.assertTrue(os.path.exists(config_path))

0 commit comments

Comments
 (0)