Skip to content
Myr

Check for a request ctx before using the request.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Use the app json coder when blueprint json coder is set to none. Revert the failling test to using an app_context re #1710

authored by RamiC · committed by RamiC · parent 501b859 · Browse files · 2 files+13 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−6 removed

  • tests:+2 added−2 removed
  • other:+11 added−4 removed

2 files+13 added−6 removed

modified:flask/json.py+11 added-4 removed
11 unmodified lines
12import uuid13from datetime import date14from .globals import current_app, request15from ._compat import text_type, PY216from .ctx import has_request_context1718from werkzeug.http import http_date19from jinja2 import Markup20
73 unmodified lines
9495def _dump_arg_defaults(kwargs):96 """Inject default arguments for dump functions."""97 if current_app:97 bp = current_app.blueprints.get(request.blueprint, None)98 kwargs.setdefault('cls', bp.json_encoder if bp else current_app.json_encoder)98 bp = current_app.blueprints.get(request.blueprint,99 None) if has_request_context() else None100 kwargs.setdefault('cls',101 bp.json_encoder if bp and bp.json_encoder102 else current_app.json_encoder)103 if not current_app.config['JSON_AS_ASCII']:104 kwargs.setdefault('ensure_ascii', False)105 kwargs.setdefault('sort_keys', current_app.config['JSON_SORT_KEYS'])106 else:
3 unmodified lines
110111def _load_arg_defaults(kwargs):112 """Inject default arguments for load functions."""113 if current_app:110 bp = current_app.blueprints.get(request.blueprint, None)111 kwargs.setdefault('cls', bp.json_decoder if bp else current_app.json_decoder)114 bp = current_app.blueprints.get(request.blueprint,115 None) if has_request_context() else None116 kwargs.setdefault('cls',117 bp.json_decoder if bp and bp.json_decoder118 else current_app.json_decoder)119 else:120 kwargs.setdefault('cls', JSONDecoder)121122
159 unmodified lines
modified:tests/test_helpers.py+2 added-2 removed
88 unmodified lines
89 def test_json_as_unicode(self):90 app = flask.Flask(__name__)9192 app.config['JSON_AS_ASCII'] = True93 with app.test_request_context():93 with app.app_context():94 rv = flask.json.dumps(u'\N{SNOWMAN}')95 assert rv == '"\\u2603"'9697 app.config['JSON_AS_ASCII'] = False98 with app.test_request_context():98 with app.app_context():99 rv = flask.json.dumps(u'\N{SNOWMAN}')100 assert rv == u'"\u2603"'101102 def test_jsonify_basic_types(self):
711 unmodified lines