Opened 7 years ago

Closed 6 years ago

#2446 enhancement closed fixed (fixed)

Python 2.4 has poor round-trip behaviour for xmlrpclib.DateTime; Twisted should work around.

Reported by: kgi Owned by:
Priority: normal Milestone:
Component: web Keywords: python2.4, xmlrpc, unicode
Cc: Branch: branches/xmlrpclib-datetime-hack-2446
(diff, github, buildbot, log)
Author: exarkun Launchpad Bug:

Description

Python 2.4 has a quirky xmlrpclib.py that means that re-serializing an un-serialized xmlrpclib.DateTime object results in it being stringified as Unicode, which causes Twisted's XML-RPC server to complain. Python 2.5 does not exhibit this behaviour. As a workaround, the xmlrpclib.py file from Python 2.4 can be replaced with the same file from Python 2.5 and recompiled.

This was discussed on the twisted-web mailing list on 7 Feb 2007 with the subject, "Confused about XML-RPC Unicode behaviour". JP suggested that Twisted can probably work around the bug.

The email contained some example code. This is reproduced below for convenience.

Example server that complains:

# Run with 'twistd -noy thisfile.py'
from twisted.web         import xmlrpc
from twisted.web         import server
from twisted.application import strports
from twisted.application import service

class XMLRPCDateServer ( xmlrpc.XMLRPC ):
    def xmlrpc_echoDate ( self, the_date ):
        print "Received date: ", repr ( the_date ), the_date.__class__
        return the_date

resource    = XMLRPCDateServer()
application = service.Application ( "XMLRPCDateServer" )
site        = server.Site ( resource )
service     = strports.service ( "tcp:8111", site )
service.setServiceParent ( application )

Example client:

# Run with 'python thisfile.py'
import xmlrpclib
from twisted.internet import reactor
from twisted.web      import xmlrpc

def doIt():
    proxy = xmlrpc.Proxy ( 'http://localhost:8111' )
    the_date = xmlrpclib.DateTime()
    print "Sending Date: ", the_date

    def _cb ( r ):
        print "Received reply: ", r
        reactor.stop()

    def _eb ( f ):
        print "Received error: ", f
        reactor.stop()

    d = proxy.callRemote ( "echoDate", the_date )
    d.addCallbacks ( _cb, _eb )

doIt()
reactor.run()

The error received on Python 2.4 is:

2007/02/09 15:02 +0300 [HTTPChannel,0,127.0.0.1] Received date:  <DateTime u'20070209T14:02:25' at -4872b754> xmlrpclib.DateTime
2007/02/09 15:02 +0300 [HTTPChannel,0,127.0.0.1] Unhandled error in Deferred:
2007/02/09 15:02 +0300 [HTTPChannel,0,127.0.0.1] Unhandled Error
        Traceback (most recent call last):
          File "/usr/lib/python2.4/site-packages/twisted/web/server.py", line 167, in render
            body = resrc.render(self)
          File "/usr/lib/python2.4/site-packages/twisted/web/xmlrpc.py", line 118, in render
            defer.maybeDeferred(function, *args).addErrback(
          File "/usr/lib/python2.4/site-packages/twisted/internet/defer.py", line 191, in addCallback
            callbackKeywords=kw)
          File "/usr/lib/python2.4/site-packages/twisted/internet/defer.py", line 182, in addCallbacks
            self._runCallbacks()
        --- <exception caught here> ---
          File "/usr/lib/python2.4/site-packages/twisted/internet/defer.py", line 317, in _runCallbacks
            self.result = callback(self.result, *args, **kw)
          File "/usr/lib/python2.4/site-packages/twisted/web/xmlrpc.py", line 136, in _cbRender
            request.write(s)
          File "/usr/lib/python2.4/site-packages/twisted/web/http.py", line 724, in write
            self.transport.write(data)
          File "/usr/lib/python2.4/site-packages/twisted/internet/abstract.py", line 173, in write
            raise TypeError("Data must not be unicode")
        exceptions.TypeError: Data must not be unicode

Change History (9)

comment:1 Changed 7 years ago by kgi

  • Component changed from core to web
  • Owner changed from glyph to jknight

Realised that bug is better assigned to 'web' than 'core'.

comment:2 Changed 6 years ago by exarkun

  • Author set to exarkun
  • Branch set to branches/xmlrpclib-datetime-hack-2446

(In [26014]) Branching to 'xmlrpclib-datetime-hack-2446'

comment:3 Changed 6 years ago by exarkun

  • Keywords review added
  • Owner jknight deleted

comment:4 Changed 6 years ago by glyph

  • Owner set to exarkun

The test seems to hang, rather than fail, when the fix is removed, although a traceback is logged. Can you fix that?

comment:5 Changed 6 years ago by glyph

  • Keywords review removed

"Interrupted System Call", said trac, and only applied some of my changes. Nice.

comment:6 Changed 6 years ago by exarkun

  • Keywords review added
  • Owner exarkun deleted

trac is super great!

I added some more error handling to the XML-RPC server in r26032, and another new test for that. Response failures should happen fast now.

comment:7 Changed 6 years ago by mwh

  • Keywords review removed
  • Owner set to exarkun

Looks fine. Please merge.

comment:8 Changed 6 years ago by exarkun

  • Resolution set to fixed
  • Status changed from new to closed

(In [26099]) Merge xmlrpclib-datetime-hack-2446

Author: exarkun
Reviewer: mwhudson
Fixes: #2446

Add error handling to the fault serialization code in twisted.web.xmlrpc.XMLRPC so
that responses are not left hanging indefinitely if there is an error serializing an
error report. Also insert a monkey patch for xmlrpclib.DateTime deserialization so
that these objects will correctly round-trip over XML-RPC on Python 2.3 and Python 2.4.

comment:9 Changed 3 years ago by <automation>

  • Owner exarkun deleted
Note: See TracTickets for help on using tickets.