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.

Author meador.inge
Recipients PeterASilva, amaury.forgeotdarc, belopolsky, meador.inge, vladris
Date 2011-09-03.05:04:03
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1315026244.52.0.00780968473244.issue5149@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2011-09-03 05:04:04meador.ingesetrecipients: + meador.inge, amaury.forgeotdarc, belopolsky, PeterASilva, vladris
2011-09-03 05:04:04meador.ingesetmessageid: <1315026244.52.0.00780968473244.issue5149@psf.upfronthosting.co.za>
2011-09-03 05:04:03meador.ingelinkissue5149 messages
2011-09-03 05:04:03meador.ingecreate