You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-dev@quetz.apache.org by rm...@pobox.com on 2002/10/02 09:01:53 UTC

Patch to allow kwargs in publisher.py

I sent this to the mod_python@modpython.org list, then realized that the
python-dev list is probably a better one to send this to.

While playing around with my brand new install of mod_python 3.0.0 beta
2, I discovered that publisher.py does not handle the **kwargs syntax in
functions it's dispatching to. Apparently I'm not the only one who wants
that feature added:

http://www.modpython.org/pipermail/mod_python/2002-June/002171.html

It seems like the Right Thing for publisher.py's handler() func to do
would be to check whether the function accepts the **kwargs syntax
before stripping out unexpected args. Presumably if the user asked for
**kwargs-style args to be passed, he is expecting to receive all the
GET/POST arguments in the **kwargs dictionary. It would be intuitive
(it's what I was expecting to happen, actually, and I was surprised that
it didn't) and useful. And since it hadn't been done yet, I went ahead
and did it -- it was surprisingly simple. Here's a patch. It seems to
work fine for me -- I can't say I've given it a foolproof amount of
testing, but every test I threw at it seemed to do what I expected.

-- 
Robin Munn
rmunn@pobox.com


--- lib/python/mod_python/publisher.py	2002-09-12 13:24:05.000000000 -0500
+++ lib/python/mod_python/publisher.py.new	2002-10-02 01:07:46.000000000 -0500
@@ -180,10 +180,17 @@
             fc = object.im_func.func_code
             expected = fc.co_varnames[1:fc.co_argcount]
 
-        # remove unexpected args
-        for name in args.keys():
-            if name not in expected:
-                del args[name]
+        # does it want keyword args?
+        # co_flags & 0x04 is set if a function wants *args syntax
+        # co_flags & 0x08 is set if a function wants **kwargs syntax
+        if fc.co_flags & 0x08:
+            # don't strip unexpected args if function wants **kwargs
+            pass
+        else:
+            # remove unexpected args
+            for name in args.keys():
+                if name not in expected:
+                    del args[name]
 
         result = apply(object, (), args)


Re: Patch to allow kwargs in publisher.py

Posted by rm...@pobox.com.
On Thu, Oct 03, 2002 at 04:56:19PM -0400, Gregory (Grisha) Trubetskoy wrote:
> 
> Thanks for the patch. I'm not sure I like the "pass", why not simply do
> this:
> 
>       if not (fc.co_flags & 0x08):
>           for name in args.keys():
>               if name not in expected:
>                   del args[name]

A matter of coding style, I guess; I've always preferred to write "if
(condition): pass else: (do something)" than "if not (condition): (do
something)" when the condition is complicated enough -- and, in fact,
that's become my default way of doing negative-condition ifs even when
(as in this case) the condition isn't too complicated. What you suggest
should work fine.

Note that once you apply this patch, you'll run into the util.py bug
that I just submitted another patch for. That patch was invisible until
now because None was, of course, not in expected and so args[None] would
be removed.

> 
> 
> On Wed, 2 Oct 2002 rmunn@pobox.com wrote:
> 
> > @@ -180,10 +180,17 @@ fc = object.im_func.func_code expected =
> > fc.co_varnames[1:fc.co_argcount]
> >
> > -        # remove unexpected args -        for name in args.keys():
> > -            if name not in expected: -                del
> > args[name] +        # does it want keyword args?  +        #
> > co_flags & 0x04 is set if a function wants *args syntax +        #
> > co_flags & 0x08 is set if a function wants **kwargs syntax +
> > if fc.co_flags & 0x08: +            # don't strip unexpected args if
> > function wants **kwargs +            pass +        else: +
> > # remove unexpected args +            for name in args.keys(): +
> > if name not in expected: +                    del args[name]
> >
> >          result = apply(object, (), args)
> >
> 
> 

-- 
Robin Munn
rmunn@pobox.com

Re: Patch to allow kwargs in publisher.py

Posted by "Gregory (Grisha) Trubetskoy" <gr...@apache.org>.
Thanks for the patch. I'm not sure I like the "pass", why not simply do
this:

      if not (fc.co_flags & 0x08):
          for name in args.keys():
              if name not in expected:
                  del args[name]


On Wed, 2 Oct 2002 rmunn@pobox.com wrote:

> @@ -180,10 +180,17 @@
>              fc = object.im_func.func_code
>              expected = fc.co_varnames[1:fc.co_argcount]
>
> -        # remove unexpected args
> -        for name in args.keys():
> -            if name not in expected:
> -                del args[name]
> +        # does it want keyword args?
> +        # co_flags & 0x04 is set if a function wants *args syntax
> +        # co_flags & 0x08 is set if a function wants **kwargs syntax
> +        if fc.co_flags & 0x08:
> +            # don't strip unexpected args if function wants **kwargs
> +            pass
> +        else:
> +            # remove unexpected args
> +            for name in args.keys():
> +                if name not in expected:
> +                    del args[name]
>
>          result = apply(object, (), args)
>