[Twisted-Python] Catching exception on a Serial Connection

Jean-Paul Calderone exarkun at divmod.com
Thu Apr 30 08:14:14 EDT 2009


On Wed, 29 Apr 2009 18:09:08 -0500, "Nestor A. Diaz" <nestor at tiendalinux.com> wrote:
>Hello, i am newbie to twisted, i am trying to find a way to catch
>exceptions raised from a protocol over a serial line, like the
>twisted.protocol.gps.zodiac, i see the demo script, on the twisted
>website:
>http://twistedmatrix.com/projects/core/documentation/examples/gpsfix.py
>but i don't figure yet how to handle exceptions coming from the protocol
>since i didn't figure how to register a callback for an error, cause i
>haven't found a way to create a deferred object for a serial connection.

Protocol's aren't supposed to raise exceptions from their dataReceived
method.  If they do, then they're basically giving up control - both of
what happens to the exception and what happens to the connection (usually
it will be closed for them shortly afterwards).

If you'd like to handle these exceptions, you should handle them /before/
they escape from dataReceived.  For example:

    class Foo(Protocol):
        def dataReceived(self, bytes):
            try:
                print 'Received', int(bytes)
            except ValueError:
                print 'Received non-int', repr(bytes)

Jean-Paul




More information about the Twisted-Python mailing list