This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ctypes: access violation reading
Type: crash Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dum.Dum, eryksun
Priority: normal Keywords:

Created on 2015-09-26 17:29 by Dum.Dum, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
error_script.py Dum.Dum, 2015-09-26 17:29 Script causing the error
Messages (4)
msg251658 - (view) Author: Dum Dum (Dum.Dum) Date: 2015-09-26 17:29
On Windows 8.1 64-bit, using a fresh installation of Python 3.5.150.0 64-bit, `error_script.py` crashes, while Python 3.4.3150 and 2.7.10150 (both 64-bit) run the script properly.

Here is a traceback: https://bpaste.net/show/3ecfbf93b96e
msg251660 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-09-26 19:10
The default function pointer restype is c_int, and the default integer argument conversion is also c_int. However, the handle returned by FindFirstVolume is a pointer to a private structure that it uses for the volume enumeration, so you must set restype to a pointer type. Similarly, if restype is a simple type that gets converted to a Python integer (e.g. wintypes.HANDLE), then you must either set FindNextVolumeW.argtypes or manually wrap the handle value (e.g. vhandle = wintypes.HANDLE(vhandle)). The default c_int conversions will only work if the address happens to fit in a 32-bit int. 

Don't forget to call FindVolumeClose if the process is expected to continue. Otherwise you're leaking memory.

Also, if you're defining function pointer prototypes for a library, please do not use ctypes.cdll or ctypes.windll. The loaders are global to ctypes and by design cache the loaded library, which by design caches function pointers. Projects such as pyreadline and colorama have demonstrated the problems that this creates due to inconsistent prototype definitions, especially for commonly used Win32 APIs.

Here is one way to rewrite your code to have it work more reliably:

    import ctypes
    from ctypes import wintypes

    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

    kernel32.FindFirstVolumeW.restype = wintypes.HANDLE
    kernel32.FindNextVolumeW.argtypes = (wintypes.HANDLE,
                                         wintypes.LPWSTR,
                                         wintypes.DWORD)
    kernel32.FindVolumeClose.argtypes = (wintypes.HANDLE,)

    INVALID_HANDLE_VALUE = wintypes.HANDLE(-1).value
    ERROR_NO_MORE_FILES = 18

    def list_volumes():
        vname = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
        vhandle = kernel32.FindFirstVolumeW(vname, len(vname))
        if vhandle == INVALID_HANDLE_VALUE:
            raise ctypes.WinError(ctypes.get_last_error())
        volumes = []
        try:
            while True:
                volumes.append(vname.value)
                if not kernel32.FindNextVolumeW(vhandle, vname, len(vname)):
                    last_error = ctypes.get_last_error()
                    if last_error == ERROR_NO_MORE_FILES:
                        break
                    else:
                        raise ctypes.WinError(last_error)
        finally:
            if not kernel32.FindVolumeClose(vhandle):
                raise ctypes.WinError(ctypes.get_last_error())
        return volumes

    if __name__ == '__main__':
        for volume in list_volumes():
            print(volume)
msg251663 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2015-09-26 21:25
Here's a rewrite with a cleaner while loop, at least to me:

    def list_volumes():
        vname = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
        vhandle = kernel32.FindFirstVolumeW(vname, len(vname))
        if vhandle == INVALID_HANDLE_VALUE:
            raise ctypes.WinError(ctypes.get_last_error())
        volumes = [vname.value]
        try:
            while kernel32.FindNextVolumeW(vhandle, vname, len(vname)):
                volumes.append(vname.value)
            last_error = ctypes.get_last_error()
            if last_error != ERROR_NO_MORE_FILES:        
                raise ctypes.WinError(last_error)
        finally:
            if not kernel32.FindVolumeClose(vhandle):
                raise ctypes.WinError(ctypes.get_last_error())
        return volumes
msg251665 - (view) Author: Dum Dum (Dum.Dum) Date: 2015-09-26 22:44
I rewrote the code following your suggestions and it appears the error was indeed on my side. I somewhat expected that, but found it weird that it only seemed to occur on 3.5. Thank you!
History
Date User Action Args
2022-04-11 14:58:21adminsetgithub: 69428
2015-09-26 22:44:01Dum.Dumsetmessages: + msg251665
2015-09-26 21:25:55eryksunsetmessages: + msg251663
2015-09-26 19:10:42eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg251660

resolution: not a bug
stage: resolved
2015-09-26 17:29:11Dum.Dumcreate