Issue #11694: Raise ConversionError in xdrlib as documented
This commit is contained in:
parent
866c4e2188
commit
3894b2a24f
|
@ -51,8 +51,32 @@ class XDRTest(unittest.TestCase):
|
||||||
up.done()
|
up.done()
|
||||||
self.assertRaises(EOFError, up.unpack_uint)
|
self.assertRaises(EOFError, up.unpack_uint)
|
||||||
|
|
||||||
|
class ConversionErrorTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.packer = xdrlib.Packer()
|
||||||
|
|
||||||
|
def assertRaisesConversion(self, *args):
|
||||||
|
self.assertRaises(xdrlib.ConversionError, *args)
|
||||||
|
|
||||||
|
def test_pack_int(self):
|
||||||
|
self.assertRaisesConversion(self.packer.pack_int, 'string')
|
||||||
|
|
||||||
|
def test_pack_uint(self):
|
||||||
|
self.assertRaisesConversion(self.packer.pack_uint, 'string')
|
||||||
|
|
||||||
|
def test_float(self):
|
||||||
|
self.assertRaisesConversion(self.packer.pack_float, 'string')
|
||||||
|
|
||||||
|
def test_double(self):
|
||||||
|
self.assertRaisesConversion(self.packer.pack_double, 'string')
|
||||||
|
|
||||||
|
def test_uhyper(self):
|
||||||
|
self.assertRaisesConversion(self.packer.pack_uhyper, 'string')
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
support.run_unittest(XDRTest)
|
support.run_unittest(XDRTest)
|
||||||
|
support.run_unittest(ConversionErrorTest)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main()
|
test_main()
|
||||||
|
|
|
@ -6,6 +6,7 @@ See: RFC 1014
|
||||||
|
|
||||||
import struct
|
import struct
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
|
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
|
||||||
|
|
||||||
|
@ -31,6 +32,16 @@ class Error(Exception):
|
||||||
class ConversionError(Error):
|
class ConversionError(Error):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def raise_conversion_error(function):
|
||||||
|
""" Wrap any raised struct.errors in a ConversionError. """
|
||||||
|
|
||||||
|
@wraps(function)
|
||||||
|
def result(self, value):
|
||||||
|
try:
|
||||||
|
return function(self, value)
|
||||||
|
except struct.error as e:
|
||||||
|
raise ConversionError(e.args[0]) from None
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class Packer:
|
class Packer:
|
||||||
|
@ -47,9 +58,11 @@ class Packer:
|
||||||
# backwards compatibility
|
# backwards compatibility
|
||||||
get_buf = get_buffer
|
get_buf = get_buffer
|
||||||
|
|
||||||
|
@raise_conversion_error
|
||||||
def pack_uint(self, x):
|
def pack_uint(self, x):
|
||||||
self.__buf.write(struct.pack('>L', x))
|
self.__buf.write(struct.pack('>L', x))
|
||||||
|
|
||||||
|
@raise_conversion_error
|
||||||
def pack_int(self, x):
|
def pack_int(self, x):
|
||||||
self.__buf.write(struct.pack('>l', x))
|
self.__buf.write(struct.pack('>l', x))
|
||||||
|
|
||||||
|
@ -60,20 +73,24 @@ class Packer:
|
||||||
else: self.__buf.write(b'\0\0\0\0')
|
else: self.__buf.write(b'\0\0\0\0')
|
||||||
|
|
||||||
def pack_uhyper(self, x):
|
def pack_uhyper(self, x):
|
||||||
|
try:
|
||||||
self.pack_uint(x>>32 & 0xffffffff)
|
self.pack_uint(x>>32 & 0xffffffff)
|
||||||
|
except (TypeError, struct.error) as e:
|
||||||
|
raise ConversionError(e.args[0]) from None
|
||||||
|
try:
|
||||||
self.pack_uint(x & 0xffffffff)
|
self.pack_uint(x & 0xffffffff)
|
||||||
|
except (TypeError, struct.error) as e:
|
||||||
|
raise ConversionError(e.args[0]) from None
|
||||||
|
|
||||||
pack_hyper = pack_uhyper
|
pack_hyper = pack_uhyper
|
||||||
|
|
||||||
|
@raise_conversion_error
|
||||||
def pack_float(self, x):
|
def pack_float(self, x):
|
||||||
try: self.__buf.write(struct.pack('>f', x))
|
self.__buf.write(struct.pack('>f', x))
|
||||||
except struct.error as msg:
|
|
||||||
raise ConversionError(msg)
|
|
||||||
|
|
||||||
|
@raise_conversion_error
|
||||||
def pack_double(self, x):
|
def pack_double(self, x):
|
||||||
try: self.__buf.write(struct.pack('>d', x))
|
self.__buf.write(struct.pack('>d', x))
|
||||||
except struct.error as msg:
|
|
||||||
raise ConversionError(msg)
|
|
||||||
|
|
||||||
def pack_fstring(self, n, s):
|
def pack_fstring(self, n, s):
|
||||||
if n < 0:
|
if n < 0:
|
||||||
|
|
|
@ -24,6 +24,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #11694: Raise ConversionError in xdrlib as documented. Patch
|
||||||
|
by Filip Gruszczyński and Claudiu Popa.
|
||||||
|
|
||||||
- Issue #22462: Fix pyexpat's creation of a dummy frame to make it
|
- Issue #22462: Fix pyexpat's creation of a dummy frame to make it
|
||||||
appear in exception tracebacks.
|
appear in exception tracebacks.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue