Skip to content

Commit

Permalink
Support encoding memoryview to JSON
Browse files Browse the repository at this point in the history
psycopg2 returns `buffer` objects in Python 2.7 and `memoryview`
in Python 3. See #3156
  • Loading branch information
NicolasLM committed Oct 10, 2019
1 parent c4d94e1 commit b4df990
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
6 changes: 1 addition & 5 deletions redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

from .human_time import parse_human_time

try:
buffer
except NameError:
buffer = bytes

COMMENTS_REGEX = re.compile("/\*.*?\*/")
WRITER_ENCODING = os.environ.get('REDASH_CSV_WRITER_ENCODING', 'utf-8')
Expand Down Expand Up @@ -101,7 +97,7 @@ def default(self, o):
result = o.isoformat()
if o.microsecond:
result = result[:12]
elif isinstance(o, buffer):
elif isinstance(o, memoryview):
result = binascii.hexlify(o)
else:
result = super(JSONEncoder, self).default(o)
Expand Down
6 changes: 1 addition & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from redash.utils import (build_url, collect_parameters_from_request,
filter_none, json_dumps, generate_token)

try:
buffer
except NameError:
buffer = bytes

DummyRequest = namedtuple('DummyRequest', ['host', 'scheme'])

Expand Down Expand Up @@ -49,7 +45,7 @@ def test_skips_nones(self):

class TestJsonDumps(TestCase):
def test_handles_binary(self):
self.assertEqual(json_dumps(buffer("test")), '"74657374"')
self.assertEqual(json_dumps(memoryview(b"test")), '"74657374"')


class TestGenerateToken(TestCase):
Expand Down

0 comments on commit b4df990

Please sign in to comment.