From 81c9b3570b70c092a2428fe9f21fb897ea5486b1 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 20 May 2013 09:44:05 +0100 Subject: [PATCH 1/4] Removed 2.5 from travis --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fd590bea..307945bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: python python: - - 2.5 - 2.6 - 2.7 - pypy From aecc41deb8d5cb8a1505be52648c78432e9238ae Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 20 May 2013 09:47:07 +0100 Subject: [PATCH 2/4] Restore 2.5 support for the time being --- .travis.yml | 1 + flask/ctx.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 307945bf..fd590bea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: python python: + - 2.5 - 2.6 - 2.7 - pypy diff --git a/flask/ctx.py b/flask/ctx.py index 6b271687..0340486b 100644 --- a/flask/ctx.py +++ b/flask/ctx.py @@ -9,6 +9,8 @@ :license: BSD, see LICENSE for more details. """ +from __future__ import with_statement + import sys from functools import update_wrapper From eb023bcfad7741248705f6715055ffe46928b7fe Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 23 May 2013 13:46:51 +0100 Subject: [PATCH 3/4] Support old and new name for json --- flask/json.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/flask/json.py b/flask/json.py index 717eb2ab..0f1d4c2a 100644 --- a/flask/json.py +++ b/flask/json.py @@ -15,8 +15,11 @@ from .globals import current_app, request from werkzeug.http import http_date # Use the same json implementation as itsdangerous on which we -# depend anyways. -from itsdangerous import simplejson as _json +# depend anyways. This name changed at one point so support both. +try: + from itsdangerous import simplejson as _json +except ImportError: + from itsdangerous import json as _json # figure out if simplejson escapes slashes. This behavior was changed From 4c27f7a8c4bbe6621681178be716e6270067a3ad Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 23 May 2013 13:59:10 +0100 Subject: [PATCH 4/4] Removed incorrect JSON exception subclasses --- CHANGES | 3 +++ flask/exceptions.py | 48 -------------------------------------- flask/testsuite/helpers.py | 14 +---------- flask/wrappers.py | 19 ++++++--------- 4 files changed, 11 insertions(+), 73 deletions(-) delete mode 100644 flask/exceptions.py diff --git a/CHANGES b/CHANGES index e383b56f..46b816a5 100644 --- a/CHANGES +++ b/CHANGES @@ -54,6 +54,9 @@ Release date to be decided. - Added `message_flashed` signal that simplifies flashing testing. - Added support for copying of request contexts for better working with greenlets. +- Removed custom JSON HTTP exception subclasses. If you were relying on them + you can reintroduce them again yourself trivially. Using them however is + strongly discouraged as the interface was flawed. Version 0.9 ----------- diff --git a/flask/exceptions.py b/flask/exceptions.py deleted file mode 100644 index 83b9556b..00000000 --- a/flask/exceptions.py +++ /dev/null @@ -1,48 +0,0 @@ -# -*- coding: utf-8 -*- -""" - flask.exceptions - ~~~~~~~~~~~~ - - Flask specific additions to :class:`~werkzeug.exceptions.HTTPException` - - :copyright: (c) 2011 by Armin Ronacher. - :license: BSD, see LICENSE for more details. -""" -from werkzeug.exceptions import HTTPException, BadRequest -from . import json - - -class JSONHTTPException(HTTPException): - """A base class for HTTP exceptions with ``Content-Type: - application/json``. - - The ``description`` attribute of this class must set to a string (*not* an - HTML string) which describes the error. - - """ - - def get_body(self, environ): - """Overrides :meth:`werkzeug.exceptions.HTTPException.get_body` to - return the description of this error in JSON format instead of HTML. - - """ - return json.dumps(dict(description=self.get_description(environ))) - - def get_headers(self, environ): - """Returns a list of headers including ``Content-Type: - application/json``. - - """ - return [('Content-Type', 'application/json')] - - -class JSONBadRequest(JSONHTTPException, BadRequest): - """Represents an HTTP ``400 Bad Request`` error whose body contains an - error message in JSON format instead of HTML format (as in the superclass). - """ - - #: The description of the error which occurred as a string. - description = ( - 'The browser (or proxy) sent a request that this server could not ' - 'understand.' - ) diff --git a/flask/testsuite/helpers.py b/flask/testsuite/helpers.py index fdf2d89f..8e471af9 100644 --- a/flask/testsuite/helpers.py +++ b/flask/testsuite/helpers.py @@ -35,23 +35,11 @@ class JSONTestCase(FlaskTestCase): app = flask.Flask(__name__) @app.route('/json', methods=['POST']) def return_json(): - return unicode(flask.request.json) + return flask.jsonify(foo=unicode(flask.request.json)) c = app.test_client() rv = c.post('/json', data='malformed', content_type='application/json') self.assert_equal(rv.status_code, 400) - def test_json_bad_requests_content_type(self): - app = flask.Flask(__name__) - @app.route('/json', methods=['POST']) - def return_json(): - return unicode(flask.request.json) - c = app.test_client() - rv = c.post('/json', data='malformed', content_type='application/json') - self.assert_equal(rv.status_code, 400) - self.assert_equal(rv.mimetype, 'application/json') - self.assert_('description' in flask.json.loads(rv.data)) - self.assert_('

' not in flask.json.loads(rv.data)['description']) - def test_json_body_encoding(self): app = flask.Flask(__name__) app.testing = True diff --git a/flask/wrappers.py b/flask/wrappers.py index a56fe5d7..100decd0 100644 --- a/flask/wrappers.py +++ b/flask/wrappers.py @@ -11,8 +11,8 @@ from werkzeug.wrappers import Request as RequestBase, Response as ResponseBase from werkzeug.utils import cached_property +from werkzeug.exceptions import BadRequest -from .exceptions import JSONBadRequest from .debughelpers import attach_enctype_error_multidict from . import json from .globals import _request_ctx_stack @@ -107,21 +107,16 @@ class Request(RequestBase): def on_json_loading_failed(self, e): """Called if decoding of the JSON data failed. The return value of this method is used by :attr:`json` when an error occurred. The default - implementation raises a :class:`JSONBadRequest`, which is a subclass of - :class:`~werkzeug.exceptions.BadRequest` which sets the - ``Content-Type`` to ``application/json`` and provides a JSON-formatted - error description:: + implementation just raises a :class:`BadRequest` exception. - {"description": "The browser (or proxy) sent a request that \ - this server could not understand."} - - .. versionchanged:: 0.9 - Return a :class:`JSONBadRequest` instead of a - :class:`~werkzeug.exceptions.BadRequest` by default. + .. versionchanged:: 0.10 + Removed buggy previous behavior of generating a random JSON + response. If you want that behavior back you can trivially + add it by subclassing. .. versionadded:: 0.8 """ - raise JSONBadRequest() + raise BadRequest() def _load_form_data(self): RequestBase._load_form_data(self)