From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id B5EBB1382C5 for ; Thu, 31 Dec 2020 02:17:34 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id CCCDAE086F; Thu, 31 Dec 2020 02:17:33 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id A4CA3E086F for ; Thu, 31 Dec 2020 02:17:33 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 67D1933FE60 for ; Thu, 31 Dec 2020 02:17:32 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 8F3CE46B for ; Thu, 31 Dec 2020 02:17:30 +0000 (UTC) From: "Zac Medico" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Zac Medico" Message-ID: <1609378842.e93e6d65fa1ca75f676a227f7918f8b6d747425c.zmedico@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/util/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/util/__init__.py X-VCS-Directories: lib/portage/util/ X-VCS-Committer: zmedico X-VCS-Committer-Name: Zac Medico X-VCS-Revision: e93e6d65fa1ca75f676a227f7918f8b6d747425c X-VCS-Branch: master Date: Thu, 31 Dec 2020 02:17:30 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: d32f4b8f-8487-4f2b-9323-2c19b64f2862 X-Archives-Hash: 54b20d41ee81d973fbf6506adebd0061 commit: e93e6d65fa1ca75f676a227f7918f8b6d747425c Author: Florian Schmaus geekplace eu> AuthorDate: Fri Dec 18 18:46:38 2020 +0000 Commit: Zac Medico gentoo org> CommitDate: Thu Dec 31 01:40:42 2020 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=e93e6d65 Make atomic_ofstream a Context Manager This allows using a "with statement" together with instances of atomic_ofstream. Allowing for more readable, less error prone and shorter code. Signed-off-by: Florian Schmaus geekplace.eu> Signed-off-by: Zac Medico gentoo.org> lib/portage/util/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/portage/util/__init__.py b/lib/portage/util/__init__.py index 0412b2b59..bedcbcfe6 100644 --- a/lib/portage/util/__init__.py +++ b/lib/portage/util/__init__.py @@ -11,6 +11,7 @@ __all__ = ['apply_permissions', 'apply_recursive_permissions', 'stack_dicts', 'stack_lists', 'unique_array', 'unique_everseen', 'varexpand', 'write_atomic', 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout'] +from contextlib import AbstractContextManager from copy import deepcopy import errno import io @@ -1246,7 +1247,7 @@ def apply_secpass_permissions(filename, uid=-1, gid=-1, mode=-1, mask=-1, stat_cached=stat_cached, follow_links=follow_links) return all_applied -class atomic_ofstream(ObjectProxy): +class atomic_ofstream(AbstractContextManager, ObjectProxy): """Write a file atomically via os.rename(). Atomic replacement prevents interprocess interference and prevents corruption of the target file when the write is interrupted (for example, when an 'out of space' @@ -1287,6 +1288,13 @@ class atomic_ofstream(ObjectProxy): encoding=_encodings['fs'], errors='strict'), mode=mode, **kargs)) + def __exit__(self, exc_type, exc_val, exc_tb): + if exc_type is not None: + self.abort() + else: + self.close() + return None + def _get_target(self): return object.__getattribute__(self, '_file')