comparison mercurial/revlog.py @ 47335:1844a2e3401c

revlog: simplify the try nesting in the `_writing` context Lets use a single try, with conditional cleanup. This make is easier to add a file handle dedicated to sidedata. Differential Revision: https://phab.mercurial-scm.org/D10775
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 26 May 2021 21:35:51 +0200
parents 0a3fa41fa719
children 27e9ed1217c5
comparison
equal deleted inserted replaced
47334:53ab13d6a5db 47335:1844a2e3401c
2230 msg %= self.display_id 2230 msg %= self.display_id
2231 raise error.ProgrammingError(msg) 2231 raise error.ProgrammingError(msg)
2232 if self._writinghandles is not None: 2232 if self._writinghandles is not None:
2233 yield 2233 yield
2234 else: 2234 else:
2235 r = len(self) 2235 ifh = dfh = None
2236 dsize = 0
2237 if r:
2238 dsize = self.end(r - 1)
2239 dfh = None
2240 if not self._inline:
2241 try:
2242 dfh = self._datafp(b"r+")
2243 if self._docket is None:
2244 dfh.seek(0, os.SEEK_END)
2245 else:
2246 dfh.seek(self._docket.data_end, os.SEEK_SET)
2247 except IOError as inst:
2248 if inst.errno != errno.ENOENT:
2249 raise
2250 dfh = self._datafp(b"w+")
2251 transaction.add(self._datafile, dsize)
2252 try: 2236 try:
2237 r = len(self)
2238 # opening the data file.
2239 dsize = 0
2240 if r:
2241 dsize = self.end(r - 1)
2242 dfh = None
2243 if not self._inline:
2244 try:
2245 dfh = self._datafp(b"r+")
2246 if self._docket is None:
2247 dfh.seek(0, os.SEEK_END)
2248 else:
2249 dfh.seek(self._docket.data_end, os.SEEK_SET)
2250 except IOError as inst:
2251 if inst.errno != errno.ENOENT:
2252 raise
2253 dfh = self._datafp(b"w+")
2254 transaction.add(self._datafile, dsize)
2255
2256 # opening the index file.
2253 isize = r * self.index.entry_size 2257 isize = r * self.index.entry_size
2254 ifh = self.__index_write_fp() 2258 ifh = self.__index_write_fp()
2255 if self._inline: 2259 if self._inline:
2256 transaction.add(self._indexfile, dsize + isize) 2260 transaction.add(self._indexfile, dsize + isize)
2257 else: 2261 else:
2258 transaction.add(self._indexfile, isize) 2262 transaction.add(self._indexfile, isize)
2259 try: 2263 # exposing all file handle for writing.
2260 self._writinghandles = (ifh, dfh) 2264 self._writinghandles = (ifh, dfh)
2261 try: 2265 yield
2262 yield 2266 if self._docket is not None:
2263 if self._docket is not None: 2267 self._write_docket(transaction)
2264 self._write_docket(transaction) 2268 finally:
2265 finally: 2269 self._writinghandles = None
2266 self._writinghandles = None 2270 if ifh is not None:
2267 finally:
2268 ifh.close() 2271 ifh.close()
2269 finally:
2270 if dfh is not None: 2272 if dfh is not None:
2271 dfh.close() 2273 dfh.close()
2272 2274
2273 def _write_docket(self, transaction): 2275 def _write_docket(self, transaction):
2274 """write the current docket on disk 2276 """write the current docket on disk