diff --git a/swift/obj/diskfile.py b/swift/obj/diskfile.py index 2ae3dd654b..089a529f54 100644 --- a/swift/obj/diskfile.py +++ b/swift/obj/diskfile.py @@ -274,14 +274,18 @@ class DiskFile(object): length = stop - start else: length = None - for chunk in self: - if length is not None: - length -= len(chunk) - if length < 0: - # Chop off the extra: - yield chunk[:length] - break - yield chunk + try: + for chunk in self: + if length is not None: + length -= len(chunk) + if length < 0: + # Chop off the extra: + yield chunk[:length] + break + yield chunk + finally: + if not self.suppress_file_closing: + self.close() def app_iter_ranges(self, ranges, content_type, boundary, size): """Returns an iterator over the data file for a set of ranges""" diff --git a/test/unit/obj/test_diskfile.py b/test/unit/obj/test_diskfile.py index 00b611a4c4..b10360aa53 100644 --- a/test/unit/obj/test_diskfile.py +++ b/test/unit/obj/test_diskfile.py @@ -71,6 +71,12 @@ class TestDiskFile(unittest.TestCase): FakeLogger(), keep_data_fp=True) self.assertEqual(''.join(df.app_iter_range(5, None)), '67890') + def test_disk_file_app_iter_partial_closes(self): + df = self._create_test_file('1234567890') + it = df.app_iter_range(0, 5) + self.assertEqual(''.join(it), '12345') + self.assertEqual(df.fp, None) + def test_disk_file_app_iter_ranges(self): df = self._create_test_file('012345678911234567892123456789') it = df.app_iter_ranges([(0, 10), (10, 20), (20, 30)], 'plain/text',