From fb47f0eb986fd9d203279377440548a84c45a29f Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Sat, 30 May 2020 06:37:14 -0700 Subject: [PATCH] WireData did not implement the iteration protocol correctly and was thus uniterable. --- dns/wiredata.py | 9 ++------- tests/test_wiredata.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/dns/wiredata.py b/dns/wiredata.py index 71edf919..64bc120a 100644 --- a/dns/wiredata.py +++ b/dns/wiredata.py @@ -42,13 +42,8 @@ class WireData(bytes): raise dns.exception.FormError def __iter__(self): - i = 0 - while 1: - try: - yield self[i] - i += 1 - except dns.exception.FormError: - raise StopIteration + for i in range(len(self)): + yield self[i] def unwrap(self): return bytes(self) diff --git a/tests/test_wiredata.py b/tests/test_wiredata.py index 7b59c3cd..9274259a 100644 --- a/tests/test_wiredata.py +++ b/tests/test_wiredata.py @@ -9,7 +9,7 @@ import unittest from dns.exception import FormError -from dns.wiredata import WireData +from dns.wiredata import WireData, maybe_wrap class WireDataSlicingTestCase(unittest.TestCase): @@ -121,3 +121,14 @@ class WireDataSlicingTestCase(unittest.TestCase): inst = WireData(b'0123456789') with self.assertRaises(FormError): inst[10] # pylint: disable=pointless-statement + + def testIteration(self): + bval = b'0123' + inst = WireData(bval) + l = list(inst) + self.assertEqual(l, [x for x in bval]) + + def testBadWrap(self): + def bad(): + w = maybe_wrap(123) + self.assertRaises(ValueError, bad) -- 2.47.3