summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2014-10-10 18:29:43 (GMT)
committerPetri Lehtinen <petri@digip.org>2014-10-10 18:29:43 (GMT)
commit221439dd618ae531d2a7c2636d2649521eb10db1 (patch)
treedb79cdcf5bf4d743712b72d67ba5d3c7729b8bb2
parent5e8b04eefb08828dde5b609efc7e1430244a6ae3 (diff)
parent3894b2a24f1a0a94601cc2436f779aa666a2f9dd (diff)
downloadcpython-221439dd618ae531d2a7c2636d2649521eb10db1.zip
cpython-221439dd618ae531d2a7c2636d2649521eb10db1.tar.gz
cpython-221439dd618ae531d2a7c2636d2649521eb10db1.tar.bz2
#11694: merge with 3.4
-rw-r--r--Lib/test/test_xdrlib.py24
-rw-r--r--Lib/xdrlib.py33
-rw-r--r--Misc/NEWS3
3 files changed, 52 insertions, 8 deletions
diff --git a/Lib/test/test_xdrlib.py b/Lib/test/test_xdrlib.py
index 6004c9f..70496d6 100644
--- a/Lib/test/test_xdrlib.py
+++ b/Lib/test/test_xdrlib.py
@@ -51,8 +51,32 @@ class XDRTest(unittest.TestCase):
up.done()
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():
support.run_unittest(XDRTest)
+ support.run_unittest(ConversionErrorTest)
if __name__ == "__main__":
test_main()
diff --git a/Lib/xdrlib.py b/Lib/xdrlib.py
index c05cf87..d6e1aeb 100644
--- a/Lib/xdrlib.py
+++ b/Lib/xdrlib.py
@@ -6,6 +6,7 @@ See: RFC 1014
import struct
from io import BytesIO
+from functools import wraps
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
@@ -31,6 +32,16 @@ class Error(Exception):
class ConversionError(Error):
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:
@@ -47,9 +58,11 @@ class Packer:
# backwards compatibility
get_buf = get_buffer
+ @raise_conversion_error
def pack_uint(self, x):
self.__buf.write(struct.pack('>L', x))
+ @raise_conversion_error
def pack_int(self, x):
self.__buf.write(struct.pack('>l', x))
@@ -60,20 +73,24 @@ class Packer:
else: self.__buf.write(b'\0\0\0\0')
def pack_uhyper(self, x):
- self.pack_uint(x>>32 & 0xffffffff)
- self.pack_uint(x & 0xffffffff)
+ try:
+ 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)
+ except (TypeError, struct.error) as e:
+ raise ConversionError(e.args[0]) from None
pack_hyper = pack_uhyper
+ @raise_conversion_error
def pack_float(self, x):
- try: self.__buf.write(struct.pack('>f', x))
- except struct.error as msg:
- raise ConversionError(msg)
+ self.__buf.write(struct.pack('>f', x))
+ @raise_conversion_error
def pack_double(self, x):
- try: self.__buf.write(struct.pack('>d', x))
- except struct.error as msg:
- raise ConversionError(msg)
+ self.__buf.write(struct.pack('>d', x))
def pack_fstring(self, n, s):
if n < 0:
diff --git a/Misc/NEWS b/Misc/NEWS
index 9c65557..856a62a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -169,6 +169,9 @@ Core and Builtins
Library
-------
+- Issue #11694: Raise ConversionError in xdrlib as documented. Patch
+ by Filip GruszczyƄski and Claudiu Popa.
+
- Issue #19380: Optimized parsing of regular expressions.
- Issue #1519638: Now unmatched groups are replaced with empty strings in re.sub()