[PATCH 01 of 11] util: add tryunlink function

Ryan McElroy rm at fb.com
Tue Mar 21 13:54:23 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 5c9cdd8046845f76e169885ed490a603b911d0d4
# Parent  49de4dfb282e2ad4dc91328ffd7fc396ee92b4a0
util: add tryunlink function

Throughout mercurial cdoe, there is a common pattern of attempting to remove
a file and ignoring ENOENT errors. Let's move this into a common function to
allow for cleaner code.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1617,6 +1617,14 @@ def unlinkpath(f, ignoremissing=False):
     except OSError:
         pass
 
+def tryunlink(f):
+    """Attempt to remove a file, ignoring ENOENT errors."""
+    try:
+        unlink(f)
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            raise
+
 def makedirs(name, mode=None, notindexed=False):
     """recursive directory creation with parent mode inheritance
 


More information about the Mercurial-devel mailing list