[Python-modules-commits] [dulwich] 06/19: Raise FileLocked in GitFile.

Jelmer Vernooij jelmer at moszumanska.debian.org
Sun Oct 29 17:22:27 UTC 2017


This is an automated email from the git hooks/post-receive script.

jelmer pushed a commit to branch master
in repository dulwich.

commit d4899f081fa9e4ccb6ee5e2fe23ed7e5f5e5a808
Author: Jelmer Vernooij <jelmer at debian.org>
Date:   Sun Oct 15 15:44:05 2017 +0100

    Raise FileLocked in GitFile.
---
 NEWS                       |  5 +++++
 dulwich/file.py            | 21 ++++++++++++++++++---
 dulwich/tests/test_file.py |  6 +++---
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/NEWS b/NEWS
index ca4c3ff..415e4e1 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,11 @@
 
   * Fix cwd for hooks. (Fabian Grünbichler)
 
+ API CHANGES
+
+  * GitFile now raises ``FileLocked`` when encountering a lock
+    rather than OSError(EEXIST). (Jelmer Vernooij)
+
 0.18.4	2017-10-01
 
  BUG FIXES
diff --git a/dulwich/file.py b/dulwich/file.py
index 78b46e9..96275a8 100644
--- a/dulwich/file.py
+++ b/dulwich/file.py
@@ -90,6 +90,15 @@ def GitFile(filename, mode='rb', bufsize=-1):
         return io.open(filename, mode, bufsize)
 
 
+class FileLocked(Exception):
+    """File is already locked."""
+
+    def __init__(self, filename, lockfilename):
+        self.filename = filename
+        self.lockfilename = lockfilename
+        super(FileLocked, self).__init__(filename, lockfilename)
+
+
 class _GitFile(object):
     """File that follows the git locking protocol for writes.
 
@@ -110,9 +119,14 @@ class _GitFile(object):
     def __init__(self, filename, mode, bufsize):
         self._filename = filename
         self._lockfilename = '%s.lock' % self._filename
-        fd = os.open(
-            self._lockfilename,
-            os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0))
+        try:
+            fd = os.open(
+                self._lockfilename,
+                os.O_RDWR | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0))
+        except OSError as e:
+            if e.errno == errno.EEXIST:
+                raise FileLocked(filename, self._lockfilename)
+            raise
         self._file = os.fdopen(fd, mode, bufsize)
         self._closed = False
 
@@ -149,6 +163,7 @@ class _GitFile(object):
         """
         if self._closed:
             return
+        os.fsync(self._file.fileno())
         self._file.close()
         try:
             try:
diff --git a/dulwich/tests/test_file.py b/dulwich/tests/test_file.py
index 2d028d6..9f937cc 100644
--- a/dulwich/tests/test_file.py
+++ b/dulwich/tests/test_file.py
@@ -25,7 +25,7 @@ import shutil
 import sys
 import tempfile
 
-from dulwich.file import GitFile, _fancy_rename
+from dulwich.file import FileLocked, GitFile, _fancy_rename
 from dulwich.tests import (
     SkipTest,
     TestCase,
@@ -158,8 +158,8 @@ class GitFileTests(TestCase):
         try:
             f2 = GitFile(foo, 'wb')
             self.fail()
-        except OSError as e:
-            self.assertEqual(errno.EEXIST, e.errno)
+        except FileLocked as e:
+            pass
         else:
             f2.close()
         f1.write(b' contents')

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/dulwich.git



More information about the Python-modules-commits mailing list