[PATCH 4 of 5 V2] scmutil: use context managers for file handles

Adrian Buehlmann adrian at cadifra.com
Tue Jan 5 11:53:13 CST 2016


On 2016-01-03 01:45, Gregory Szorc wrote:
> @@ -521,21 +506,20 @@ class vfs(abstractvfs):
>                      return util.atomictempfile(f, mode, self.createmode)
>                  try:
>                      if 'w' in mode:
>                          util.unlink(f)
>                          nlink = 0
>                      else:
>                          # nlinks() may behave differently for files on Windows
>                          # shares if the file is open.
> -                        fd = util.posixfile(f)
> -                        nlink = util.nlinks(f)
> -                        if nlink < 1:
> -                            nlink = 2 # force mktempcopy (issue1922)
> -                        fd.close()
> +                        with util.posixfile(f):
> +                            nlink = util.nlinks(f)
> +                            if nlink < 1:
> +                                nlink = 2 # force mktempcopy (issue1922)
>                  except (OSError, IOError) as e:
>                      if e.errno != errno.ENOENT:
>                          raise
>                      nlink = 0
>                      util.ensuredirs(dirname, self.createmode, notindexed)
>                  if nlink > 0:
>                      if self._trustnlink is None:
>                          self._trustnlink = nlink > 1 or util.checknlink(f)

Churning this esoteric code area just in order to do this rather
cosmetic change feels a bit pointless to me, but - at least - it looks
correct to me (but I won't test it).



More information about the Mercurial-devel mailing list