Skip to content

Commit abc1505

Browse files
committed
Fixed various issues on the Python 3 port
1 parent 8aaf302 commit abc1505

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

flask/_compat.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def reraise(tp, value, tb=None):
4747
encode_filename = _identity
4848
get_next = lambda x: x.__next__
4949

50-
from urllib.parse import urlparse
51-
5250
else:
5351
unichr = unichr
5452
text_type = unicode
@@ -86,8 +84,6 @@ def encode_filename(filename):
8684
return filename.encode('utf-8')
8785
return filename
8886

89-
from urlparse import urlparse
90-
9187

9288
def with_metaclass(meta, *bases):
9389
# This requires a bit of explanation: the basic idea is to make a

flask/json.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
:copyright: (c) 2012 by Armin Ronacher.
99
:license: BSD, see LICENSE for more details.
1010
"""
11+
import io
1112
import uuid
1213
from datetime import datetime
1314
from .globals import current_app, request
14-
from ._compat import text_type
15+
from ._compat import text_type, PY2
1516

1617
from werkzeug.http import http_date
1718

@@ -33,6 +34,20 @@
3334
'jsonify']
3435

3536

37+
def _wrap_reader_for_text(fp, encoding):
38+
if isinstance(fp.read(0), bytes):
39+
fp = io.TextIOWrapper(io.BufferedReader(fp), encoding)
40+
return fp
41+
42+
43+
def _wrap_writer_for_text(fp, encoding):
44+
try:
45+
fp.write('')
46+
except TypeError:
47+
fp = io.TextIOWrapper(fp, encoding)
48+
return fp
49+
50+
3651
class JSONEncoder(_json.JSONEncoder):
3752
"""The default Flask JSON encoder. This one extends the default simplejson
3853
encoder by also supporting ``datetime`` objects, ``UUID`` as well as
@@ -100,13 +115,20 @@ def dumps(obj, **kwargs):
100115
and can be overriden by the simplejson ``ensure_ascii`` parameter.
101116
"""
102117
_dump_arg_defaults(kwargs)
103-
return _json.dumps(obj, **kwargs)
118+
encoding = kwargs.pop('encoding', None)
119+
rv = _json.dumps(obj, **kwargs)
120+
if encoding is not None and isinstance(rv, text_type):
121+
rv = rv.encode(encoding)
122+
return rv
104123

105124

106125
def dump(obj, fp, **kwargs):
107126
"""Like :func:`dumps` but writes into a file object."""
108127
_dump_arg_defaults(kwargs)
109-
return _json.dump(obj, fp, **kwargs)
128+
encoding = kwargs.pop('encoding', None)
129+
if encoding is not None:
130+
fp = _wrap_writer_for_text(fp, encoding)
131+
_json.dump(obj, fp, **kwargs)
110132

111133

112134
def loads(s, **kwargs):
@@ -115,13 +137,17 @@ def loads(s, **kwargs):
115137
application on the stack.
116138
"""
117139
_load_arg_defaults(kwargs)
140+
if isinstance(s, bytes):
141+
s = s.decode(kwargs.pop('encoding', None) or 'utf-8')
118142
return _json.loads(s, **kwargs)
119143

120144

121145
def load(fp, **kwargs):
122146
"""Like :func:`loads` but reads from a file object.
123147
"""
124148
_load_arg_defaults(kwargs)
149+
if not PY2:
150+
fp = _wrap_reader_for_text(fp, kwargs.pop('encoding', None) or 'utf-8')
125151
return _json.load(fp, **kwargs)
126152

127153

@@ -148,7 +174,7 @@ def jsonify(*args, **kwargs):
148174
to this function are the same as to the :class:`dict` constructor.
149175
150176
Example usage::
151-
177+
152178
from flask import jsonify
153179
154180
@app.route('/_get_current_user')

flask/testing.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
from contextlib import contextmanager
1414
from werkzeug.test import Client, EnvironBuilder
1515
from flask import _request_ctx_stack
16-
from ._compat import urlparse
16+
17+
try:
18+
from werkzeug.urls import url_parse
19+
except ImportError:
20+
from urlparse import urlsplit as url_parse
1721

1822

1923
def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):
2024
"""Creates a new test builder with some application defaults thrown in."""
2125
http_host = app.config.get('SERVER_NAME')
2226
app_root = app.config.get('APPLICATION_ROOT')
2327
if base_url is None:
24-
url = urlparse(path)
28+
url = url_parse(path)
2529
base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')
2630
if app_root:
2731
base_url += app_root.lstrip('/')

0 commit comments

Comments
 (0)