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.

classification
Title: Incorrect use of c_char_p in example code
Type: Stage: resolved
Components: ctypes, Documentation Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: amaury.forgeotdarc, belopolsky, docs@python, meador.inge, miss-islington, rrt, steve.dower, xtreak
Priority: normal Keywords: patch

Created on 2019-07-12 07:40 by rrt, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 14721 merged mangrisano, 2019-07-12 19:05
PR 14762 merged miss-islington, 2019-07-14 07:55
PR 14763 merged miss-islington, 2019-07-14 07:55
PR 14764 merged steve.dower, 2019-07-14 07:59
PR 14766 merged miss-islington, 2019-07-14 08:10
PR 14767 merged miss-islington, 2019-07-14 08:10
Messages (9)
msg347725 - (view) Author: Reuben Thomas (rrt) Date: 2019-07-12 07:40
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.
msg347727 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-07-12 07:57
Thanks for the report. Looks like a valid issue to me. Would you like to create a PR?
msg347873 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-07-14 07:55
New changeset 6b929580eb018cfef386db7f7f66b3a58532eada by Steve Dower (Michele Angrisano) in branch 'master':
bpo-37571: Add 'b' to prevent the TypeError exception. (GH-14721)
https://github.com/python/cpython/commit/6b929580eb018cfef386db7f7f66b3a58532eada
msg347875 - (view) Author: miss-islington (miss-islington) Date: 2019-07-14 08:07
New changeset e7c114df38eaef0cec3457f55835a2276eccbff6 by Miss Islington (bot) in branch '3.8':
bpo-37571: Add 'b' to prevent the TypeError exception. (GH-14721)
https://github.com/python/cpython/commit/e7c114df38eaef0cec3457f55835a2276eccbff6
msg347876 - (view) Author: miss-islington (miss-islington) Date: 2019-07-14 08:08
New changeset d7caf75c73626d7df4c0628c63761738b7063463 by Miss Islington (bot) in branch '3.7':
bpo-37571: Add 'b' to prevent the TypeError exception. (GH-14721)
https://github.com/python/cpython/commit/d7caf75c73626d7df4c0628c63761738b7063463
msg347878 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-07-14 08:09
New changeset 68c74d05c1fdaf59d8711431884af975ac2ac5f8 by Steve Dower in branch 'master':
bpo-37571: Remove extra space in ctypes docs (GH14764)
https://github.com/python/cpython/commit/68c74d05c1fdaf59d8711431884af975ac2ac5f8
msg347879 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-07-14 08:14
Thanks for the patch!
msg347882 - (view) Author: miss-islington (miss-islington) Date: 2019-07-14 08:22
New changeset 4f733f48b48735231b79cd0f6605d3d0a2d5b511 by Miss Islington (bot) in branch '3.8':
bpo-37571: Remove extra space in ctypes docs (GH14764)
https://github.com/python/cpython/commit/4f733f48b48735231b79cd0f6605d3d0a2d5b511
msg347884 - (view) Author: miss-islington (miss-islington) Date: 2019-07-14 08:24
New changeset 13c89f3c876363c44d35ec5f8dc374aecbca62a1 by Miss Islington (bot) in branch '3.7':
bpo-37571: Remove extra space in ctypes docs (GH14764)
https://github.com/python/cpython/commit/13c89f3c876363c44d35ec5f8dc374aecbca62a1
History
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81752
2019-07-14 08:24:38miss-islingtonsetmessages: + msg347884
2019-07-14 08:23:00miss-islingtonsetmessages: + msg347882
2019-07-14 08:14:38steve.dowersetstatus: open -> closed
resolution: fixed
messages: + msg347879

stage: patch review -> resolved
2019-07-14 08:10:13miss-islingtonsetpull_requests: + pull_request14562
2019-07-14 08:10:06miss-islingtonsetpull_requests: + pull_request14561
2019-07-14 08:09:47steve.dowersetmessages: + msg347878
2019-07-14 08:08:51miss-islingtonsetmessages: + msg347876
2019-07-14 08:07:09miss-islingtonsetnosy: + miss-islington
messages: + msg347875
2019-07-14 07:59:14steve.dowersetpull_requests: + pull_request14559
2019-07-14 07:55:28miss-islingtonsetpull_requests: + pull_request14558
2019-07-14 07:55:22miss-islingtonsetpull_requests: + pull_request14557
2019-07-14 07:55:14steve.dowersetnosy: + steve.dower
messages: + msg347873
2019-07-12 19:05:09mangrisanosetkeywords: + patch
stage: patch review
pull_requests: + pull_request14518
2019-07-12 07:57:59xtreaksetversions: + Python 3.7, Python 3.8
nosy: + amaury.forgeotdarc, belopolsky, meador.inge, xtreak

messages: + msg347727

components: + ctypes
2019-07-12 07:40:49rrtcreate