fixed JSON history corruption

This commit is contained in:
Anthony Scopatz 2017-11-08 21:27:28 -05:00
parent 88647b4a8f
commit 5a958bfe71
3 changed files with 24 additions and 4 deletions

View file

@ -1,8 +1,6 @@
**Added:** None
**Changed:**
* ``xip`` is now ``xpip``, because of a conflict with the Mac OSX archive format
**Changed:** None
**Deprecated:** None

13
news/hiscor.rst Normal file
View file

@ -0,0 +1,13 @@
**Added:** None
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:**
* Made JSON history loading more robust to corrupt files.
**Security:** None

View file

@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
"""Implements JSON version of xonsh history backend."""
import os
import sys
import time
import json
import builtins
import collections
import threading
@ -396,7 +398,14 @@ class JsonHistory(History):
except ValueError:
# Invalid json file
continue
commands = json_file.load()['cmds']
try:
commands = json_file.load()['cmds']
except json.decoder.JSONDecodeError:
# file is corrupted somehow
if builtins.__xonsh_env__.get('XONSH_DEBUG') > 0:
msg = 'xonsh history file {0!r} is not valid JSON'
print(msg.format(f), file=sys.stderr)
continue
for c in commands:
yield {'inp': c['inp'].rstrip(), 'ts': c['ts'][0]}
# all items should also include session items