Mercurial > public > mercurial-scm > hg
comparison hgext/fastannotate/revmap.py @ 52391:6baf4c6ee14b
fastannotate: stop using the `pycompat.open()` shim
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Thu, 05 Dec 2024 13:11:41 -0500 |
parents | f4733654f144 |
children | 24ee91ba9aa8 |
comparison
equal
deleted
inserted
replaced
52390:e21485f51402 | 52391:6baf4c6ee14b |
---|---|
11 import io | 11 import io |
12 import os | 12 import os |
13 import struct | 13 import struct |
14 | 14 |
15 from mercurial.node import hex | 15 from mercurial.node import hex |
16 from mercurial.pycompat import open | |
17 from mercurial import ( | 16 from mercurial import ( |
18 error as hgerror, | 17 error as hgerror, |
19 ) | 18 ) |
20 from . import error | 19 from . import error |
21 | 20 |
161 def flush(self): | 160 def flush(self): |
162 """write the state down to the file""" | 161 """write the state down to the file""" |
163 if not self.path: | 162 if not self.path: |
164 return | 163 return |
165 if self._lastmaxrev == -1: # write the entire file | 164 if self._lastmaxrev == -1: # write the entire file |
166 with open(self.path, b'wb') as f: | 165 with open(self.path, 'wb') as f: |
167 f.write(self.HEADER) | 166 f.write(self.HEADER) |
168 for i in range(1, len(self._rev2hsh)): | 167 for i in range(1, len(self._rev2hsh)): |
169 self._writerev(i, f) | 168 self._writerev(i, f) |
170 else: # append incrementally | 169 else: # append incrementally |
171 with open(self.path, b'ab') as f: | 170 with open(self.path, 'ab') as f: |
172 for i in range(self._lastmaxrev + 1, len(self._rev2hsh)): | 171 for i in range(self._lastmaxrev + 1, len(self._rev2hsh)): |
173 self._writerev(i, f) | 172 self._writerev(i, f) |
174 self._lastmaxrev = self.maxrev | 173 self._lastmaxrev = self.maxrev |
175 | 174 |
176 def _load(self): | 175 def _load(self): |
179 return | 178 return |
180 # use local variables in a loop. CPython uses LOAD_FAST for them, | 179 # use local variables in a loop. CPython uses LOAD_FAST for them, |
181 # which is faster than both LOAD_CONST and LOAD_GLOBAL. | 180 # which is faster than both LOAD_CONST and LOAD_GLOBAL. |
182 flaglen = 1 | 181 flaglen = 1 |
183 hshlen = _hshlen | 182 hshlen = _hshlen |
184 with open(self.path, b'rb') as f: | 183 with open(self.path, 'rb') as f: |
185 if f.read(len(self.HEADER)) != self.HEADER: | 184 if f.read(len(self.HEADER)) != self.HEADER: |
186 raise error.CorruptedFileError() | 185 raise error.CorruptedFileError() |
187 self.clear(flush=False) | 186 self.clear(flush=False) |
188 while True: | 187 while True: |
189 buf = f.read(flaglen) | 188 buf = f.read(flaglen) |
249 """return the last hash in a revmap, without loading its full content. | 248 """return the last hash in a revmap, without loading its full content. |
250 this is equivalent to `m = revmap(path); m.rev2hsh(m.maxrev)`, but faster. | 249 this is equivalent to `m = revmap(path); m.rev2hsh(m.maxrev)`, but faster. |
251 """ | 250 """ |
252 hsh = None | 251 hsh = None |
253 try: | 252 try: |
254 with open(path, b'rb') as f: | 253 with open(path, 'rb') as f: |
255 f.seek(-_hshlen, io.SEEK_END) | 254 f.seek(-_hshlen, io.SEEK_END) |
256 if f.tell() > len(revmap.HEADER): | 255 if f.tell() > len(revmap.HEADER): |
257 hsh = f.read(_hshlen) | 256 hsh = f.read(_hshlen) |
258 except IOError: | 257 except IOError: |
259 pass | 258 pass |