diff options
author | Xavier de Gaye <xdegaye@users.sourceforge.net> | 2016-12-12 08:55:57 (GMT) |
---|---|---|
committer | Xavier de Gaye <xdegaye@users.sourceforge.net> | 2016-12-12 08:55:57 (GMT) |
commit | 452b3a6a3e9e4ca99d9c6caff367fce749a7ec58 (patch) | |
tree | 86c92d001866ca9e68099298f75b39d048a98176 /Lib/mailbox.py | |
parent | b227227cb574a7bad6ed6d5cfc3ef23b10b1919f (diff) | |
download | cpython-452b3a6a3e9e4ca99d9c6caff367fce749a7ec58.zip cpython-452b3a6a3e9e4ca99d9c6caff367fce749a7ec58.tar.gz cpython-452b3a6a3e9e4ca99d9c6caff367fce749a7ec58.tar.bz2 |
Issue #28764: Fix a test_mailbox failure on Android API 24 when run as a non-root user.
Diffstat (limited to 'Lib/mailbox.py')
-rw-r--r-- | Lib/mailbox.py | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 0e23987..39f24f9 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -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' % |