Always close files when creating collection
If an exception occurs we rely on garbage collection to close the files.
This commit is contained in:
		| @@ -25,6 +25,7 @@ entry. | |||||||
|  |  | ||||||
| """ | """ | ||||||
|  |  | ||||||
|  | import contextlib | ||||||
| import errno | import errno | ||||||
| import json | import json | ||||||
| import os | import os | ||||||
| @@ -583,17 +584,18 @@ class Collection(BaseCollection): | |||||||
|         uploads them nonatomic and without existence checks. |         uploads them nonatomic and without existence checks. | ||||||
|  |  | ||||||
|         """ |         """ | ||||||
|  |         with contextlib.ExitStack() as stack: | ||||||
|             fs = [] |             fs = [] | ||||||
|             for href, item in vobject_items.items(): |             for href, item in vobject_items.items(): | ||||||
|                 if not is_safe_filesystem_path_component(href): |                 if not is_safe_filesystem_path_component(href): | ||||||
|                     raise UnsafePathError(href) |                     raise UnsafePathError(href) | ||||||
|                 path = path_to_filesystem(self._filesystem_path, href) |                 path = path_to_filesystem(self._filesystem_path, href) | ||||||
|             fs.append(open(path, "w", encoding=self.encoding, newline="")) |                 fs.append(stack.enter_context( | ||||||
|  |                     open(path, "w", encoding=self.encoding, newline=""))) | ||||||
|                 fs[-1].write(item.serialize()) |                 fs[-1].write(item.serialize()) | ||||||
|             # sync everything at once because it's slightly faster. |             # sync everything at once because it's slightly faster. | ||||||
|             for f in fs: |             for f in fs: | ||||||
|                 self._fsync(f.fileno()) |                 self._fsync(f.fileno()) | ||||||
|             f.close() |  | ||||||
|         self._sync_directory(self._filesystem_path) |         self._sync_directory(self._filesystem_path) | ||||||
|  |  | ||||||
|     @classmethod |     @classmethod | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Unrud
					Unrud