summaryrefslogtreecommitdiff
path: root/mercurial/pure/osutil.py
diff options
context:
space:
mode:
Diffstat (limited to 'mercurial/pure/osutil.py')
-rw-r--r--mercurial/pure/osutil.py21
1 files changed, 18 insertions, 3 deletions
diff --git a/mercurial/pure/osutil.py b/mercurial/pure/osutil.py
index 2476bd6..28bbbc5 100644
--- a/mercurial/pure/osutil.py
+++ b/mercurial/pure/osutil.py
@@ -58,7 +58,7 @@ def listdir(path, stat=False, skip=None):
if os.name != 'nt':
posixfile = open
else:
- import ctypes, msvcrt
+ import ctypes, ctypes.util
_kernel32 = ctypes.windll.kernel32
@@ -68,6 +68,15 @@ else:
_INVALID_HANDLE_VALUE = _HANDLE(-1).value
+ def _crtname():
+ try:
+ # find_msvcrt was introduced in Python 2.6
+ return ctypes.util.find_msvcrt()
+ except AttributeError:
+ return 'msvcr71.dll' # CPython 2.5 and 2.4
+
+ _crt = ctypes.PyDLL(_crtname())
+
# CreateFile
_FILE_SHARE_READ = 0x00000001
_FILE_SHARE_WRITE = 0x00000002
@@ -96,6 +105,9 @@ else:
_DWORD, _DWORD, _HANDLE]
_kernel32.CreateFileA.restype = _HANDLE
+ _crt._open_osfhandle.argtypes = [_HANDLE, ctypes.c_int]
+ _crt._open_osfhandle.restype = ctypes.c_int
+
def _raiseioerror(name):
err = ctypes.WinError()
raise IOError(err.errno, '%s: %s' % (name, err.strerror))
@@ -119,7 +131,7 @@ else:
flags = _O_TEXT
m0 = mode[0]
- if m0 == 'r' and '+' not in mode:
+ if m0 == 'r' and not '+' in mode:
flags |= _O_RDONLY
access = _GENERIC_READ
else:
@@ -144,7 +156,10 @@ else:
if fh == _INVALID_HANDLE_VALUE:
_raiseioerror(name)
- fd = msvcrt.open_osfhandle(fh, flags)
+ # for CPython we must use the same CRT as Python uses,
+ # or the os.fdopen call below will abort with
+ # "OSError: [Errno 9] Bad file descriptor"
+ fd = _crt._open_osfhandle(fh, flags)
if fd == -1:
_kernel32.CloseHandle(fh)
_raiseioerror(name)