Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3894b2a

Browse files
committed
Issue #11694: Raise ConversionError in xdrlib as documented
1 parent 866c4e2 commit 3894b2a

3 files changed

Lines changed: 52 additions & 8 deletions

File tree

Lib/test/test_xdrlib.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,32 @@ def test_xdr(self):
5151
up.done()
5252
self.assertRaises(EOFError, up.unpack_uint)
5353

54+
class ConversionErrorTest(unittest.TestCase):
55+
56+
def setUp(self):
57+
self.packer = xdrlib.Packer()
58+
59+
def assertRaisesConversion(self, *args):
60+
self.assertRaises(xdrlib.ConversionError, *args)
61+
62+
def test_pack_int(self):
63+
self.assertRaisesConversion(self.packer.pack_int, 'string')
64+
65+
def test_pack_uint(self):
66+
self.assertRaisesConversion(self.packer.pack_uint, 'string')
67+
68+
def test_float(self):
69+
self.assertRaisesConversion(self.packer.pack_float, 'string')
70+
71+
def test_double(self):
72+
self.assertRaisesConversion(self.packer.pack_double, 'string')
73+
74+
def test_uhyper(self):
75+
self.assertRaisesConversion(self.packer.pack_uhyper, 'string')
76+
5477
def test_main():
5578
support.run_unittest(XDRTest)
79+
support.run_unittest(ConversionErrorTest)
5680

5781
if __name__ == "__main__":
5882
test_main()

Lib/xdrlib.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import struct
88
from io import BytesIO
9+
from functools import wraps
910

1011
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
1112

@@ -31,6 +32,16 @@ def __str__(self):
3132
class ConversionError(Error):
3233
pass
3334

35+
def raise_conversion_error(function):
36+
""" Wrap any raised struct.errors in a ConversionError. """
37+
38+
@wraps(function)
39+
def result(self, value):
40+
try:
41+
return function(self, value)
42+
except struct.error as e:
43+
raise ConversionError(e.args[0]) from None
44+
return result
3445

3546

3647
class Packer:
@@ -47,9 +58,11 @@ def get_buffer(self):
4758
# backwards compatibility
4859
get_buf = get_buffer
4960

61+
@raise_conversion_error
5062
def pack_uint(self, x):
5163
self.__buf.write(struct.pack('>L', x))
5264

65+
@raise_conversion_error
5366
def pack_int(self, x):
5467
self.__buf.write(struct.pack('>l', x))
5568

@@ -60,20 +73,24 @@ def pack_bool(self, x):
6073
else: self.__buf.write(b'\0\0\0\0')
6174

6275
def pack_uhyper(self, x):
63-
self.pack_uint(x>>32 & 0xffffffff)
64-
self.pack_uint(x & 0xffffffff)
76+
try:
77+
self.pack_uint(x>>32 & 0xffffffff)
78+
except (TypeError, struct.error) as e:
79+
raise ConversionError(e.args[0]) from None
80+
try:
81+
self.pack_uint(x & 0xffffffff)
82+
except (TypeError, struct.error) as e:
83+
raise ConversionError(e.args[0]) from None
6584

6685
pack_hyper = pack_uhyper
6786

87+
@raise_conversion_error
6888
def pack_float(self, x):
69-
try: self.__buf.write(struct.pack('>f', x))
70-
except struct.error as msg:
71-
raise ConversionError(msg)
89+
self.__buf.write(struct.pack('>f', x))
7290

91+
@raise_conversion_error
7392
def pack_double(self, x):
74-
try: self.__buf.write(struct.pack('>d', x))
75-
except struct.error as msg:
76-
raise ConversionError(msg)
93+
self.__buf.write(struct.pack('>d', x))
7794

7895
def pack_fstring(self, n, s):
7996
if n < 0:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #11694: Raise ConversionError in xdrlib as documented. Patch
28+
by Filip Gruszczyński and Claudiu Popa.
29+
2730
- Issue #22462: Fix pyexpat's creation of a dummy frame to make it
2831
appear in exception tracebacks.
2932

0 commit comments

Comments
 (0)