[New-bugs-announce] [issue37571] Incorrect use of c_char_p in example code

Reuben Thomas report at bugs.python.org
Fri Jul 12 03:40:49 EDT 2019


New submission from Reuben Thomas <rrt at sc3d.org>:

The CTypes documentation has this example:

>>> s = c_char_p()
>>> s.value = "abc def ghi"
>>> s.value
'abc def ghi'
>>> s.value is s.value
False
>>>

It appears not to have been updated since Python 2: in Python 3, you can't assign a str to a c_char_p. If one tries the example code above, one gets:

>>> s = c_char_p()
>>> s.value = "abc def ghi"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bytes or integer address expected instead of str instance

Using a bytes works:

>>> s = c_char_p()
>>> s.value = b"abc def ghi"
>>> s.value
b'abc def ghi'
>>> s.value is s.value
False
>>>

Hence adding the two "b"s is an obvious fix.

Note that the similar example with c_wchar_p does work fine with str.

----------
assignee: docs at python
components: Documentation
messages: 347725
nosy: docs at python, rrt
priority: normal
severity: normal
status: open
title: Incorrect use of c_char_p in example code
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37571>
_______________________________________


More information about the New-bugs-announce mailing list