When iterating over a range of a file, always close it
This is needed on Pythons without reference counting garbage collectors (e.g. PyPy). Change-Id: I1d06eb8fe08ee6eeb45caa47b653d6af0bb18267
This commit is contained in:
		| @@ -274,14 +274,18 @@ class DiskFile(object): | |||||||
|             length = stop - start |             length = stop - start | ||||||
|         else: |         else: | ||||||
|             length = None |             length = None | ||||||
|         for chunk in self: |         try: | ||||||
|             if length is not None: |             for chunk in self: | ||||||
|                 length -= len(chunk) |                 if length is not None: | ||||||
|                 if length < 0: |                     length -= len(chunk) | ||||||
|                     # Chop off the extra: |                     if length < 0: | ||||||
|                     yield chunk[:length] |                         # Chop off the extra: | ||||||
|                     break |                         yield chunk[:length] | ||||||
|             yield chunk |                         break | ||||||
|  |                 yield chunk | ||||||
|  |         finally: | ||||||
|  |             if not self.suppress_file_closing: | ||||||
|  |                 self.close() | ||||||
|  |  | ||||||
|     def app_iter_ranges(self, ranges, content_type, boundary, size): |     def app_iter_ranges(self, ranges, content_type, boundary, size): | ||||||
|         """Returns an iterator over the data file for a set of ranges""" |         """Returns an iterator over the data file for a set of ranges""" | ||||||
|   | |||||||
| @@ -71,6 +71,12 @@ class TestDiskFile(unittest.TestCase): | |||||||
|                                     FakeLogger(), keep_data_fp=True) |                                     FakeLogger(), keep_data_fp=True) | ||||||
|         self.assertEqual(''.join(df.app_iter_range(5, None)), '67890') |         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): |     def test_disk_file_app_iter_ranges(self): | ||||||
|         df = self._create_test_file('012345678911234567892123456789') |         df = self._create_test_file('012345678911234567892123456789') | ||||||
|         it = df.app_iter_ranges([(0, 10), (10, 20), (20, 30)], 'plain/text', |         it = df.app_iter_ranges([(0, 10), (10, 20), (20, 30)], 'plain/text', | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Alex Gaynor
					Alex Gaynor