[Zope-Checkins]
SVN: Zope/branches/2.10/lib/python/AccessControl/requestmethod.
Merge default value fix from trunk
Martijn Pieters
mj at zopatista.com
Fri Apr 20 10:30:40 EDT 2007
Log message for revision 74260:
Merge default value fix from trunk
Changed:
U Zope/branches/2.10/lib/python/AccessControl/requestmethod.py
U Zope/branches/2.10/lib/python/AccessControl/requestmethod.txt
-=-
Modified: Zope/branches/2.10/lib/python/AccessControl/requestmethod.py
===================================================================
--- Zope/branches/2.10/lib/python/AccessControl/requestmethod.py 2007-04-20 14:23:43 UTC (rev 74259)
+++ Zope/branches/2.10/lib/python/AccessControl/requestmethod.py 2007-04-20 14:30:39 UTC (rev 74260)
@@ -15,14 +15,16 @@
from zExceptions import Forbidden
from ZPublisher.HTTPRequest import HTTPRequest
+_default = []
+
def _buildFacade(spec, docstring):
"""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.
"""
- args = inspect.formatargspec(formatvalue=lambda v: '=None', *spec)
+ args = inspect.formatargspec(formatvalue=lambda v: '=_default', *spec)
callargs = inspect.formatargspec(formatvalue=lambda v: '', *spec)
return 'def _facade%s:\n """%s"""\n return _curried%s' % (
args, docstring, callargs)
@@ -46,7 +48,6 @@
if len(args) > r_index:
request = args[r_index]
-
if isinstance(request, HTTPRequest):
if request.get('REQUEST_METHOD', 'GET').upper() != 'POST':
raise Forbidden('Request must be POST')
@@ -55,7 +56,7 @@
if defaults is not None:
args, kwparams = args[:arglen], args[arglen:]
for positional, (key, default) in zip(kwparams, defaults):
- if positional is None:
+ if positional is _default:
kw[key] = default
else:
kw[key] = positional
@@ -63,7 +64,7 @@
return callable(*args, **kw)
# 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
return facade_globs['_facade']
Modified: Zope/branches/2.10/lib/python/AccessControl/requestmethod.txt
===================================================================
--- Zope/branches/2.10/lib/python/AccessControl/requestmethod.txt 2007-04-20 14:23:43 UTC (rev 74259)
+++ Zope/branches/2.10/lib/python/AccessControl/requestmethod.txt 2007-04-20 14:30:39 UTC (rev 74260)
@@ -61,9 +61,9 @@
>>> import inspect
>>> mutabledefault = dict()
>>> @postonly
- ... def foo(bar, baz=mutabledefault, REQUEST=None, **kw):
- ... return bar, baz is mutabledefault, REQUEST
+ ... def foo(bar, baz=mutabledefault, egg=mutabledefault, REQUEST=None, **kw):
+ ... return bar, baz is mutabledefault, egg is None, REQUEST
>>> inspect.getargspec(foo)[:3]
- (['bar', 'baz', 'REQUEST'], None, 'kw')
- >>> foo('spam')
- ('spam', True, None)
+ (['bar', 'baz', 'egg', 'REQUEST'], None, 'kw')
+ >>> foo('spam', egg=None)
+ ('spam', True, True, None)
More information about the Zope-Checkins
mailing list