Skip to content

Commit

Permalink
Merge banana-unsupported-type-exception-7663
Browse files Browse the repository at this point in the history
Author: wolfgang61, exarkun
Reviewer: exarkun
Fixes: twisted#7663

Change Banana's unsupported type exception message to include the type of the
unsupported object.


git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@43213 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
  • Loading branch information
exarkun committed Oct 5, 2014
1 parent 3a0f978 commit fbad806
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
12 changes: 3 additions & 9 deletions docs/core/specifications/banana.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ Banana Protocol Specifications
Introduction
------------




Banana is an efficient, extendable protocol for sending and receiving s-expressions.
A s-expression in this context is a list composed of byte strings, integers,
large integers, floats and/or s-expressions.




A s-expression in this context is a list composed of bytes, integers, large integers, floats and/or s-expressions.
Unicode is not supported (but can be encoded to and decoded from bytes on the way into and out of Banana).
Unsupported types must be converted into a supported type before sending them with Banana.


Banana Encodings
Expand Down
14 changes: 13 additions & 1 deletion twisted/spread/banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from twisted.internet import protocol
from twisted.persisted import styles
from twisted.python import log
from twisted.python.reflect import fullyQualifiedName

class BananaError(Exception):
pass
Expand Down Expand Up @@ -282,6 +283,16 @@ def __init__(self, isClient=1):
self.isClient = isClient

def sendEncoded(self, obj):
"""
Send the encoded representation of the given object:
@param obj: An object to encode and send.
@raise BananaError: If the given object is not an instance of one of
the types supported by Banana.
@return: C{None}
"""
io = cStringIO.StringIO()
self._encode(obj, io.write)
value = io.getvalue()
Expand Down Expand Up @@ -329,7 +340,8 @@ def _encode(self, obj, write):
write(STRING)
write(obj)
else:
raise BananaError("could not send object: %r" % (obj,))
raise BananaError("Banana cannot send {0} objects: {1!r}".format(
fullyQualifiedName(type(obj)), obj))


# For use from the interactive interpreter
Expand Down
45 changes: 45 additions & 0 deletions twisted/test/test_banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,51 @@ def testString(self):
self.enc.dataReceived(self.io.getvalue())
assert self.result == 'hello'


def test_unsupportedUnicode(self):
"""
Banana does not support unicode. ``Banana.sendEncoded`` raises
``BananaError`` if called with an instance of ``unicode``.
"""
self._unsupportedTypeTest(u"hello", "__builtin__.unicode")


def test_unsupportedBuiltinType(self):
"""
Banana does not support arbitrary builtin types like L{type}.
L{banana.Banana.sendEncoded} raises L{banana.BananaError} if called
with an instance of L{type}.
"""
# type is an instance of type
self._unsupportedTypeTest(type, "__builtin__.type")


def test_unsupportedUserType(self):
"""
Banana does not support arbitrary user-defined types (such as those
defined with the ``class`` statement). ``Banana.sendEncoded`` raises
``BananaError`` if called with an instance of such a type.
"""
self._unsupportedTypeTest(MathTestCase(), __name__ + ".MathTestCase")


def _unsupportedTypeTest(self, obj, name):
"""
Assert that L{banana.Banana.sendEncoded} raises L{banana.BananaError}
if called with the given object.
@param obj: Some object that Banana does not support.
@param name: The name of the type of the object.
@raise: The failure exception is raised if L{Banana.sendEncoded} does
not raise L{banana.BananaError} or if the message associated with the
exception is not formatted to include the type of the unsupported
object.
"""
exc = self.assertRaises(banana.BananaError, self.enc.sendEncoded, obj)
self.assertIn("Banana cannot send {0} objects".format(name), str(exc))


def test_int(self):
"""
A positive integer less than 2 ** 32 should round-trip through
Expand Down
1 change: 1 addition & 0 deletions twisted/topfiles/7663.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
twisted.spread.banana.Banana.sendEncoded() now raises a more informative error message if the user tries to encode objects of unsupported type.

0 comments on commit fbad806

Please sign in to comment.