[mod_python] Patch to allow kwargs in publisher.py

rmunn at pobox.com rmunn at pobox.com
Wed Oct 2 01:27:51 EST 2002


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 at 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)
 



More information about the Mod_python mailing list