# HG changeset patch # User Pierre-Yves David # Date 1652447997 -7200 # Node ID f923bdd7477d3b02feb5dbd14001aa8580bdddef # Parent 71774d799de761130b03b2494df40c02d7fbb316 branchmap: use a context manager when writing the branchmap This is cleaner and safer. The previous code date from long before we had context manager available. diff -r 71774d799de7 -r f923bdd7477d mercurial/branchmap.py --- a/mercurial/branchmap.py Tue Apr 05 05:01:58 2022 +0200 +++ b/mercurial/branchmap.py Fri May 13 15:19:57 2022 +0200 @@ -428,22 +428,22 @@ self._delayed = True return try: - f = repo.cachevfs(self._filename(repo), b"w", atomictemp=True) - cachekey = [hex(self.tipnode), b'%d' % self.tiprev] - if self.filteredhash is not None: - cachekey.append(hex(self.filteredhash)) - f.write(b" ".join(cachekey) + b'\n') - nodecount = 0 - for label, nodes in sorted(self._entries.items()): - label = encoding.fromlocal(label) - for node in nodes: - nodecount += 1 - if node in self._closednodes: - state = b'c' - else: - state = b'o' - f.write(b"%s %s %s\n" % (hex(node), state, label)) - f.close() + filename = self._filename(repo) + with repo.cachevfs(filename, b"w", atomictemp=True) as f: + cachekey = [hex(self.tipnode), b'%d' % self.tiprev] + if self.filteredhash is not None: + cachekey.append(hex(self.filteredhash)) + f.write(b" ".join(cachekey) + b'\n') + nodecount = 0 + for label, nodes in sorted(self._entries.items()): + label = encoding.fromlocal(label) + for node in nodes: + nodecount += 1 + if node in self._closednodes: + state = b'c' + else: + state = b'o' + f.write(b"%s %s %s\n" % (hex(node), state, label)) repo.ui.log( b'branchcache', b'wrote %s with %d labels and %d nodes\n',