[PATCH Preview 1] implement posixfile for Windows using ctypes

Adrian Buehlmann adrian at cadifra.com
Sat May 14 10:19:26 CDT 2011


On 2011-05-14 12:43, Adrian Buehlmann wrote:
> On 2011-05-14 01:56, Adrian Buehlmann wrote:
>> # HG changeset patch
>> # User Adrian Buehlmann <adrian at cadifra.com>
>> # Date 1305284858 -7200
>> # Node ID e8c58e3dfee0e83e329cbdc809c46bb8729767da
>> # Parent  1f46be4689ed5692f041bc5856aa6d11f6f38373
>> implement posixfile for Windows using ctypes
>>
>> obsoletes the C implementation in osutil.c
>>
> 
> after having replaced the 'is' operators with '==', I got this almost working
> on pypy
> 
> $ hg diff
> diff --git a/mercurial/win32.py b/mercurial/win32.py
> --- a/mercurial/win32.py
> +++ b/mercurial/win32.py
> @@ -420,7 +420,7 @@
>          else:
>              flags = _O_TEXT
> 
> -        if m0 is 'r' and not '+' in mode:
> +        if m0 == 'r' and not '+' in mode:
>              flags |= _O_RDONLY
>              access = _GENERIC_READ
>          else:
> @@ -429,11 +429,11 @@
>              flags |= _O_RDWR
>              access = _GENERIC_READ | _GENERIC_WRITE
> 
> -        if m0 is 'r':
> +        if m0 == 'r':
>              creation = _OPEN_EXISTING
> -        elif m0 is 'w':
> +        elif m0 == 'w':
>              creation = _CREATE_ALWAYS
> -        elif m0 is 'a':
> +        elif m0 == 'a':
>              creation = _OPEN_ALWAYS
>              flags |= _O_APPEND
>          else:
> 
> 
> $ set PYTHONPATH=C:\Users\adi\hgrepos\hg-crew
> 
> $ C:\pypy\pypy.exe
> Python 2.7.1 (aefc70438132+, Apr 29 2011, 12:45:42)
> [PyPy 1.5.0-alpha0 with MSC v.1600 32 bit] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> And now for something completely different: ``casuality violations and flying''
>>>>> from mercurial import win32
>>>>> posixfile = win32.posixfile
>>>>> f = posixfile('foo.txt', 'w')
> C:\Users\adi\hgrepos\hg-crew\mercurial\win32.py:444: RuntimeWarning: C function without declared arguments called
>   None, creation, _FILE_ATTRIBUTE_NORMAL, None)
> C:\Users\adi\hgrepos\hg-crew\mercurial\win32.py:444: RuntimeWarning: C function without declared return type called
>   None, creation, _FILE_ATTRIBUTE_NORMAL, None)
> C:\Users\adi\hgrepos\hg-crew\mercurial\win32.py:451: RuntimeWarning: C function without declared arguments called
>   fd = _crt._open_osfhandle(fh, flags)
> C:\Users\adi\hgrepos\hg-crew\mercurial\win32.py:451: RuntimeWarning: C function without declared return type called
>   fd = _crt._open_osfhandle(fh, flags)
>>>>> f.write('hello pypy')
>>>>> f.close()
>>>>> print posixfile('foo.txt').read()
> hello pypy
> 
> 
> Now, I'm puzzled by these warnings...

Interesting.

pypy wants to see argtypes and restype to be defined [1][2]

If I change the call

        fh = _kernel32.CreateFileA(name, access,
                _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
                None, creation, _FILE_ATTRIBUTE_NORMAL, None)

to

        _kernel32.CreateFileA.argtypes = [_LPCSTR, _DWORD, _DWORD,
            ctypes.c_void_p, _DWORD, _DWORD, _HANDLE]
        _kernel32.CreateFileA.restype = _HANDLE
        fh = _kernel32.CreateFileA(name, access,
                _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
                None, creation, _FILE_ATTRIBUTE_NORMAL, None)

pypy stops complaining about the CreateFileA call.

CPython is also happy with that (at least it doesn't complain and the test
use case still works).

Looks like I got all the puzzle pieces together now. \o/

[1] http://docs.python.org/library/ctypes.html#specifying-the-required-argument-types-function-prototypes
[2] http://docs.python.org/library/ctypes.html#return-types



More information about the Mercurial-devel mailing list