[New-bugs-announce] [issue26833] returning ctypes._SimpleCData objects from callbacks

Thomas report at bugs.python.org
Sat Apr 23 04:32:05 EDT 2016


New submission from Thomas:

If a callback function returns a ctypes._SimpleCData object, it will fail with a type error and complain that it expects a basic type.

Using the qsort example:

def py_cmp_func(a, b):
    print(a.contents, b.contents)
    return c_int(0)

> TypeError: an integer is required (got type c_int)
> Exception ignored in: <function py_cmp_func at 0x2439648>

This is somewhat surprising as it is totally fine to pass a c_int (or an int) as an c_int argument. But this is really an issue for subclasses of fundamental data types:

(sticking with qsort for simplicity, full example attached)

class CmpRet(c_int):
    pass

cmp_ctype = CFUNCTYPE(CmpRet, POINTER(c_int), POINTER(c_int))

def py_cmp_func(a, b):
    print(a.contents, b.contents)
    return CmpRet(0)

> TypeError: an integer is required (got type CmpRet)
> Exception ignored in: <function py_cmp_func at 0x1049cf8>

This is inconsistent with the no transparent argument/return type conversion rule for subclasses.

Consider for instance an enum with a specific underlying type. A subclass (with __eq__ on value) from the corresponding ctype can be useful to provide a typesafe way to pass / receive those from C. Due to the described behavior, this doesn't work for callbacks.

This is related to #5710, that discusses composite types.

----------
files: callback_ret_sub.py
messages: 264056
nosy: tilsche
priority: normal
severity: normal
status: open
title: returning ctypes._SimpleCData objects from callbacks
type: behavior
versions: Python 3.5
Added file: http://bugs.python.org/file42575/callback_ret_sub.py

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


More information about the New-bugs-announce mailing list