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,6 +274,7 @@ class DiskFile(object): | ||||
|             length = stop - start | ||||
|         else: | ||||
|             length = None | ||||
|         try: | ||||
|             for chunk in self: | ||||
|                 if length is not None: | ||||
|                     length -= len(chunk) | ||||
| @@ -282,6 +283,9 @@ class DiskFile(object): | ||||
|                         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""" | ||||
|   | ||||
| @@ -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', | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Alex Gaynor
					Alex Gaynor