Skip to content

Commit

Permalink
Merge pull request #1572 from ssanderson/more-unicode-fun
Browse files Browse the repository at this point in the history
BUG: Fix unicode errors for py2 client and py3 server.
  • Loading branch information
llllllllll committed Aug 17, 2016
2 parents dfb2c3b + 98bf0c4 commit a1aa9a2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
8 changes: 8 additions & 0 deletions blaze/compatibility.py
Expand Up @@ -4,6 +4,7 @@
from types import MethodType

import pandas.util.testing as tm
from toolz import identity

PY3 = sys.version_info[0] == 3
PY2 = sys.version_info[0] == 2
Expand Down Expand Up @@ -135,3 +136,10 @@ def assert_series_equal(left, right, check_names=True, **kwargs):
if check_names:
assert left.name == right.name
return tm.assert_series_equal(left, right, **kwargs)


if PY2:
def u8(cs):
return cs.decode('utf-8')
else:
u8 = identity
10 changes: 5 additions & 5 deletions blaze/server/serialization/json_dumps.py
Expand Up @@ -27,27 +27,27 @@ def json_dumps(dt):
if not dt.tzname():
s += u'Z'

return {'__!datetime': s}
return {u'__!datetime': s}


@dispatch(frozenset)
def json_dumps(ds):
return {'__!frozenset': list(ds)}
return {u'__!frozenset': list(ds)}


@dispatch(datetime.timedelta)
def json_dumps(ds):
return {'__!timedelta': ds.total_seconds()}
return {u'__!timedelta': ds.total_seconds()}


@dispatch(Mono)
def json_dumps(m):
return {'__!mono': unicode(m)}
return {u'__!mono': unicode(m)}


@dispatch(DataShape)
def json_dumps(ds):
return {'__!datashape': unicode(ds)}
return {u'__!datashape': unicode(ds)}


@dispatch(object)
Expand Down
5 changes: 3 additions & 2 deletions blaze/server/serialization/json_dumps_trusted.py
Expand Up @@ -3,6 +3,7 @@
from collections import Callable

from blaze.dispatch import dispatch
from blaze.compatibility import u8
from functools import partial

from .json_dumps import json_dumps
Expand All @@ -17,8 +18,8 @@ def json_dumps_trusted(f):
# let the server serialize any callable - this is only used for testing
# at present - do the error handling when json comes from client so in
# object_hook, catch anything that is not pandas_numpy
fcn = ".".join([f.__module__, f.__name__])
return {'__!callable': fcn}
fcn = u8(".".join([f.__module__, f.__name__]))
return {u'__!callable': fcn}


for tps, func in json_dumps.funcs.items():
Expand Down
17 changes: 5 additions & 12 deletions blaze/server/server.py
Expand Up @@ -21,11 +21,11 @@
from flask import Blueprint, Flask, Response
from flask_cors import cross_origin
from werkzeug.http import parse_options_header
from toolz import valmap, compose, identity
from toolz import valmap, compose

import blaze
from blaze import compute, resource
from blaze.compatibility import ExitStack, PY2
from blaze.compatibility import ExitStack, u8
from blaze.compute import compute_up
from .serialization import json, all_formats
from ..interactive import _Data
Expand Down Expand Up @@ -401,13 +401,6 @@ def shape():
return pprint(discover(_get_data()), width=0)


if PY2:
def _u8(cs):
return cs.decode('utf-8')
else:
_u8 = identity


def to_tree(expr, names=None):
""" Represent Blaze expression with core data structures
Expand Down Expand Up @@ -466,12 +459,12 @@ def to_tree(expr, names=None):
if isinstance(expr, expr_utils._slice):
return to_tree(expr.as_slice(), names=names)
elif isinstance(expr, _Data):
return to_tree(symbol(_u8(expr._name), expr.dshape), names)
return to_tree(symbol(u8(expr._name), expr.dshape), names)
elif isinstance(expr, Expr):
return {u'op': _u8(type(expr).__name__),
return {u'op': u8(type(expr).__name__),
u'args': [to_tree(arg, names) for arg in expr._args]}
elif isinstance(expr, str):
return _u8(expr)
return u8(expr)
else:
return expr

Expand Down
3 changes: 3 additions & 0 deletions docs/source/whatsnew/0.11.1.txt
Expand Up @@ -41,6 +41,9 @@ Bug Fixes
* Fixed a testing regression introduced by the latest pymysql version
(:issue:`1571`).

* Fixed unicode conversion issues when using a Python 3 blaze server and a
Python 2 client (:issue:`1572`), (:issue:`1566`).

Miscellaneous
~~~~~~~~~~~~~

Expand Down

0 comments on commit a1aa9a2

Please sign in to comment.