[issue5149] syntactic sugar: type coercion on pointer assignment

Meador Inge report at bugs.python.org
Sat Sep 3 07:04:04 CEST 2011


Meador Inge <meadori at gmail.com> added the comment:

This is busted for plain old assignment too:

Python 3.3.0a0 (default:6374b4ffe00c, Sep  2 2011, 23:50:39) 
[GCC 4.6.0 20110603 (Red Hat 4.6.0-10)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> buff = create_string_buffer(b'foo')
>>> p = c_char_p()
>>> p.value = addressof(buff)
>>> print(p.value)
b'foo'
>>> p.value = buff.value
>>> print(p.value)
b'foo'
>>> p.value = buff
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string or integer address expected instead of c_char_Array_4 instance

I think having the conversion is an entirely reasonable request.  It is the equivalent of:

    char buff[128] = "foo";
    char *p = buff;

in C.  I imagine a lot of C programmers would expect this behavior.

Also, 'ctypes' already does this type of conversion for function parameters.  Consider a function exported from a shared library 'libfoo' called 'print_string':

void print_string (char *str)
{
  printf ("%s\n", str);
}

The following all work fine:

>>> libfoo = CDLL("./libfoo.so.1.0")
>>> buff = create_string_buffer("foo")
>>> libfoo.print_string(buff)
foo
>>> libfoo.print_string("foo")
foo
>>> libfoo.print_string(addressof(buff))
foo

I am working on a patch for this.  I will post it soon.

----------
assignee: theller -> 
nosy: +amaury.forgeotdarc, belopolsky, meadori -theller
stage: test needed -> needs patch

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


More information about the Python-bugs-list mailing list