Fixed various issues on the Python 3 port
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 8aaf302 · Browse files · 3 files·+36 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+36 added−10 removed
- other:+36 added−10 removed
Provenance
- Commit
abc1505- Tree
3b9ba61- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
8aaf302
3 files changed+36 added−10 removed
modified:flask/_compat.py-4 removed
45 unmodified lines46 implements_to_string = _identity47 encode_filename = _identity48 get_next = lambda x: x.__next__49 50- from urllib.parse import urlparse51-50 else:51 unichr = unichr52 text_type = unicode53 range_type = xrange29 unmodified lines83 if isinstance(filename, unicode):84 return filename.encode('utf-8')85 return filename86 89- from urlparse import urlparse87 91-88 def with_metaclass(meta, *bases):89 # This requires a bit of explanation: the basic idea is to make a90 # dummy metaclass for one level of class instantiation that replaces91 # itself with the actual metaclass. Because of internal type checks20 unmodified linesmodified:flask/json.py+30 added-4 removed
6 unmodified lines7 8 :copyright: (c) 2012 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+import io12 import uuid13 from datetime import datetime14 from .globals import current_app, request14-from ._compat import text_type15+from ._compat import text_type, PY216 17 from werkzeug.http import http_date18 19 # Use the same json implementation as itsdangerous on which we13 unmodified lines33 'htmlsafe_dumps', 'JSONDecoder', 'JSONEncoder',34 'jsonify']35 36 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 fp41+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 fp49+50+51 class JSONEncoder(_json.JSONEncoder):52 """The default Flask JSON encoder. This one extends the default simplejson53 encoder by also supporting ``datetime`` objects, ``UUID`` as well as54 ``Markup`` objects which are serialized as RFC 822 datetime strings (same59 unmodified lines114 default is controlled by the ``JSON_AS_ASCII`` configuration variable115 and can be overriden by the simplejson ``ensure_ascii`` parameter.116 """117 _dump_arg_defaults(kwargs)118+ encoding = kwargs.pop('encoding', None)103- return _json.dumps(obj, **kwargs)119+ rv = _json.dumps(obj, **kwargs)120+ if encoding is not None and isinstance(rv, text_type):121+ rv = rv.encode(encoding)122+ return rv123 124 125 def dump(obj, fp, **kwargs):126 """Like :func:`dumps` but writes into a file object."""127 _dump_arg_defaults(kwargs)128+ encoding = kwargs.pop('encoding', None)129+ if encoding is not None:130+ fp = _wrap_writer_for_text(fp, encoding)109- return _json.dump(obj, fp, **kwargs)131+ _json.dump(obj, fp, **kwargs)132 133 134 def loads(s, **kwargs):135 """Unserialize a JSON object from a string ``s`` by using the application's136 configured decoder (:attr:`~flask.Flask.json_decoder`) if there is an137 application on the stack.138 """139 _load_arg_defaults(kwargs)140+ if isinstance(s, bytes):141+ s = s.decode(kwargs.pop('encoding', None) or 'utf-8')142 return _json.loads(s, **kwargs)143 144 145 def load(fp, **kwargs):146 """Like :func:`loads` but reads from a file object.147 """148 _load_arg_defaults(kwargs)149+ if not PY2:150+ fp = _wrap_reader_for_text(fp, kwargs.pop('encoding', None) or 'utf-8')151 return _json.load(fp, **kwargs)152 153 154 def htmlsafe_dumps(obj, **kwargs):18 unmodified lines173 the given arguments with an `application/json` mimetype. The arguments174 to this function are the same as to the :class:`dict` constructor.175 176 Example usage::151- 177+178 from flask import jsonify179 180 @app.route('/_get_current_user')181 def get_current_user():20 unmodified linesmodified:flask/testing.py+6 added-2 removed
11 unmodified lines12 13 from contextlib import contextmanager14 from werkzeug.test import Client, EnvironBuilder15 from flask import _request_ctx_stack16-from ._compat import urlparse16 17+try:18+ from werkzeug.urls import url_parse19+except ImportError:20+ from urlparse import urlsplit as url_parse21 22+23 def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):24 """Creates a new test builder with some application defaults thrown in."""25 http_host = app.config.get('SERVER_NAME')26 app_root = app.config.get('APPLICATION_ROOT')27 if base_url is None:24- url = urlparse(path)28+ url = url_parse(path)29 base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')30 if app_root:31 base_url += app_root.lstrip('/')32 if url.netloc:92 unmodified lines