summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py11
-rw-r--r--tests/test_history.py17
2 files changed, 12 insertions, 16 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index a85a39dc..8310da93 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3469,15 +3469,18 @@ class Cmd(cmd.Cmd):
# first we try and unpickle the history file
history = History()
+ # on Windows, trying to open a directory throws a permission
+ # error, not a `IsADirectoryError`. So we'll check it ourselves.
+ if os.path.isdir(hist_file):
+ msg = "persistent history file '{}' is a directory"
+ self.perror(msg.format(hist_file))
+ return
+
try:
with open(hist_file, 'rb') as fobj:
history = pickle.load(fobj)
except (FileNotFoundError, KeyError, EOFError):
pass
- except IsADirectoryError:
- msg = "persistent history file '{}' is a directory"
- self.perror(msg.format(hist_file))
- return
except OSError as ex:
msg = "can not read persistent history file '{}': {}"
self.perror(msg.format(hist_file, ex), traceback_war=False)
diff --git a/tests/test_history.py b/tests/test_history.py
index 09b83279..3a52bda9 100644
--- a/tests/test_history.py
+++ b/tests/test_history.py
@@ -459,18 +459,11 @@ def hist_file():
pass
def test_bad_history_file_path(capsys, request):
- # can't use tempfile.TemporaryDirectory() as a context on Appveyor
- # on windows. it causes a file locking issue which is reflected as
- # a permission exception
- test_dir = tempfile.mkdtemp()
- # Create a new cmd2 app
- cmd2.Cmd(persistent_history_file=test_dir)
- _, err = capsys.readouterr()
- assert 'is a directory' in err
- try:
- os.rmdir(test_dir)
- except OSError:
- pass
+ with tempfile.TemporaryDirectory() as test_dir:
+ # Create a new cmd2 app
+ cmd2.Cmd(persistent_history_file=test_dir)
+ _, err = capsys.readouterr()
+ assert 'is a directory' in err
def test_history_file_conversion_no_truncate_on_init(hist_file, capsys):
# test the code that converts a plain text history file to a pickle binary