Skip to content

Commit 5fe14a2

Browse files
committed
Manually check whether persistent_history_file is a directory
1 parent 0ed74c7 commit 5fe14a2

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

cmd2/cmd2.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,15 +3469,18 @@ def _initialize_history(self, hist_file):
34693469

34703470
# first we try and unpickle the history file
34713471
history = History()
3472+
# on Windows, trying to open a directory throws a permission
3473+
# error, not a `IsADirectoryError`. So we'll check it ourselves.
3474+
if os.path.isdir(hist_file):
3475+
msg = "persistent history file '{}' is a directory"
3476+
self.perror(msg.format(hist_file))
3477+
return
3478+
34723479
try:
34733480
with open(hist_file, 'rb') as fobj:
34743481
history = pickle.load(fobj)
34753482
except (FileNotFoundError, KeyError, EOFError):
34763483
pass
3477-
except IsADirectoryError:
3478-
msg = "persistent history file '{}' is a directory"
3479-
self.perror(msg.format(hist_file))
3480-
return
34813484
except OSError as ex:
34823485
msg = "can not read persistent history file '{}': {}"
34833486
self.perror(msg.format(hist_file, ex), traceback_war=False)

tests/test_history.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,11 @@ def hist_file():
459459
pass
460460

461461
def test_bad_history_file_path(capsys, request):
462-
# can't use tempfile.TemporaryDirectory() as a context on Appveyor
463-
# on windows. it causes a file locking issue which is reflected as
464-
# a permission exception
465-
test_dir = tempfile.mkdtemp()
466-
# Create a new cmd2 app
467-
cmd2.Cmd(persistent_history_file=test_dir)
468-
_, err = capsys.readouterr()
469-
assert 'is a directory' in err
470-
try:
471-
os.rmdir(test_dir)
472-
except OSError:
473-
pass
462+
with tempfile.TemporaryDirectory() as test_dir:
463+
# Create a new cmd2 app
464+
cmd2.Cmd(persistent_history_file=test_dir)
465+
_, err = capsys.readouterr()
466+
assert 'is a directory' in err
474467

475468
def test_history_file_conversion_no_truncate_on_init(hist_file, capsys):
476469
# test the code that converts a plain text history file to a pickle binary

0 commit comments

Comments
 (0)