]> granicus.if.org Git - python/commitdiff
Issue #28764: Fix a test_mailbox failure on Android API 24 when run as a non-root...
authorXavier de Gaye <xdegaye@users.sourceforge.net>
Mon, 12 Dec 2016 08:55:57 +0000 (09:55 +0100)
committerXavier de Gaye <xdegaye@users.sourceforge.net>
Mon, 12 Dec 2016 08:55:57 +0000 (09:55 +0100)
Lib/mailbox.py
Lib/test/test_mailbox.py

index 0e23987ce75be0c443032d07a32dc33f8672548a..39f24f9a7239dc24613f23e0b70eb8ae97002bfd 100644 (file)
@@ -313,11 +313,12 @@ class Maildir(Mailbox):
         # final position in order to prevent race conditions with changes
         # from other programs
         try:
-            if hasattr(os, 'link'):
+            try:
                 os.link(tmp_file.name, dest)
-                os.remove(tmp_file.name)
-            else:
+            except (AttributeError, PermissionError):
                 os.rename(tmp_file.name, dest)
+            else:
+                os.remove(tmp_file.name)
         except OSError as e:
             os.remove(tmp_file.name)
             if e.errno == errno.EEXIST:
@@ -1200,13 +1201,14 @@ class MH(Mailbox):
         for key in self.iterkeys():
             if key - 1 != prev:
                 changes.append((key, prev + 1))
-                if hasattr(os, 'link'):
+                try:
                     os.link(os.path.join(self._path, str(key)),
                             os.path.join(self._path, str(prev + 1)))
-                    os.unlink(os.path.join(self._path, str(key)))
-                else:
+                except (AttributeError, PermissionError):
                     os.rename(os.path.join(self._path, str(key)),
                               os.path.join(self._path, str(prev + 1)))
+                else:
+                    os.unlink(os.path.join(self._path, str(key)))
             prev += 1
         self._next_key = prev + 1
         if len(changes) == 0:
@@ -2076,13 +2078,14 @@ def _lock_file(f, dotlock=True):
                 else:
                     raise
             try:
-                if hasattr(os, 'link'):
+                try:
                     os.link(pre_lock.name, f.name + '.lock')
                     dotlock_done = True
-                    os.unlink(pre_lock.name)
-                else:
+                except (AttributeError, PermissionError):
                     os.rename(pre_lock.name, f.name + '.lock')
                     dotlock_done = True
+                else:
+                    os.unlink(pre_lock.name)
             except FileExistsError:
                 os.remove(pre_lock.name)
                 raise ExternalClashError('dot lock unavailable: %s' %
index aeabdbb2b5e4efb53280ce77ba06bb0a5e1f286e..2ba944335ae87db5ee9fc8254d9e4aa2ea9780bb 100644 (file)
@@ -2137,9 +2137,9 @@ class MaildirTestCase(unittest.TestCase):
             if mbox:
                 fp.write(FROM_)
             fp.write(DUMMY_MESSAGE)
-        if hasattr(os, "link"):
+        try:
             os.link(tmpname, newname)
-        else:
+        except (AttributeError, PermissionError):
             with open(newname, "w") as fp:
                 fp.write(DUMMY_MESSAGE)
         self._msgfiles.append(newname)