Merge "Close iterables at the end of iteration"
This commit is contained in:
@@ -442,7 +442,11 @@ class IterableWithLength(object):
|
|||||||
self.length = length
|
self.length = length
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self.iterable
|
try:
|
||||||
|
for chunk in self.iterable:
|
||||||
|
yield chunk
|
||||||
|
finally:
|
||||||
|
self.iterable.close()
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
return next(self.iterable)
|
return next(self.iterable)
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import mock
|
||||||
import six
|
import six
|
||||||
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange
|
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange
|
||||||
from six.moves import range
|
from six.moves import range
|
||||||
@@ -163,3 +164,15 @@ class TestUtils(testtools.TestCase):
|
|||||||
self.assertIn('--test', arg)
|
self.assertIn('--test', arg)
|
||||||
self.assertEqual(str, opts['type'])
|
self.assertEqual(str, opts['type'])
|
||||||
self.assertIn('None, opt-1, opt-2', opts['help'])
|
self.assertIn('None, opt-1, opt-2', opts['help'])
|
||||||
|
|
||||||
|
def test_iterable_closes(self):
|
||||||
|
# Regression test for bug 1461678.
|
||||||
|
def _iterate(i):
|
||||||
|
for chunk in i:
|
||||||
|
raise(IOError)
|
||||||
|
|
||||||
|
data = six.moves.StringIO('somestring')
|
||||||
|
data.close = mock.Mock()
|
||||||
|
i = utils.IterableWithLength(data, 10)
|
||||||
|
self.assertRaises(IOError, _iterate, i)
|
||||||
|
data.close.assert_called_with()
|
||||||
|
Reference in New Issue
Block a user