quetz-mod_python-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rm...@pobox.com
Subject Patch to allow kwargs in publisher.py
Date Wed, 02 Oct 2002 07:01:53 GMT
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)


Mime
View raw message