[PATCH 2 of 4] revlog: add a context manager to allow file handle reuse

Yuya Nishihara yuya at tcha.org
Fri Nov 4 03:43:15 EDT 2016


On Tue, 01 Nov 2016 18:16:37 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc at gmail.com>
> # Date 1477099818 25200
> #      Fri Oct 21 18:30:18 2016 -0700
> # Node ID d631065a702fa7eb956258e2289679d5902ccff6
> # Parent  fb93d9a0a24db5a93a6a6758eacc6ba5ca37531e
> revlog: add a context manager to allow file handle reuse

> +    @contextlib.contextmanager
> +    def cachefilehandle(self):
> +        """Maintain a persistent file handle during operations.
> +
> +        When this context manager is active, a file descriptor will be reused
> +        for all read operations, ensuring the underlying revlog file isn't
> +        reopened multiple times.
> +        """
> +        if self._readfh:
> +            raise error.Abort('cachefilehandle already active')
> +
> +        # Inline revlogs have data chunks cached at open time. If we opened
> +        # a file handle it wouldn't be read.
> +        if self._inline:
> +            yield
> +            return
> +
> +        try:
> +            self._readfh = self.opener(self.datafile)
> +            yield
> +        finally:
> +            self._readfh.close()
> +            self._readfh = None

This would crash if open() failed. It should be:

  self._readfh = self.opener(self.datafile)
  try:
      yield
  finally:
      self._readfh.close()
      self._readfh = None

Should I fix it in flight? The other changes look good to me.


More information about the Mercurial-devel mailing list