[Freeipa-devel] argument passing

John Dennis jdennis at redhat.com
Wed Aug 22 18:02:29 UTC 2007


O.K. I've looked at this a bit and I think the fundamental problem is a
concept mismatch.

The first thing to understand is that xmlrpc only supports positional
arguments and has no notion of a function signature from which to derive
parameter information.

When generating the call the arguments are passed in the order they are
seen. When receiving the call the arguments are unpacked in the same
order and then passed in that same order to handler function.

This exactly maps to a python function which either contains no keyword
arguments (kwds) or whose keyword arguments preserve their position in
the argument list.

The above rules means in xmlrpc you can have a restricted form of
optional arguments. You can pass the first 0..n-1 arguments in order for
a function expecting to receive n arguments. On the receiving end the
invoked function will be passed the arguments in order, if its less than
n then the receiving function must provide default values for the
remaining positional arguments.

This means any function you register in the xmlrpc server to handle a
method call must not rely on pythons keyword passing scheme which
permits out of order parameters. Note, the out of order keyword
arguments are evaluated in python *before* the call is made and then
converted to positional arguments to make the call. But in an xmlrpc
server this step is omitted because the caller is remote thus python
cannot massage the out of order parameters.

So your options are:

1) Never use out of order keyword arguments in a function definition
which will be registered with an xmlrpc server as a method handler.

But you say, hey, that really restricts how I might want to call that
function directly in my own python code. If that's the case then write a
"wrapper" which converts parameters to positional arguments. Expose the
wrapper to your python code so folks can call it locally in a friendly
manner, the real work is always done in function you register with the
rpc server.

2) Have every programmer make sure every function they might call via
xmlrpc supplies an empty positional argument if any optional arguments
follow it. This is kinda what you were doing. Not very appealing or
robust. But wait, you can still do the wrapper approach with a special
value substituted for a omitted positional parameter. The receiving code
has to share the special value convention.

3) Use python introspection and possibly function 'decorators'. It's
possible in python to query the formal parameter list of any defined
function. Once you know the formal parameter list of the rpc call you
can massage your incoming parameters into the expected rpc parameter
list, this is most easily done in a 'decorator'. I've implemented this
in my own rpc code, not too hard, but extra implementation work you
might want to avoid.

However, bottom line, the really important point is that on the
receiving end you cannot have out of order optional parameters for any
method registered as an rpc method handler.

HTH,
-- 
John Dennis <jdennis at redhat.com>





More information about the Freeipa-devel mailing list