瀏覽代碼

Make _fancy_rename private, and only use it on Windows.

Jelmer Vernooij 9 年之前
父節點
當前提交
1fdca18915
共有 2 個文件被更改,包括 11 次插入9 次删除
  1. 7 5
      dulwich/file.py
  2. 4 4
      dulwich/tests/test_file.py

+ 7 - 5
dulwich/file.py

@@ -19,9 +19,10 @@
 """Safe access to git files."""
 
 import errno
+import io
 import os
+import sys
 import tempfile
-import io
 
 def ensure_dir_exists(dirname):
     """Ensure a directory exists, creating if necessary."""
@@ -32,7 +33,7 @@ def ensure_dir_exists(dirname):
             raise
 
 
-def fancy_rename(oldname, newname):
+def _fancy_rename(oldname, newname):
     """Rename file with temporary backup file to rollback if rename fails"""
     if not os.path.exists(newname):
         try:
@@ -148,10 +149,11 @@ class _GitFile(object):
             try:
                 os.rename(self._lockfilename, self._filename)
             except OSError as e:
-                # Windows versions prior to Vista don't support atomic renames
-                if e.errno != errno.EEXIST:
+                if sys.platform == 'win32' and e.errno == errno.EEXIST:
+                    # Windows versions prior to Vista don't support atomic renames
+                    _fancy_rename(self._lockfilename, self._filename)
+                else:
                     raise
-                fancy_rename(self._lockfilename, self._filename)
         finally:
             self.abort()
 

+ 4 - 4
dulwich/tests/test_file.py

@@ -23,7 +23,7 @@ import shutil
 import sys
 import tempfile
 
-from dulwich.file import GitFile, fancy_rename
+from dulwich.file import GitFile, _fancy_rename
 from dulwich.tests import (
     SkipTest,
     TestCase,
@@ -53,7 +53,7 @@ class FancyRenameTests(TestCase):
 
     def test_no_dest_exists(self):
         self.assertFalse(os.path.exists(self.bar))
-        fancy_rename(self.foo, self.bar)
+        _fancy_rename(self.foo, self.bar)
         self.assertFalse(os.path.exists(self.foo))
 
         new_f = open(self.bar, 'rb')
@@ -62,7 +62,7 @@ class FancyRenameTests(TestCase):
 
     def test_dest_exists(self):
         self.create(self.bar, b'bar contents')
-        fancy_rename(self.foo, self.bar)
+        _fancy_rename(self.foo, self.bar)
         self.assertFalse(os.path.exists(self.foo))
 
         new_f = open(self.bar, 'rb')
@@ -74,7 +74,7 @@ class FancyRenameTests(TestCase):
             raise SkipTest("platform allows overwriting open files")
         self.create(self.bar, b'bar contents')
         dest_f = open(self.bar, 'rb')
-        self.assertRaises(OSError, fancy_rename, self.foo, self.bar)
+        self.assertRaises(OSError, _fancy_rename, self.foo, self.bar)
         dest_f.close()
         self.assertTrue(os.path.exists(self.path('foo')))