From 3610ac0df063a0cf20af106d26e362fecfb6d446 Mon Sep 17 00:00:00 2001 From: Roger Hunwicks Date: Thu, 5 Oct 2017 23:02:20 +0200 Subject: [PATCH] Py2 fix for DataFrameCache - see #3302 --- contrib/connectors/pandas/cache.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/contrib/connectors/pandas/cache.py b/contrib/connectors/pandas/cache.py index 232eaaf7d1704..f013050715ed0 100644 --- a/contrib/connectors/pandas/cache.py +++ b/contrib/connectors/pandas/cache.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function +from __future__ import unicode_literals from io import open import json @@ -13,6 +14,7 @@ import pickle import pandas as pd +from six import u from werkzeug.contrib.cache import FileSystemCache from werkzeug.posixemulation import rename @@ -55,7 +57,7 @@ def _prune(self): for idx, cname in enumerate(entries): mname = os.path.splitext(cname)[0] + self._fs_metadata_suffix try: - with open(mname, 'r') as f: + with open(mname, 'r', encoding='utf-8') as f: metadata = json.load(f) except (IOError, OSError): metadata = {'expires': -1} @@ -83,7 +85,7 @@ def get(self, key): cname = filename + self._fs_cache_suffix mname = filename + self._fs_metadata_suffix try: - with open(mname, 'r') as f: + with open(mname, 'r', encoding='utf-8') as f: metadata = json.load(f) except (IOError, OSError): metadata = {'expires': -1} @@ -133,8 +135,8 @@ def set(self, key, value, timeout=None): metadata['format'] = 'pickle' rename(tmp, cname) os.chmod(cname, self._mode) - with open(mname, 'w') as f: - json.dump(metadata, f) + with open(mname, 'w', encoding='utf-8') as f: + f.write(u(json.dumps(metadata))) os.chmod(mname, self._mode) except (IOError, OSError): return False @@ -158,7 +160,7 @@ def has(self, key): cname = filename + self._fs_cache_suffix mname = filename + self._fs_metadata_suffix try: - with open(mname, 'r') as f: + with open(mname, 'r', encoding='utf-8') as f: metadata = json.load(f) except (IOError, OSError): metadata = {'expires': -1}