Skip to content

Commit

Permalink
The WSGI App is now thread-safe by default. Deprecated webapp2_extras…
Browse files Browse the repository at this point in the history
….local_app. Updated docs and version number.
  • Loading branch information
moraes committed Jul 17, 2011
1 parent fc78bcb commit fb715e0
Show file tree
Hide file tree
Showing 18 changed files with 243 additions and 113 deletions.
29 changes: 21 additions & 8 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
webapp2 is maintained by Rodrigo Moraes <[email protected]>.

Based on `webapp`_ with some functions and ideas borrowed from `WebOb`_,
`Tornado`_, `Werkzeug`_ and `Flask`_.

.. _webapp: http://code.google.com/appengine/docs/python/tools/webapp/
.. _WebOb: http://pythonpaste.org/webob/
.. _Tornado: http://www.tornadoweb.org/
.. _Werkzeug: http://werkzeug.pocoo.org/
.. _Flask: http://flask.pocoo.org/
webapp2 includes code or ideas ported or adapted from the following projects:

- `Google App Engine SDK <http://code.google.com/appengine/>`_:
- Copyright (c) 2008 Google Inc.
- Apache 2.0 License.

- `WebOb <http://pythonpaste.org/webob/>`_:
- Copyright (c) 2007 Ian Bicking and Contributors.
- MIT License.

- `Werkzeug <http://werkzeug.pocoo.org/>`_:
- Copyright (c) 2011 Werkzeug Team.
- BSD License.

- `Tornado <http://www.tornadoweb.org/>`_:
- Copyright (c) 2009 Facebook.
- Apache 2.0 License.

- `Flask <http://flask.pocoo.org/>`_:
- Copyright (c) 2010 Armin Ronacher and Contributors.
- BSD License.
19 changes: 13 additions & 6 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Development version
===================
Version 2.0 - July 17, 2011
===========================
- Added support for the (not yet released) App Engine Python 2.7 SDK.

- Deprecated webapp2_extras.local_app. WSGIApplication is now thread-safe
by default when webapp2_extras.local is available.
webapp2_extras.local_app.WSGIApplication can still be imported, but it is
just an alias to webapp2.WSGIApplication (and a deprecation warning is
emitted when it is imported).

- Like DomainRoute, PathPrefixRoute now only tests the nested routes if the
prefix matches. So now it is not only convenient but matches smartly and
faster. ;)
Expand All @@ -22,18 +28,19 @@ Development version
- webapp2.urlunsplit() was removed from the public API.

- Variables names in routes must now look like python identifiers -
this means that they can no longer start with a number.
this means that they can no longer start with a number, like before.

- Removed support for _anchor in uri_for() -- documented keyword is _fragment.
- Removed support for _anchor in uri_for() -- the documented keyword is
_fragment.

- Removed support for handler functions receiving (request, response). The
correct arguments for function handlers are (request, *args, **kwargs).

- webapp2_extras.json:
- Fixed an issue with importing json on non-GAE environments (missing
absolute import).
- Now imports simplejson directly instead of using Django, as it was
included since SDK 1.4.2.
- Now imports simplejson directly instead of using the one from Django,
since it is now included in the App Engine SDK (since version 1.4.2).

- Added WSGIApplication.clear_globals(). Instead of
app.set_globals(app=None, request=None), use app.clear_globals().
Expand Down
128 changes: 128 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,131 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


GOOGLE APP ENGINE SDK
=====================
Copyright 2008 Google Inc.
All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


WebOb
=====
Copyright (c) 2007 Ian Bicking and Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


Werkzeug
========
Copyright (c) 2011 by the Werkzeug Team, see AUTHORS for more details.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Tornado
=======
Copyright 2009 Facebook

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.


Flask
=====
Copyright (c) 2010 by Armin Ronacher and contributors. See AUTHORS
for more details.

Some rights reserved.

Redistribution and use in source and binary forms of the software as well
as documentation, with or without modification, are permitted provided
that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.

THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
4 changes: 4 additions & 0 deletions docs/api/extras.local_app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ Local App
=========
.. module:: webapp2_extras.local_app

.. note::
This module is deprecated. :class:`webapp2.WSGIApplication` is now
thread-safe by default when :mod:`webapp2_extras.local` is available.

.. autoclass:: WSGIApplication
:members: set_globals
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@
# built documents.
#
# The short X.Y version.
version = '1.8.1'
version = '2.0'
# The full version, including alpha/beta/rc tags.
release = '1.8.1'
release = '2.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,4 @@ package provides common utilities that integrate well with webapp2:
- Extra route classes -- to match subdomains and other conveniences
- Support for third party libraries: Jinja2, Mako and Google's ProtoRPC
- Support for threaded environments, so that you can use webapp2 outside of
App Engine
App Engine or in the upcoming App Engine Python 2.7 runtime
17 changes: 7 additions & 10 deletions docs/guide/app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ or in production checking the 'SERVER_SOFTWARE' environ variable::

Thread-safe application
-----------------------
:class:`webapp2_extras.local_app.WSGIApplication` is a special class to be used
in threaded environments, e.g., outside App Engine. It uses thread-locals to
store request and app references, instead of simple class attributes which are
fine for App Engine. See in the :ref:`tutorials.quickstart.nogae` tutorial a
complete explanation about how to use it.
By default, webapp2 is thread-safe when the module
:class:`webapp2_extras.local` is available. This means that it can be used
outside of App Engine or in the upcoming App Engine Python 2.7 runtime.
This also works in non-threaded environments such as App Engine Python 2.5.

See in the :ref:`tutorials.quickstart.nogae` tutorial an explanation on how
to use webapp2 outside of App Engine.


Running the app
Expand Down Expand Up @@ -261,8 +263,3 @@ access the app registry or configuration values::

app = webapp2.get_app()
config_value = app.config.get('my-config-key')

The application instance is stored as a class attribute, which is fine on App
Engine because there are no concurrent requests for the same Python interpreter
instance. For threaded environments, an application that supports threads must
be used as described in the :ref:`tutorials.quickstart.nogae` tutorial.
2 changes: 1 addition & 1 deletion docs/guide/extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ webapp2. It includes:
- Extra route classes -- to match subdomains and other conveniences
- Support for third party libraries: Jinja2, Mako and Google's ProtoRPC
- Support for threaded environments, so that you can use webapp2 outside of
App Engine
App Engine or in the upcoming App Engine Python 2.7 runtime

Some of these modules (:ref:`api.extras.i18n`, :ref:`api.extras.jinja2`,
:ref:`api.extras.mako` and :ref:`api.extras.sessions`) use configuration
Expand Down
6 changes: 1 addition & 5 deletions docs/guide/request.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ request. You can access it in ``request.app``.
Getting the current request
---------------------------
The active ``Request`` instance can be accessed during a request using the
function :func:`webapp2.get_request`. It is stored as a class attribute,
which is fine on App Engine because there are no concurrent requests for
the same Python interpreter instance. For threaded environments,
an application that supports threads must be used as described in the
:ref:`tutorials.quickstart.nogae` tutorial.
function :func:`webapp2.get_request`.


Learn more about WebOb
Expand Down
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ API Reference - webapp2_extras
api/extras.jinja2.rst
api/extras.json.rst
api/extras.local.rst
api/extras.local_app.rst
api/extras.mako.rst
api/extras.protorpc.rst
api/extras.routes.rst
Expand Down
9 changes: 2 additions & 7 deletions docs/tutorials/quickstart.nogae.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ framework, as it offers these features:

- It is independent of the App Engine SDK. If the SDK is not found, it sets
fallbacks to be used outside of GAE.
- It supports threaded environments, using the :ref:`api.extras.local_app`
module.
- It supports threaded environments, using the module :ref:`api.extras.local`.
- All webapp2_extras modules are designed to be thread-safe.
- It is compatible with ``WebOb`` 1.0 and superior, which fixes several bugs
found in the version bundled with the SDK (which is of course supported as
Expand Down Expand Up @@ -142,13 +141,12 @@ a handler to display a 'Hello, webapp2!' message. This will be our bootstrap
file::

import webapp2
from webapp2_extras import local_app

class HelloWebapp2(webapp2.RequestHandler):
def get(self):
self.response.write('Hello, webapp2!')

app = local_app.WSGIApplication([
app = webapp2.WSGIApplication([
('/', HelloWebapp2),
], debug=True)

Expand All @@ -159,9 +157,6 @@ file::
if __name__ == '__main__':
main()

Notice that we use ``local_app.WSGIApplication``. This is a special version
of the WSGI application that is thread-safe.


Test your app
-------------
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

setup(
name = 'webapp2',
version = '1.8.1',
version = '2.0',
license = 'Apache Software License',
url = 'http://www.tipfy.org/',
description = "Taking Google App Engine's webapp to the next level!",
Expand Down
2 changes: 1 addition & 1 deletion tests/extras_i18n_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setUp(self):
def test_translations_not_set(self):
# We release it here because it is set on setUp()
self.app.clear_globals()
self.assertRaises(AssertionError, i18n.gettext, 'foo')
self.assertRaises(RuntimeError, i18n.gettext, 'foo')

def test_gettext(self):
self.assertEqual(i18n.gettext('foo'), u'foo')
Expand Down
17 changes: 7 additions & 10 deletions tests/handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def get_redirect_url(handler, **kwargs):
class TestHandler(test_base.BaseTestCase):
def tearDown(self):
super(TestHandler, self).tearDown()
app.clear_globals()
app.error_handlers = {}

def test_200(self):
Expand Down Expand Up @@ -391,13 +390,13 @@ class Handler(webapp2.RequestHandler):
def get(self, *args, **kwargs):
pass

request = webapp2.Request.blank('http://localhost:80/')
request.route = webapp2.Route('')
request.route_args = tuple()
request.route_kwargs = {}
request.app = app
app.request = webapp2.WSGIApplication.request = request
handler = Handler(request, webapp2.Response())
req = webapp2.Request.blank('http://localhost:80/')
req.route = webapp2.Route('')
req.route_args = tuple()
req.route_kwargs = {}
req.app = app
app.set_globals(app=app, request=req)
handler = Handler(req, webapp2.Response())
handler.app = app

for func in (handler.uri_for,):
Expand Down Expand Up @@ -431,8 +430,6 @@ def get(self, *args, **kwargs):
self.assertEqual(func('route-test', _scheme='https', _full=False, year='2010', month='07', name='test'), 'https://localhost:80/2010/07/test')
self.assertEqual(func('route-test', _scheme='https', _fragment='my-anchor', year='2010', month='07', name='test'), 'https://localhost:80/2010/07/test#my-anchor')

app.request = None

def test_extra_request_methods(self):
allowed_methods_backup = app.allowed_methods
webdav_methods = ('VERSION-CONTROL', 'UNLOCK', 'PROPFIND')
Expand Down
2 changes: 2 additions & 0 deletions tests/misc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


class TestMiscellaneous(test_base.BaseTestCase):

def test_abort(self):
self.assertRaises(webob.exc.HTTPOk, webapp2.abort, 200)
self.assertRaises(webob.exc.HTTPCreated, webapp2.abort, 201)
Expand Down Expand Up @@ -118,6 +119,7 @@ def test_redirect_to(self):
req = webapp2.Request.blank('/')
req.app = app
app.set_globals(app=app, request=req)

rsp = webapp2.redirect_to('home', _code=301, _body='Weee')
self.assertEqual(rsp.status_int, 301)
self.assertEqual(rsp.body, 'Weee')
Expand Down
Loading

0 comments on commit fb715e0

Please sign in to comment.