]> granicus.if.org Git - python/commitdiff
Update checks to consider Windows error numbers.
authorMartin v. Löwis <martin@v.loewis.de>
Thu, 4 May 2006 14:27:52 +0000 (14:27 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Thu, 4 May 2006 14:27:52 +0000 (14:27 +0000)
Lib/mailbox.py

index 14de30bb11a8bc2da515c53d70bfb988e98a9896..2d3553ab4bde93ae713563878dbb1d245074fc95 100755 (executable)
@@ -2,6 +2,7 @@
 
 """Read/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes."""
 
+import sys
 import os
 import time
 import calendar
@@ -23,6 +24,11 @@ __all__ = [ 'Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
             'BabylMessage', 'MMDFMessage', 'UnixMailbox',
             'PortableUnixMailbox', 'MmdfMailbox', 'MHMailbox', 'BabylMailbox' ]
 
+if sys.platform != 'win32':
+    # Define WindowsError so that we can use it in an except statement
+    # even on non-Windows systems
+    class WindowsError:
+        pass
 
 class Mailbox:
     """A group of messages in a particular place."""
@@ -262,10 +268,11 @@ class Maildir(Mailbox):
             self.remove(key)
         except KeyError:
             pass
+        except WindowsError, e:
+            if e.errno != 2: # ERROR_FILE_NOT_FOUND
+                raise
         except OSError, e:
-            if e.errno == errno.ENOENT:
-                pass
-            else:
+            if e.errno != errno.ENOENT:
                 raise
 
     def __setitem__(self, key, message):
@@ -419,6 +426,12 @@ class Maildir(Mailbox):
         path = os.path.join(self._path, 'tmp', uniq)
         try:
             os.stat(path)
+        except WindowsError, e:
+            if e.errno == 2: # ERROR_FILE_NOT_FOUND
+                Maildir._count += 1
+                return open(path, 'wb+')
+            else:
+                raise
         except OSError, e:
             if e.errno == errno.ENOENT:
                 Maildir._count += 1
@@ -566,6 +579,12 @@ class _singlefileMailbox(Mailbox):
         self._file.close()
         try:
             os.rename(new_file.name, self._path)
+        except WindowsError, e:
+            if e.errno == 183: # ERROR_ALREADY_EXISTS
+                os.remove(self._path)
+                os.rename(new_file.name, self._path)
+            else:
+                raise
         except OSError, e:
             if e.errno == errno.EEXIST:
                 os.remove(self._path)
@@ -1837,6 +1856,13 @@ def _lock_file(f, dotlock=True):
                 else:
                     os.rename(pre_lock.name, f.name + '.lock')
                     dotlock_done = True
+            except WindowsError, e:
+                if e.errno == 183:  # ERROR_ALREADY_EXISTS
+                    os.remove(pre_lock.name)
+                    raise ExternalClashError('dot lock unavailable: %s' %
+                                             f.name)
+                else:
+                    raise
             except OSError, e:
                 if e.errno == errno.EEXIST:
                     os.remove(pre_lock.name)