Patchwork [2,of,2,V3] util: use a context manager in readlock()

login
register
mail settings
Submitter Matt Harbison
Date Sept. 29, 2018, 2:33 a.m.
Message ID <cddac40b1a06c37cede9.1538188400@Envy>
Download mbox | patch
Permalink /patch/35198/
State Accepted
Headers show

Comments

Matt Harbison - Sept. 29, 2018, 2:33 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1538187525 14400
#      Fri Sep 28 22:18:45 2018 -0400
# Node ID cddac40b1a06c37cede9757914d930de92250612
# Parent  1b472f36e081a9a98e45f84a0cf34f5b6008dd47
util: use a context manager in readlock()
Yuya Nishihara - Sept. 30, 2018, 12:46 a.m.
On Fri, 28 Sep 2018 22:33:20 -0400, Matt Harbison wrote:
> # HG changeset patch
> # User Matt Harbison <matt_harbison@yahoo.com>
> # Date 1538187525 14400
> #      Fri Sep 28 22:18:45 2018 -0400
> # Node ID cddac40b1a06c37cede9757914d930de92250612
> # Parent  1b472f36e081a9a98e45f84a0cf34f5b6008dd47
> util: use a context manager in readlock()

Queued, thanks.

> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1848,10 +1848,8 @@ def readlock(pathname):
>              raise
>      except AttributeError: # no symlink in os
>          pass

Related to the previous patch, it seems not ideal to catch AttributeError
raised inside windows.readlink(). We might want an exception denoting platform
capability is not satisfied.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1848,10 +1848,8 @@  def readlock(pathname):
             raise
     except AttributeError: # no symlink in os
         pass
-    fp = posixfile(pathname, 'rb')
-    r = fp.read()
-    fp.close()
-    return r
+    with posixfile(pathname, 'rb') as fp:
+        return fp.read()
 
 def fstat(fp):
     '''stat file object that may not have fileno method.'''