From f156151e58b03fa3439b2937df65f50c0a12b443 Mon Sep 17 00:00:00 2001 From: Cologler Date: Mon, 10 Aug 2020 15:20:27 +0800 Subject: [PATCH] ignore file not found when remove file --- ilock/__init__.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/ilock/__init__.py b/ilock/__init__.py index 0d223fe..00f5d4d 100644 --- a/ilock/__init__.py +++ b/ilock/__init__.py @@ -54,18 +54,16 @@ def __exit__(self, exc_type, exc_val, exc_tb): if self._enter_count > 0: return - if sys.platform.startswith('linux'): - # In Linux you can delete a locked file - os.unlink(self._filepath) - self._lockfile.close() - if sys.platform == 'win32': - # In Windows you need to unlock a file before deletion - try: - os.remove(self._filepath) - except WindowsError as e: - # Mute exception in case an access was already acquired (EACCES) - # and in more rare case when it was even already released and file was deleted (ENOENT) - if e.errno not in [errno.EACCES, errno.ENOENT]: - raise + try: + os.unlink(self._filepath) + except WindowsError as e: + # Mute exception in case an access was already acquired (EACCES) + # and in more rare case when it was even already released and file was deleted (ENOENT) + if e.errno not in [errno.EACCES, errno.ENOENT]: + raise + except FileNotFoundError: + # the file may already removed from another process. + # https://github.com/symonsoft/ilock/issues/4 + pass