fix unicode/json on py3

This commit is contained in:
Brian Warner 2017-09-13 18:18:29 -07:00
parent 2e0c97cea8
commit a1333e51cd
2 changed files with 3 additions and 3 deletions

View File

@ -9,7 +9,7 @@ class UsageLog(unittest.TestCase):
os.mkdir(d) os.mkdir(d)
usage_logfile = os.path.join(d, "usage.log") usage_logfile = os.path.join(d, "usage.log")
def read(): def read():
with open(usage_logfile, "rb") as f: with open(usage_logfile, "r") as f:
return [json.loads(line) for line in f.readlines()] return [json.loads(line) for line in f.readlines()]
t = Transit(None, usage_logfile, None) t = Transit(None, usage_logfile, None)
t.recordUsage(started=123, result="happy", total_bytes=100, t.recordUsage(started=123, result="happy", total_bytes=100,

View File

@ -300,7 +300,7 @@ class Transit(protocol.ServerFactory):
def _update_stats(self, total_bytes, mood): def _update_stats(self, total_bytes, mood):
try: try:
with open(self._stats_file, "rb") as f: with open(self._stats_file, "r") as f:
stats = json.load(f) stats = json.load(f)
except (EnvironmentError, ValueError): except (EnvironmentError, ValueError):
stats = {} stats = {}
@ -332,7 +332,7 @@ class Transit(protocol.ServerFactory):
um[m] = 0 um[m] = 0
um[mood] += 1 um[mood] += 1
tmpfile = self._stats_file + ".tmp" tmpfile = self._stats_file + ".tmp"
with open(tmpfile, "wb") as f: with open(tmpfile, "w") as f:
f.write(json.dumps(stats)) f.write(json.dumps(stats))
f.write("\n") f.write("\n")
os.rename(tmpfile, self._stats_file) os.rename(tmpfile, self._stats_file)