[PATCH 1 of 3 v2] posix: use local reference to unlink

Ryan McElroy rm at fb.com
Tue Mar 21 13:53:18 UTC 2017


# HG changeset patch
# User Ryan McElroy <rmcelroy at fb.com>
# Date 1490104228 25200
#      Tue Mar 21 06:50:28 2017 -0700
# Node ID 281fd76b392c38bd5299198810f85f9e3c2034be
# Parent  527a247f114f8af37326805fd6cce923f9ac6453
posix: use local reference to unlink

We have a local reference to os.unlink in module scope, but we still used
os.unlink inside functions. This changes util to use the local reference,
which will pave the way for combining duplicated code in future patches.

diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -105,7 +105,7 @@ def setflags(f, l, x):
             fp = open(f)
             data = fp.read()
             fp.close()
-            os.unlink(f)
+            unlink(f)
             try:
                 os.symlink(data, f)
             except OSError:
@@ -118,7 +118,7 @@ def setflags(f, l, x):
     if stat.S_ISLNK(s):
         # switch link to file
         data = os.readlink(f)
-        os.unlink(f)
+        unlink(f)
         fp = open(f, "w")
         fp.write(data)
         fp.close()
@@ -187,9 +187,9 @@ def checkexec(path):
                         # check-exec is exec and check-no-exec is not exec
                         return True
                     # checknoexec exists but is exec - delete it
-                    os.unlink(checknoexec)
+                    unlink(checknoexec)
                 # checkisexec exists but is not exec - delete it
-                os.unlink(checkisexec)
+                unlink(checkisexec)
 
             # check using one file, leave it as checkisexec
             checkdir = cachedir
@@ -210,7 +210,7 @@ def checkexec(path):
                     return True
         finally:
             if fn is not None:
-                os.unlink(fn)
+                unlink(fn)
     except (IOError, OSError):
         # we don't care, the user probably won't be able to commit anyway
         return False
@@ -248,12 +248,12 @@ def checklink(path):
             try:
                 os.symlink(target, name)
                 if cachedir is None:
-                    os.unlink(name)
+                    unlink(name)
                 else:
                     try:
                         os.rename(name, checklink)
                     except OSError:
-                        os.unlink(name)
+                        unlink(name)
                 return True
             except OSError as inst:
                 # link creation might race, try again
@@ -268,7 +268,7 @@ def checklink(path):
         except OSError as inst:
             # sshfs might report failure while successfully creating the link
             if inst[0] == errno.EIO and os.path.exists(name):
-                os.unlink(name)
+                unlink(name)
             return False
 
 def checkosfilename(path):
@@ -539,7 +539,7 @@ def makedir(path, notindexed):
 def unlinkpath(f, ignoremissing=False):
     """unlink and remove the directory if it is empty"""
     try:
-        os.unlink(f)
+        unlink(f)
     except OSError as e:
         if not (ignoremissing and e.errno == errno.ENOENT):
             raise


More information about the Mercurial-devel mailing list