diff -r b7c0137cccbe Lib/test/test_xdrlib.py --- a/Lib/test/test_xdrlib.py Thu Mar 26 23:50:57 2015 +0100 +++ b/Lib/test/test_xdrlib.py Fri Mar 27 13:59:18 2015 +0100 @@ -51,6 +51,13 @@ up.done() self.assertRaises(EOFError, up.unpack_uint) + def test_pack_string(self): + p = xdrlib.Packer() + + with self.assertRaises(TypeError): + p.pack_string('hello world') + self.assertFalse(p.get_buffer()) + class ConversionErrorTest(unittest.TestCase): def setUp(self): diff -r b7c0137cccbe Lib/xdrlib.py --- a/Lib/xdrlib.py Thu Mar 26 23:50:57 2015 +0100 +++ b/Lib/xdrlib.py Fri Mar 27 13:59:18 2015 +0100 @@ -105,7 +105,12 @@ def pack_string(self, s): n = len(s) self.pack_uint(n) - self.pack_fstring(n, s) + try: + self.pack_fstring(n, s) + except TypeError as e: + self.__buf.seek(self.__buf.tell() - 4) + self.__buf.truncate() + raise e pack_opaque = pack_string pack_bytes = pack_string