changeset 2547:c2ef5800253f

consolidate extra keyword args handling, no more **kw/_non_ascii_kw but rather just _kwargs
author Johannes Berg <johannes AT sipsolutions DOT net>
date Mon, 23 Jul 2007 19:38:16 +0200
parents 56dbfbde79fe
children 5db66a25f0ba
files MoinMoin/_tests/test_wikiutil.py MoinMoin/wikiutil.py
diffstat 2 files changed, 25 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/test_wikiutil.py	Mon Jul 23 19:37:36 2007 +0200
+++ b/MoinMoin/_tests/test_wikiutil.py	Mon Jul 23 19:38:16 2007 +0200
@@ -517,9 +517,8 @@
     def _test_trailing(self, a, _trailing_args=[]):
         assert _trailing_args == [u'a']
 
-    def _test_arbitrary_kw(self, expect, _non_ascii_kwargs={}, **kw):
-        assert _non_ascii_kwargs == expect
-        assert kw == {'test': u'x'}
+    def _test_arbitrary_kw(self, expect, _kwargs={}):
+        assert _kwargs == expect
 
     def testInvoke(self):
         ief = wikiutil.invoke_extension_function
@@ -568,14 +567,14 @@
                        self._test_invoke_float_None, u'i=""')
         ief(self.request, self._test_trailing, u'a=7, a')
         ief(self.request, self._test_arbitrary_kw, u'test=x, \xc3=test',
-            [{u'\xc3': 'test'}])
+            [{u'\xc3': 'test', 'test': u'x'}])
         ief(self.request, self._test_arbitrary_kw, u'test=x, "\xc3"=test',
-            [{u'\xc3': 'test'}])
+            [{u'\xc3': 'test', 'test': u'x'}])
         ief(self.request, self._test_arbitrary_kw, u'test=x, "7 \xc3"=test',
-            [{u'7 \xc3': 'test'}])
+            [{u'7 \xc3': 'test', 'test': u'x'}])
         ief(self.request, self._test_arbitrary_kw, u'test=x, 7 \xc3=test',
-            [{u'7 \xc3': 'test'}])
+            [{u'7 \xc3': 'test', 'test': u'x'}])
         ief(self.request, self._test_arbitrary_kw, u'7 \xc3=test, test= x ',
-            [{u'7 \xc3': 'test'}])
+            [{u'7 \xc3': 'test', 'test': u'x'}])
 
 coverage_modules = ['MoinMoin.wikiutil']
--- a/MoinMoin/wikiutil.py	Mon Jul 23 19:37:36 2007 +0200
+++ b/MoinMoin/wikiutil.py	Mon Jul 23 19:38:16 2007 +0200
@@ -1535,18 +1535,19 @@
 
     _ = request.getText
 
+    kwargs = {}
+    kwargs_to_pass = {}
+
     if args:
         assert isinstance(args, unicode)
 
         positional, keyword, trailing = parse_quoted_separated(args)
 
-        kwargs = {}
-        nonascii = {}
         for kw in keyword:
             try:
                 kwargs[str(kw)] = keyword[kw]
             except UnicodeEncodeError:
-                nonascii[kw] = keyword[kw]
+                kwargs_to_pass[kw] = keyword[kw]
 
         # add trailing args as keyword argument if present,
         # otherwise remove if the user entered some
@@ -1556,17 +1557,8 @@
         elif '_trailing_args' in kwargs:
             del kwargs['_trailing_args']
 
-        # add nonascii args as keyword argument if present,
-        # otherwise remove if the user entered some
-        # (so macros don't get a string where they expect a list)
-        if nonascii:
-            kwargs['_non_ascii_kwargs'] = nonascii
-        elif '_non_ascii_kwargs' in kwargs:
-            del kwargs['_non_ascii_kwargs']
-
     else:
         positional = []
-        kwargs = {}
 
     argnames, varargs, varkw, defaultlist = getargspec(function)
     # self is implicit!
@@ -1586,14 +1578,14 @@
     defaults = {}
     # reverse to be able to pop() things off
     positional.reverse()
-    allow_non_ascii_kw = False
+    allow_kwargs = False
     allow_trailing = False
     # convert all arguments to keyword arguments,
     # fill all arguments that weren't given with None
     for idx in range(argc):
         argname = argnames[idx]
-        if argname == '_non_ascii_kwargs':
-            allow_non_ascii_kw = True
+        if argname == '_kwargs':
+            allow_kwargs = True
             continue
         if argname == '_trailing_args':
             allow_trailing = True
@@ -1610,15 +1602,9 @@
     if '_trailing_args' in kwargs and not allow_trailing:
         raise ValueError(_('Cannot have arguments without name following'
                            ' named arguments'))
-    if '_non_ascii_kwargs' in kwargs and not allow_non_ascii_kw:
-        raise ValueError(_(u'No argument named "%s"') % (
-            kwargs['_non_ascii_kwargs'].keys()[0]))
-
     # type-convert all keyword arguments to the type
     # that the default value indicates
-    for argname in kwargs:
-        if not varkw and not argname in argnames:
-            raise ValueError(_('No argument named "%s"') % argname)
+    for argname in kwargs.keys()[:]:
         if argname in defaults:
             # the value of 'argname' from kwargs will be put into the
             # macro's 'argname' argument, so convert that giving the
@@ -1626,6 +1612,16 @@
             # went wrong (if it does)
             kwargs[argname] = _convert_arg(request, kwargs[argname],
                                            defaults[argname], argname)
+        if not argname in argnames:
+            # move argname into _kwargs parameter
+            kwargs_to_pass[argname] = kwargs[argname]
+            del kwargs[argname]
+
+    if kwargs_to_pass:
+        kwargs['_kwargs'] = kwargs_to_pass
+        if not allow_kwargs:
+            raise ValueError(_(u'No argument named "%s"') % (
+                kwargs_to_pass.keys()[0]))
 
     return function(*fixed_args, **kwargs)