Mailing List Archive

[issue5149] syntactic sugar: type coercion on pointer assignment
New submission from Peter A Silva <Peter.A.Silva@gmail.com>:

tough% cat toy.py

from ctypes import *

class foo(Structure):
_fields_ = [ ( "bar", c_char_p ) ]

foofoo = foo()

babar = create_string_buffer(32)

foofoo.bar = babar

tough% python toy.py
Traceback (most recent call last):
File "toy.py", line 11, in <module>
foofoo.bar = babar
TypeError: incompatible types, c_char_Array_32 instance instead of
c_char_p instance
tough%


would be nice if ctypes automagically turned the assignment into

foofoo.bar = cast(babar,c_char_p)

There doesn't seem to be any other possible intent of this syntax.
Color me stupid, but being new to ctypes, it took me approximately 30
hours to discover a method of such assignment that worked (babar.raw is
not rejected, but not correct, debugging that was fun. I actually fell
upon addressof(babar), which does also works, but is probably
undesirable.)

It would be very friendly if ctypes would just do the right thing.
It doesn't seem like there is any other possible intent of such
a syntax.

----------
assignee: theller
components: ctypes
messages: 81123
nosy: PeterASilva, theller
severity: normal
status: open
title: syntactic sugar: type coercion on pointer assignment
versions: Python 2.6, Python 2.7, Python 3.0, Python 3.1

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5149>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com