[PATCH] windows: fix use of undefined exception (issue1707)

Matt Mackall mpm at selenic.com
Thu Jun 25 12:38:24 CDT 2009


On Thu, 2009-06-25 at 17:11 +0000, Henrik Stuart wrote:
> # HG changeset patch
> # User Henrik Stuart <hg at hstuart.dk>
> # Date 1245948386 -7200
> # Node ID 0823bdb69ce6b06d2aeea599af0f5fae2d588332
> # Parent  bc9ac7a29a737d7b99289598d376fb0b03d2b6c2
> windows: fix use of undefined exception (issue1707)
> 
> This fixes the implied reliance on pywin32 and the win32 module. This
> also fixes a regression in ae275ad46bd0 that made Mercurial unusable
> without pywin32.
> 
> The exception classes are needed to wrap the buffered posixfile from
> osutil in normal IOError semantics, otherwise several uses of the file
> opener fails in unexpected ways making it impossible to operate on
> repositories.
> 
> The error constants are introduced as needed from pywin32's winerror
> module.

But are they actually needed at all? The Python docs -claim- that
WindowsError has an appropriate errno.h set in errno? Can't we do
something like this:

diff -r 7b3d837ca60e mercurial/win32.py
--- a/mercurial/win32.py	Thu Jun 25 09:23:33 2009 -0400
+++ b/mercurial/win32.py	Thu Jun 25 12:37:44 2009 -0500
@@ -20,139 +20,6 @@
 import osutil, encoding
 from win32com.shell import shell,shellcon
 
-class WinError(Exception):
-    winerror_map = {
-        winerror.ERROR_ACCESS_DENIED: errno.EACCES,
-        winerror.ERROR_ACCOUNT_DISABLED: errno.EACCES,
-        winerror.ERROR_ACCOUNT_RESTRICTION: errno.EACCES,
-        winerror.ERROR_ALREADY_ASSIGNED: errno.EBUSY,
-        winerror.ERROR_ALREADY_EXISTS: errno.EEXIST,
-        winerror.ERROR_ARITHMETIC_OVERFLOW: errno.ERANGE,
-        winerror.ERROR_BAD_COMMAND: errno.EIO,
-        winerror.ERROR_BAD_DEVICE: errno.ENODEV,
-        winerror.ERROR_BAD_DRIVER_LEVEL: errno.ENXIO,
-        winerror.ERROR_BAD_EXE_FORMAT: errno.ENOEXEC,
-        winerror.ERROR_BAD_FORMAT: errno.ENOEXEC,
-        winerror.ERROR_BAD_LENGTH: errno.EINVAL,
-        winerror.ERROR_BAD_PATHNAME: errno.ENOENT,
-        winerror.ERROR_BAD_PIPE: errno.EPIPE,
-        winerror.ERROR_BAD_UNIT: errno.ENODEV,
-        winerror.ERROR_BAD_USERNAME: errno.EINVAL,
-        winerror.ERROR_BROKEN_PIPE: errno.EPIPE,
-        winerror.ERROR_BUFFER_OVERFLOW: errno.ENAMETOOLONG,
-        winerror.ERROR_BUSY: errno.EBUSY,
-        winerror.ERROR_BUSY_DRIVE: errno.EBUSY,
-        winerror.ERROR_CALL_NOT_IMPLEMENTED: errno.ENOSYS,
-        winerror.ERROR_CANNOT_MAKE: errno.EACCES,
-        winerror.ERROR_CANTOPEN: errno.EIO,
-        winerror.ERROR_CANTREAD: errno.EIO,
-        winerror.ERROR_CANTWRITE: errno.EIO,
-        winerror.ERROR_CRC: errno.EIO,
-        winerror.ERROR_CURRENT_DIRECTORY: errno.EACCES,
-        winerror.ERROR_DEVICE_IN_USE: errno.EBUSY,
-        winerror.ERROR_DEV_NOT_EXIST: errno.ENODEV,
-        winerror.ERROR_DIRECTORY: errno.EINVAL,
-        winerror.ERROR_DIR_NOT_EMPTY: errno.ENOTEMPTY,
-        winerror.ERROR_DISK_CHANGE: errno.EIO,
-        winerror.ERROR_DISK_FULL: errno.ENOSPC,
-        winerror.ERROR_DRIVE_LOCKED: errno.EBUSY,
-        winerror.ERROR_ENVVAR_NOT_FOUND: errno.EINVAL,
-        winerror.ERROR_EXE_MARKED_INVALID: errno.ENOEXEC,
-        winerror.ERROR_FILENAME_EXCED_RANGE: errno.ENAMETOOLONG,
-        winerror.ERROR_FILE_EXISTS: errno.EEXIST,
-        winerror.ERROR_FILE_INVALID: errno.ENODEV,
-        winerror.ERROR_FILE_NOT_FOUND: errno.ENOENT,
-        winerror.ERROR_GEN_FAILURE: errno.EIO,
-        winerror.ERROR_HANDLE_DISK_FULL: errno.ENOSPC,
-        winerror.ERROR_INSUFFICIENT_BUFFER: errno.ENOMEM,
-        winerror.ERROR_INVALID_ACCESS: errno.EACCES,
-        winerror.ERROR_INVALID_ADDRESS: errno.EFAULT,
-        winerror.ERROR_INVALID_BLOCK: errno.EFAULT,
-        winerror.ERROR_INVALID_DATA: errno.EINVAL,
-        winerror.ERROR_INVALID_DRIVE: errno.ENODEV,
-        winerror.ERROR_INVALID_EXE_SIGNATURE: errno.ENOEXEC,
-        winerror.ERROR_INVALID_FLAGS: errno.EINVAL,
-        winerror.ERROR_INVALID_FUNCTION: errno.ENOSYS,
-        winerror.ERROR_INVALID_HANDLE: errno.EBADF,
-        winerror.ERROR_INVALID_LOGON_HOURS: errno.EACCES,
-        winerror.ERROR_INVALID_NAME: errno.EINVAL,
-        winerror.ERROR_INVALID_OWNER: errno.EINVAL,
-        winerror.ERROR_INVALID_PARAMETER: errno.EINVAL,
-        winerror.ERROR_INVALID_PASSWORD: errno.EPERM,
-        winerror.ERROR_INVALID_PRIMARY_GROUP: errno.EINVAL,
-        winerror.ERROR_INVALID_SIGNAL_NUMBER: errno.EINVAL,
-        winerror.ERROR_INVALID_TARGET_HANDLE: errno.EIO,
-        winerror.ERROR_INVALID_WORKSTATION: errno.EACCES,
-        winerror.ERROR_IO_DEVICE: errno.EIO,
-        winerror.ERROR_IO_INCOMPLETE: errno.EINTR,
-        winerror.ERROR_LOCKED: errno.EBUSY,
-        winerror.ERROR_LOCK_VIOLATION: errno.EACCES,
-        winerror.ERROR_LOGON_FAILURE: errno.EACCES,
-        winerror.ERROR_MAPPED_ALIGNMENT: errno.EINVAL,
-        winerror.ERROR_META_EXPANSION_TOO_LONG: errno.E2BIG,
-        winerror.ERROR_MORE_DATA: errno.EPIPE,
-        winerror.ERROR_NEGATIVE_SEEK: errno.ESPIPE,
-        winerror.ERROR_NOACCESS: errno.EFAULT,
-        winerror.ERROR_NONE_MAPPED: errno.EINVAL,
-        winerror.ERROR_NOT_ENOUGH_MEMORY: errno.ENOMEM,
-        winerror.ERROR_NOT_READY: errno.EAGAIN,
-        winerror.ERROR_NOT_SAME_DEVICE: errno.EXDEV,
-        winerror.ERROR_NO_DATA: errno.EPIPE,
-        winerror.ERROR_NO_MORE_SEARCH_HANDLES: errno.EIO,
-        winerror.ERROR_NO_PROC_SLOTS: errno.EAGAIN,
-        winerror.ERROR_NO_SUCH_PRIVILEGE: errno.EACCES,
-        winerror.ERROR_OPEN_FAILED: errno.EIO,
-        winerror.ERROR_OPEN_FILES: errno.EBUSY,
-        winerror.ERROR_OPERATION_ABORTED: errno.EINTR,
-        winerror.ERROR_OUTOFMEMORY: errno.ENOMEM,
-        winerror.ERROR_PASSWORD_EXPIRED: errno.EACCES,
-        winerror.ERROR_PATH_BUSY: errno.EBUSY,
-        winerror.ERROR_PATH_NOT_FOUND: errno.ENOENT,
-        winerror.ERROR_PIPE_BUSY: errno.EBUSY,
-        winerror.ERROR_PIPE_CONNECTED: errno.EPIPE,
-        winerror.ERROR_PIPE_LISTENING: errno.EPIPE,
-        winerror.ERROR_PIPE_NOT_CONNECTED: errno.EPIPE,
-        winerror.ERROR_PRIVILEGE_NOT_HELD: errno.EACCES,
-        winerror.ERROR_READ_FAULT: errno.EIO,
-        winerror.ERROR_SEEK: errno.EIO,
-        winerror.ERROR_SEEK_ON_DEVICE: errno.ESPIPE,
-        winerror.ERROR_SHARING_BUFFER_EXCEEDED: errno.ENFILE,
-        winerror.ERROR_SHARING_VIOLATION: errno.EACCES,
-        winerror.ERROR_STACK_OVERFLOW: errno.ENOMEM,
-        winerror.ERROR_SWAPERROR: errno.ENOENT,
-        winerror.ERROR_TOO_MANY_MODULES: errno.EMFILE,
-        winerror.ERROR_TOO_MANY_OPEN_FILES: errno.EMFILE,
-        winerror.ERROR_UNRECOGNIZED_MEDIA: errno.ENXIO,
-        winerror.ERROR_UNRECOGNIZED_VOLUME: errno.ENODEV,
-        winerror.ERROR_WAIT_NO_CHILDREN: errno.ECHILD,
-        winerror.ERROR_WRITE_FAULT: errno.EIO,
-        winerror.ERROR_WRITE_PROTECT: errno.EROFS,
-        }
-
-    def __init__(self, err):
-        try:
-            # unpack a pywintypes.error tuple
-            self.win_errno, self.win_function, self.win_strerror = err
-        except ValueError:
-            # get attributes from a WindowsError
-            self.win_errno = err.winerror
-            self.win_function = None
-            self.win_strerror = err.strerror
-        self.win_strerror = self.win_strerror.rstrip('.')
-
-class WinIOError(WinError, IOError):
-    def __init__(self, err, filename=None):
-        WinError.__init__(self, err)
-        IOError.__init__(self, self.winerror_map.get(self.win_errno, 0),
-                         self.win_strerror)
-        self.filename = filename
-
-class WinOSError(WinError, OSError):
-    def __init__(self, err):
-        WinError.__init__(self, err)
-        OSError.__init__(self, self.winerror_map.get(self.win_errno, 0),
-                         self.win_strerror)
-
 def os_link(src, dst):
     try:
         win32file.CreateHardLink(dst, src)
@@ -164,12 +31,10 @@
             except:
                 pass
             # Fake hardlinking error
-            raise WinOSError((18, 'CreateHardLink', 'The system cannot '
-                              'move the file to a different disk drive'))
-    except pywintypes.error, details:
-        raise WinOSError(details)
+            raise OSError(errno.EINVAL,
+                          'target implements hardlinks improperly'))
     except NotImplementedError: # Another fake error win Win98
-        raise WinOSError((18, 'CreateHardLink', 'Hardlinking not supported'))
+        raise OSError(errno.EINVAL, 'Hardlinking not supported')
 
 def nlinks(pathname):
     """Return number of hardlinks for the given file."""
diff -r 7b3d837ca60e mercurial/windows.py
--- a/mercurial/windows.py	Thu Jun 25 09:23:33 2009 -0400
+++ b/mercurial/windows.py	Thu Jun 25 12:37:44 2009 -0500
@@ -8,7 +8,6 @@
 from i18n import _
 import osutil, error
 import errno, msvcrt, os, re, sys
-from mercurial import win32
 
 nulldev = 'NUL:'
 umask = 002
@@ -18,7 +17,8 @@
     try:
         return osutil.posixfile(name, mode, buffering)
     except WindowsError, err:
-        raise win32.WinIOError(err)
+        # convert from an OSError-derived exception to an IOError
+        raise IOError(err.errno, err.strerror)
 posixfile.__doc__ = osutil.posixfile.__doc__
 
 class winstdout(object):




-- 
http://selenic.com : development and support for Mercurial and Linux




More information about the Mercurial-devel mailing list