Gentoo Archives: gentoo-portage-dev

From: Florian Schmaus <flo@×××××××××.eu>
To: gentoo-portage-dev@l.g.o
Cc: Florian Schmaus <flo@×××××××××.eu>
Subject: [gentoo-portage-dev] [PATCH] Use atomic_ofstream as Context Manager i.e., with-statement contexts
Date: Tue, 09 Mar 2021 07:26:10
Message-Id: 20210309072559.85509-1-flo@geekplace.eu
1 With [1: e93e6d65fa1c] atomic_ofstream became a Context Manager. This
2 commit transforms three further call sites of atomic_ofstream() to use
3 with-statement contexts for easier readability and increased
4 robustness against resource leaks.
5
6 1: e93e6d65fa1ca75f676a227f7918f8b6d747425c
7 Make atomic_ofstream a Context Manager
8
9 Signed-off-by: Florian Schmaus <flo@×××××××××.eu>
10 ---
11 lib/_emerge/BlockerCache.py | 6 +++---
12 lib/portage/dbapi/_VdbMetadataDelta.py | 11 +++++------
13 lib/portage/dbapi/vartree.py | 6 +++---
14 3 files changed, 11 insertions(+), 12 deletions(-)
15
16 diff --git a/lib/_emerge/BlockerCache.py b/lib/_emerge/BlockerCache.py
17 index 8154d9adee07..035f2212d3c6 100644
18 --- a/lib/_emerge/BlockerCache.py
19 +++ b/lib/_emerge/BlockerCache.py
20 @@ -133,9 +133,9 @@ class BlockerCache(portage.cache.mappings.MutableMapping):
21 if len(self._modified) >= self._cache_threshold and \
22 secpass >= 2:
23 try:
24 - f = portage.util.atomic_ofstream(self._cache_filename, mode='wb')
25 - pickle.dump(self._cache_data, f, protocol=2)
26 - f.close()
27 + with portage.util.atomic_ofstream(self._cache_filename, mode='wb') as f:
28 + pickle.dump(self._cache_data, f, protocol=2)
29 +
30 portage.util.apply_secpass_permissions(
31 self._cache_filename, gid=portage.portage_gid, mode=0o644)
32 except (IOError, OSError):
33 diff --git a/lib/portage/dbapi/_VdbMetadataDelta.py b/lib/portage/dbapi/_VdbMetadataDelta.py
34 index ffdc0b361da7..568e1964a6b9 100644
35 --- a/lib/portage/dbapi/_VdbMetadataDelta.py
36 +++ b/lib/portage/dbapi/_VdbMetadataDelta.py
37 @@ -18,13 +18,12 @@ class VdbMetadataDelta:
38 self._vardb = vardb
39
40 def initialize(self, timestamp):
41 - f = atomic_ofstream(self._vardb._cache_delta_filename, 'w',
42 - encoding=_encodings['repo.content'], errors='strict')
43 - json.dump({
44 - "version": self._format_version,
45 - "timestamp": timestamp
46 + with atomic_ofstream(self._vardb._cache_delta_filename, 'w',
47 + encoding=_encodings['repo.content'], errors='strict') as f:
48 + json.dump({
49 + "version": self._format_version,
50 + "timestamp": timestamp
51 }, f, ensure_ascii=False)
52 - f.close()
53
54 def load(self):
55
56 diff --git a/lib/portage/dbapi/vartree.py b/lib/portage/dbapi/vartree.py
57 index 826083eaef17..5ae035baf601 100644
58 --- a/lib/portage/dbapi/vartree.py
59 +++ b/lib/portage/dbapi/vartree.py
60 @@ -614,9 +614,9 @@ class vardbapi(dbapi):
61 timestamp = time.time()
62 self._aux_cache["timestamp"] = timestamp
63
64 - f = atomic_ofstream(self._aux_cache_filename, 'wb')
65 - pickle.dump(self._aux_cache, f, protocol=2)
66 - f.close()
67 + with atomic_ofstream(self._aux_cache_filename, 'wb') as f:
68 + pickle.dump(self._aux_cache, f, protocol=2)
69 +
70 apply_secpass_permissions(
71 self._aux_cache_filename, mode=0o644)
72
73 --
74 2.30.1

Replies