From 3052d0b41bd06314b0b1f1c78e9977b8174f637a Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 1 Nov 2009 00:34:50 +0000 Subject: - The ``repoze.bfg.functional`` module was renamed to ``repoze.bfg.compat``. --- CHANGES.txt | 3 + repoze/bfg/compat.py | 137 ++++++++++++++++++++++++++++++++++++++++ repoze/bfg/functional.py | 119 ---------------------------------- repoze/bfg/renderers.py | 6 +- repoze/bfg/tests/test_compat.py | 10 +++ repoze/bfg/tests/test_view.py | 8 --- repoze/bfg/view.py | 10 +-- repoze/bfg/wsgi.py | 7 +- 8 files changed, 153 insertions(+), 147 deletions(-) create mode 100644 repoze/bfg/compat.py delete mode 100644 repoze/bfg/functional.py create mode 100644 repoze/bfg/tests/test_compat.py diff --git a/CHANGES.txt b/CHANGES.txt index b7a715ebd..27467dcc7 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -83,6 +83,9 @@ Internal request factory (the previous return value of the now-missing ``repoze.bfg.request.create_route_request_factory``. +- The ``repoze.bfg.functional`` module was renamed to + ``repoze.bfg.compat``. + Backwards Incompatibilities --------------------------- diff --git a/repoze/bfg/compat.py b/repoze/bfg/compat.py new file mode 100644 index 000000000..9eda9d4f0 --- /dev/null +++ b/repoze/bfg/compat.py @@ -0,0 +1,137 @@ +# Some code in this file was lifted wholesale from Django +# (see +# http://code.djangoproject.com/browser/django/trunk/LICENSE for +# license text; BSD-like) + +# License for code in this file that was taken from Python 2.5. + +# PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 +# -------------------------------------------- +# +# 1. This LICENSE AGREEMENT is between the Python Software Foundation +# ("PSF"), and the Individual or Organization ("Licensee") accessing and +# otherwise using this software ("Python") in source or binary form and +# its associated documentation. +# +# 2. Subject to the terms and conditions of this License Agreement, PSF +# hereby grants Licensee a nonexclusive, royalty-free, world-wide +# license to reproduce, analyze, test, perform and/or display publicly, +# prepare derivative works, distribute, and otherwise use Python +# alone or in any derivative version, provided, however, that PSF's +# License Agreement and PSF's notice of copyright, i.e., "Copyright (c) +# 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation; +# All Rights Reserved" are retained in Python alone or in any derivative +# version prepared by Licensee. +# +# 3. In the event Licensee prepares a derivative work that is based on +# or incorporates Python or any part thereof, and wants to make +# the derivative work available to others as provided herein, then +# Licensee hereby agrees to include in any such work a brief summary of +# the changes made to Python. +# +# 4. PSF is making Python available to Licensee on an "AS IS" +# basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR +# IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND +# DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS +# FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT +# INFRINGE ANY THIRD PARTY RIGHTS. +# +# 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON +# FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS +# A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, +# OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. +# +# 6. This License Agreement will automatically terminate upon a material +# breach of its terms and conditions. +# +# 7. Nothing in this License Agreement shall be deemed to create any +# relationship of agency, partnership, or joint venture between PSF and +# Licensee. This License Agreement does not grant permission to use PSF +# trademarks or trade name in a trademark sense to endorse or promote +# products or services of Licensee, or any third party. +# +# 8. By copying, installing or otherwise using Python, Licensee +# agrees to be bound by the terms and conditions of this License +# Agreement. + + +try: # pragma: no cover + from functools import wraps +except ImportError: #pragma no cover + # < 2.5 + def curry(_curried_func, *args, **kwargs): + def _curried(*moreargs, **morekwargs): + return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) + return _curried + + ### Begin from Python 2.5 functools.py ################################### + # Summary of changes made to the Python 2.5 code below: + # * swapped ``partial`` for ``curry`` to maintain backwards-compatibility + # in Django. + # * Wrapped the ``setattr`` call in ``update_wrapper`` with a try-except + # block to make it compatible with Python 2.3, which doesn't allow + # assigning to ``__name__``. + + # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software + # Foundation. + # All Rights Reserved. + ########################################################################## + + # update_wrapper() and wraps() are tools to help write + # wrapper functions that can handle naive introspection + + WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__') + WRAPPER_UPDATES = ('__dict__',) + def update_wrapper(wrapper, + wrapped, + assigned = WRAPPER_ASSIGNMENTS, + updated = WRAPPER_UPDATES): + """Update a wrapper function to look like the wrapped function + + wrapper is the function to be updated + wrapped is the original function + assigned is a tuple naming the attributes assigned directly + from the wrapped function to the wrapper function (defaults to + functools.WRAPPER_ASSIGNMENTS) + updated is a tuple naming the attributes off the wrapper that + are updated with the corresponding attribute from the wrapped + function (defaults to functools.WRAPPER_UPDATES) + """ + for attr in assigned: + setattr(wrapper, attr, getattr(wrapped, attr)) + + for attr in updated: + getattr(wrapper, attr).update(getattr(wrapped, attr)) + # Return the wrapper so this can be used as a decorator via curry() + return wrapper + + def wraps(wrapped, + assigned = WRAPPER_ASSIGNMENTS, + updated = WRAPPER_UPDATES): + """Decorator factory to apply update_wrapper() to a wrapper function + + Returns a decorator that invokes update_wrapper() with the decorated + function as the wrapper argument and the arguments to wraps() as the + remaining arguments. Default arguments are as for update_wrapper(). + This is a convenience function to simplify applying curry() to + update_wrapper(). + """ + return curry(update_wrapper, wrapped=wrapped, + assigned=assigned, updated=updated) + +### End from Python 2.5 functools.py ########################################## + +try: + all = all +except NameError: # pragma: no cover + def all(iterable): + for element in iterable: + if not element: + return False + return True + +try: + import json +except ImportError: + import simplejson as json + diff --git a/repoze/bfg/functional.py b/repoze/bfg/functional.py deleted file mode 100644 index 351bcc4b6..000000000 --- a/repoze/bfg/functional.py +++ /dev/null @@ -1,119 +0,0 @@ -# This file was lifted wholesale from Django -# (see http://code.djangoproject.com/browser/django/trunk/LICENSE for -# license text; BSD-like) - -# License for code in this file that was taken from Python 2.5. - -# PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 -# -------------------------------------------- -# -# 1. This LICENSE AGREEMENT is between the Python Software Foundation -# ("PSF"), and the Individual or Organization ("Licensee") accessing and -# otherwise using this software ("Python") in source or binary form and -# its associated documentation. -# -# 2. Subject to the terms and conditions of this License Agreement, PSF -# hereby grants Licensee a nonexclusive, royalty-free, world-wide -# license to reproduce, analyze, test, perform and/or display publicly, -# prepare derivative works, distribute, and otherwise use Python -# alone or in any derivative version, provided, however, that PSF's -# License Agreement and PSF's notice of copyright, i.e., "Copyright (c) -# 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation; -# All Rights Reserved" are retained in Python alone or in any derivative -# version prepared by Licensee. -# -# 3. In the event Licensee prepares a derivative work that is based on -# or incorporates Python or any part thereof, and wants to make -# the derivative work available to others as provided herein, then -# Licensee hereby agrees to include in any such work a brief summary of -# the changes made to Python. -# -# 4. PSF is making Python available to Licensee on an "AS IS" -# basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR -# IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND -# DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS -# FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT -# INFRINGE ANY THIRD PARTY RIGHTS. -# -# 5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON -# FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS -# A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON, -# OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. -# -# 6. This License Agreement will automatically terminate upon a material -# breach of its terms and conditions. -# -# 7. Nothing in this License Agreement shall be deemed to create any -# relationship of agency, partnership, or joint venture between PSF and -# Licensee. This License Agreement does not grant permission to use PSF -# trademarks or trade name in a trademark sense to endorse or promote -# products or services of Licensee, or any third party. -# -# 8. By copying, installing or otherwise using Python, Licensee -# agrees to be bound by the terms and conditions of this License -# Agreement. - - -def curry(_curried_func, *args, **kwargs): - def _curried(*moreargs, **morekwargs): - return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) - return _curried - -### Begin from Python 2.5 functools.py ######################################## - -# Summary of changes made to the Python 2.5 code below: -# * swapped ``partial`` for ``curry`` to maintain backwards-compatibility -# in Django. -# * Wrapped the ``setattr`` call in ``update_wrapper`` with a try-except -# block to make it compatible with Python 2.3, which doesn't allow -# assigning to ``__name__``. - -# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation. -# All Rights Reserved. - -############################################################################### - -# update_wrapper() and wraps() are tools to help write -# wrapper functions that can handle naive introspection - -WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__') -WRAPPER_UPDATES = ('__dict__',) -def update_wrapper(wrapper, - wrapped, - assigned = WRAPPER_ASSIGNMENTS, - updated = WRAPPER_UPDATES): - """Update a wrapper function to look like the wrapped function - - wrapper is the function to be updated - wrapped is the original function - assigned is a tuple naming the attributes assigned directly - from the wrapped function to the wrapper function (defaults to - functools.WRAPPER_ASSIGNMENTS) - updated is a tuple naming the attributes off the wrapper that - are updated with the corresponding attribute from the wrapped - function (defaults to functools.WRAPPER_UPDATES) - """ - for attr in assigned: - setattr(wrapper, attr, getattr(wrapped, attr)) - - for attr in updated: - getattr(wrapper, attr).update(getattr(wrapped, attr)) - # Return the wrapper so this can be used as a decorator via curry() - return wrapper - -def wraps(wrapped, - assigned = WRAPPER_ASSIGNMENTS, - updated = WRAPPER_UPDATES): - """Decorator factory to apply update_wrapper() to a wrapper function - - Returns a decorator that invokes update_wrapper() with the decorated - function as the wrapper argument and the arguments to wraps() as the - remaining arguments. Default arguments are as for update_wrapper(). - This is a convenience function to simplify applying curry() to - update_wrapper(). - """ - return curry(update_wrapper, wrapped=wrapped, - assigned=assigned, updated=updated) - -### End from Python 2.5 functools.py ########################################## - diff --git a/repoze/bfg/renderers.py b/repoze/bfg/renderers.py index 1e7babcbc..212c02dcf 100644 --- a/repoze/bfg/renderers.py +++ b/repoze/bfg/renderers.py @@ -7,15 +7,11 @@ from zope.component import queryUtility from repoze.bfg.interfaces import IRendererFactory from repoze.bfg.interfaces import ITemplateRenderer +from repoze.bfg.compat import json from repoze.bfg.path import caller_package from repoze.bfg.resource import resource_spec from repoze.bfg.settings import get_settings -try: - import json -except ImportError: - import simplejson as json - # concrete renderer factory implementations def json_renderer_factory(name): diff --git a/repoze/bfg/tests/test_compat.py b/repoze/bfg/tests/test_compat.py new file mode 100644 index 000000000..cbee29654 --- /dev/null +++ b/repoze/bfg/tests/test_compat.py @@ -0,0 +1,10 @@ +import unittest + +class TestAll(unittest.TestCase): + def test_it(self): + from repoze.bfg.compat import all + self.assertEqual(all([True, True]), True) + self.assertEqual(all([False, False]), False) + self.assertEqual(all([False, True]), False) + + diff --git a/repoze/bfg/tests/test_view.py b/repoze/bfg/tests/test_view.py index 14923f5b8..80179e247 100644 --- a/repoze/bfg/tests/test_view.py +++ b/repoze/bfg/tests/test_view.py @@ -1613,14 +1613,6 @@ class TestDeriveView(unittest.TestCase): inner_view, viewname='inner', wrapper_viewname='owrap') result = self.assertRaises(ValueError, wrapped, None, request) -class TestAll(unittest.TestCase): - def test_it(self): - from repoze.bfg.view import all - self.assertEqual(all([True, True]), True) - self.assertEqual(all([False, False]), False) - self.assertEqual(all([False, True]), False) - - class DummyContext: pass diff --git a/repoze/bfg/view.py b/repoze/bfg/view.py index 09a434f7e..734bb7f0a 100644 --- a/repoze/bfg/view.py +++ b/repoze/bfg/view.py @@ -35,6 +35,7 @@ from repoze.bfg.interfaces import IResponseFactory from repoze.bfg.interfaces import IRoutesMapper from repoze.bfg.interfaces import IView +from repoze.bfg.compat import all from repoze.bfg.exceptions import NotFound from repoze.bfg.exceptions import Forbidden from repoze.bfg.path import caller_package @@ -46,15 +47,6 @@ from repoze.bfg.static import PackageURLParser # b/c imports from repoze.bfg.security import view_execution_permitted -try: - all = all -except NameError: # pragma: no cover - def all(iterable): - for element in iterable: - if not element: - return False - return True - deprecated('view_execution_permitted', "('from repoze.bfg.view import view_execution_permitted' was " "deprecated as of repoze.bfg 1.0; instead use 'from " diff --git a/repoze/bfg/wsgi.py b/repoze/bfg/wsgi.py index cb68567ba..10d067901 100644 --- a/repoze/bfg/wsgi.py +++ b/repoze/bfg/wsgi.py @@ -1,9 +1,4 @@ -try: - from functools import wraps -except ImportError: #pragma NO COVERAGE - # < 2.5 - from repoze.bfg.functional import wraps #pragma NO COVERAGE - +from repoze.bfg.compat import wraps from repoze.bfg.traversal import quote_path_segment def wsgiapp(wrapped): -- cgit v1.2.3