[PATCH] consider WindowsError's behaviour to support python 2.4 on Windows

Shun-ichi Goto shunichi.goto at gmail.com
Fri Jul 12 02:15:28 UTC 2013


# HG changeset patch
# User Shun-ichi GOTO <shunichi.goto at gmail.com>
# Date 1373595282 -32400
#      Fri Jul 12 11:14:42 2013 +0900
# Node ID 35e96993ed5f97828d50515ad05a468ce00b082d
# Parent  41c06a02814e29e80f9d30c20bcfb61de7edfafa
consider WindowsError's behaviour to support python 2.4 on Windows

This change treat the ESRCH error as ENOENT like WindowsError class
does in python 2.5 or later. Without this change, some try..execpt
code which expects errno is ENOENT may fail. Actually hg command does
not work with python 2.4 on Windows.

CreateFile() will fail with error code ESRCH
when parent directory of specified path is not exist,
or ENOENT when parent directory exist but file is not exist.
Two errors are same in the mean of "file is not exist".
So WindowsError class treats error code ESRCH as ENOENT
in python 2.5 or later, but python 2.4 does not.

Actual results with python 2.4:
>>> errno.ENOENT
2
>>> errno.ESRCH
3
>>> WindowsError(3, 'msg').errno
3
>>> WindowsError(3, 'msg').args
(3, 'msg')

And with python 2.5 (or later):
>>> errno.ENOENT
2
>>> errno.ESRCH
3
>>> WindowsError(3, 'msg').errno
2
>>> WindowsError(3, 'msg').args
(3, 'msg')

Note that there is no need to fix osutil.c because it never be used
with python 2.4.

diff -r 41c06a02814e -r 35e96993ed5f mercurial/pure/osutil.py
--- a/mercurial/pure/osutil.py	Thu Jul 04 23:05:59 2013 +0900
+++ b/mercurial/pure/osutil.py	Fri Jul 12 11:14:42 2013 +0900
@@ -59,6 +59,7 @@
     posixfile = open
 else:
     import ctypes, msvcrt
+    from errno import ESRCH, ENOENT
 
     _kernel32 = ctypes.windll.kernel32
 
@@ -98,7 +99,14 @@
 
     def _raiseioerror(name):
         err = ctypes.WinError()
-        raise IOError(err.errno, '%s: %s' % (name, err.strerror))
+        # For python 2.4, treat ESRCH as ENOENT like WindowsError does
+        # in python 2.5 or later.
+        # py24:           WindowsError(3, '').errno => 3
+        # py25 or later:  WindowsError(3, '').errno => 2
+        errno = err.errno
+        if errno == ESRCH:
+            errno = ENOENT
+        raise IOError(errno, '%s: %s' % (name, err.strerror))
 
     class posixfile(object):
         '''a file object aiming for POSIX-like semantics



More information about the Mercurial-devel mailing list