[Twisted-web] XML-RPC and None

kgi iacovou at gmail.com
Fri Sep 9 15:01:11 MDT 2005


On Wednesday 15 June 2005 23:59, Tommi Virtanen wrote:
> Matt Feifarek wrote:
> > We'd like to pass None through the rpc connection on occasion. There's
> > a flag in the standard lib to allow this to serialize ( send
> > allow_none=True to the xmlrpclib.Server() )
> >
> > We're getting this error from the twisted resource when we try to
> > serialize 'None':
> >
> > Fault: <Fault 8002: "can't serialize output">
> >
> > I know that the twisted resources uses the stock xmlrpclib, but how
> > can we tell it to allow None to serialize the twisted way?
>
> With the current twisted.web.xmlrpc, you can't.
> (You'd need to patch that to optionally give an allow_none
> flag to everything using dumps/loads.)
>
> Why do people want to break the perfectly nice standard, anyway?
> Return a tuple (False, ) for None and (True, stuff) for stuff,
> or something..

(I know I'm a little late to this discussion, but I thought I'd reply to the 
thread for those who keep the mailing list for reference, like I do).

Whereas Tommi is strictly correct when he says, "Why do people want to break 
the perfectly nice standard?" in that it's generally a Good Thing to stick to 
standards, it's not always as simple as he implies: not all lookups are 
simple ones that return nice simple scalar values that can be wrapped up in 
two-tuples.

Personally, I think the absence of a "None" concept from the XML-RPC standard 
is astounding, and that the decision to adhere strictly to the standard or 
not should be up to the developer, since only he can make the decision as to 
whether the allow_none hack is worse than alternative workarounds.

One way would be to write wrappers that recurse through the data structure 
just before serialization and after deserialization and use magical strings 
like "NULLVALUE") but this is a really nasty hack and affects performance, 
since you're effectively walking the tree twice.

I'm moving some multithreaded xmlrpclib servers to Twisted; some of them 
return structured data that ultimately comes from a database. Both the 
*existence* of the column and the *absence* of a defined value are important 
to the client application.

Since the original servers used allow_none, I'd like that to be allowed via 
Twisted's xmlrpc.py. The trivial patch below is against SVN head. Note that 
rather than adding a construction argument to xmlrpc.XMLRPC, it simply adds a 
class attribute which defaults to False (this means that users don't need to 
subclass xmlrpc.XMLRPC):

class MyServer ( xmlrpc.XMLRPC ):
    allow_none = True
    def xmlrpc_Foo ( self, ... ):
        ...

Note that the modification to xmlrpclib.dumps when serializing the Fault is 
technically not needed; it's just there for completeness, just in case a hook 
for user-created Fault objects is added in the future.

Could this patch (or something like it) be committed to SVN, please?

Thanks,

Ricky



Index: xmlrpc.py
===================================================================
--- xmlrpc.py   (revision 14375)
+++ xmlrpc.py   (working copy)
@@ -93,6 +93,8 @@
     isLeaf = 1
     separator = '.'

+    allow_none = False
+
     def __init__(self):
         resource.Resource.__init__(self)
         self.subHandlers = {}
@@ -128,10 +130,12 @@
         if not isinstance(result, Fault):
             result = (result,)
         try:
-            s = xmlrpclib.dumps(result, methodresponse=1)
+            s = xmlrpclib.dumps(result, methodresponse=1,
+                                allow_none = self.allow_none)
         except:
             f = Fault(self.FAILURE, "can't serialize output")
-            s = xmlrpclib.dumps(f, methodresponse=1)
+            s = xmlrpclib.dumps(f, methodresponse=1,
+                                allow_none = self.allow_none )
         request.setHeader("content-length", str(len(s)))
         request.write(s)
         request.finish()



More information about the Twisted-web mailing list