Commit 01666dcd authored by Martijn Pieters's avatar Martijn Pieters

Fix potential bug in requestmethod where you cannot specify None for a keyword...

Fix potential bug in requestmethod where you cannot specify None for a keyword argument where None is not the default
parent 0fb0e234
...@@ -15,14 +15,16 @@ import inspect ...@@ -15,14 +15,16 @@ import inspect
from zExceptions import Forbidden from zExceptions import Forbidden
from zope.publisher.interfaces.browser import IBrowserRequest from zope.publisher.interfaces.browser import IBrowserRequest
_default = []
def _buildFacade(spec, docstring): def _buildFacade(spec, docstring):
"""Build a facade function, matching the decorated method in signature. """Build a facade function, matching the decorated method in signature.
Note that defaults are replaced by None, and _curried will reconstruct Note that defaults are replaced by _default, and _curried will reconstruct
these to preserve mutable defaults. these to preserve mutable defaults.
""" """
args = inspect.formatargspec(formatvalue=lambda v: '=None', *spec) args = inspect.formatargspec(formatvalue=lambda v: '=_default', *spec)
callargs = inspect.formatargspec(formatvalue=lambda v: '', *spec) callargs = inspect.formatargspec(formatvalue=lambda v: '', *spec)
return 'def _facade%s:\n """%s"""\n return _curried%s' % ( return 'def _facade%s:\n """%s"""\n return _curried%s' % (
args, docstring, callargs) args, docstring, callargs)
...@@ -60,7 +62,7 @@ def requestmethod(*methods): ...@@ -60,7 +62,7 @@ def requestmethod(*methods):
if defaults is not None: if defaults is not None:
args, kwparams = args[:arglen], args[arglen:] args, kwparams = args[:arglen], args[arglen:]
for positional, (key, default) in zip(kwparams, defaults): for positional, (key, default) in zip(kwparams, defaults):
if positional is None: if positional is _default:
kw[key] = default kw[key] = default
else: else:
kw[key] = positional kw[key] = positional
...@@ -68,7 +70,7 @@ def requestmethod(*methods): ...@@ -68,7 +70,7 @@ def requestmethod(*methods):
return callable(*args, **kw) return callable(*args, **kw)
# Build a facade, with a reference to our locally-scoped _curried # Build a facade, with a reference to our locally-scoped _curried
facade_globs = dict(_curried=_curried) facade_globs = dict(_curried=_curried, _default=_default)
exec _buildFacade(spec, callable.__doc__) in facade_globs exec _buildFacade(spec, callable.__doc__) in facade_globs
return facade_globs['_facade'] return facade_globs['_facade']
......
...@@ -57,12 +57,12 @@ original closely, and keyword parameter defaults must be preserved:: ...@@ -57,12 +57,12 @@ original closely, and keyword parameter defaults must be preserved::
>>> import inspect >>> import inspect
>>> mutabledefault = dict() >>> mutabledefault = dict()
>>> @requestmethod('POST') >>> @requestmethod('POST')
... def foo(bar, baz=mutabledefault, REQUEST=None, **kw): ... def foo(bar, baz=mutabledefault, egg=mutabledefault, REQUEST=None, **kw):
... return bar, baz is mutabledefault, REQUEST ... return bar, baz is mutabledefault, egg is None, REQUEST
>>> inspect.getargspec(foo)[:3] >>> inspect.getargspec(foo)[:3]
(['bar', 'baz', 'REQUEST'], None, 'kw') (['bar', 'baz', 'egg', 'REQUEST'], None, 'kw')
>>> foo('spam') >>> foo('spam', egg=None)
('spam', True, None) ('spam', True, True, None)
The requestmethod decorator factory can be used for any request method, simply The requestmethod decorator factory can be used for any request method, simply
pass in the desired request method:: pass in the desired request method::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment