Add workaround for displaCy server on Python 2/3 (resolves #1227)

Make sure status and headers are bytes on Python 2 and strings on
Python 3
This commit is contained in:
ines 2017-08-01 01:11:35 +02:00
parent c16ef0a85c
commit 78e262140f
2 changed files with 12 additions and 1 deletions

View File

@ -61,6 +61,14 @@ elif is_python3:
json_dumps = lambda data: ujson.dumps(data, indent=2) json_dumps = lambda data: ujson.dumps(data, indent=2)
path2str = lambda path: str(path) path2str = lambda path: str(path)
def b_to_str(b_str):
if is_python2:
return b_str
# important: if no encoding is set, string becomes "b'...'"
return str(b_str, encoding='utf8')
def getattr_(obj, name, *default): def getattr_(obj, name, *default):
if is_python3 and isinstance(name, bytes): if is_python3 and isinstance(name, bytes):
name = name.decode('utf8') name = name.decode('utf8')

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
from .render import DependencyRenderer, EntityRenderer from .render import DependencyRenderer, EntityRenderer
from ..tokens import Doc from ..tokens import Doc
from ..compat import b_to_str
from ..util import prints, is_in_jupyter from ..util import prints, is_in_jupyter
@ -65,7 +66,9 @@ def serve(docs, style='dep', page=True, minify=False, options={}, manual=False,
def app(environ, start_response): def app(environ, start_response):
start_response('200 OK', [('Content-type', 'text/html; charset=utf-8')]) # headers and status need to be bytes in Python 2, see #1227
headers = [(b_to_str(b'Content-type'), b_to_str(b'text/html; charset=utf-8'))]
start_response(b_to_str(b'200 OK'), headers)
res = _html['parsed'].encode(encoding='utf-8') res = _html['parsed'].encode(encoding='utf-8')
return [res] return [res]