[New-bugs-announce] [issue27803] ctypes automatic byref failing on custom classes attributes

Erwan Le Pape report at bugs.python.org
Fri Aug 19 14:11:37 EDT 2016


New submission from Erwan Le Pape:

When using a custom class to store a ctype value, passing that class as a function argument explicitly declared to be a pointer type fails to pass the _as_parameter_ class attribute as a pointer and instead raises a TypeError.

For example:
>>> from ctypes import *
>>> from ctypes.wintypes import *
>>>
>>> class CustomPHKEY(object):
...     def __init__(self, value):
...             self._as_parameter_ = HKEY(value)
...
>>>
>>> function = windll.function
>>> function.argtypes = [POINTER(HKEY)]
>>> function.restype = LONG
>>> result = CustomPHKEY(0)
>>> function(result)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_c_void_p instance instead of c_void_p

Shouldn't ctypes apply the required byref() conversion automatically? Or is this behavior normal and automatic byref() only concerns native ctypes types?

I only flagged Python 3.5 and Python 2.7 since they are the only ones I explicitly tested this on but I suspect other versions are affected.

----------
components: ctypes
messages: 273149
nosy: amaury.forgeotdarc, belopolsky, lepaperwan, meador.inge
priority: normal
severity: normal
status: open
title: ctypes automatic byref failing on custom classes attributes
type: behavior
versions: Python 2.7, Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27803>
_______________________________________


More information about the New-bugs-announce mailing list