[issue37571] Incorrect use of c_char_p in example code

New submission from Reuben Thomas <rrt@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@python components: Documentation messages: 347725 nosy: docs@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@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment: Thanks for the report. Looks like a valid issue to me. Would you like to create a PR? ---------- components: +ctypes nosy: +amaury.forgeotdarc, belopolsky, meador.inge, xtreak versions: +Python 3.7, Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Change by Michele Angrisano <michele.angrisano@gmail.com>: ---------- keywords: +patch pull_requests: +14518 stage: -> patch review pull_request: https://github.com/python/cpython/pull/14721 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Steve Dower <steve.dower@python.org> added the comment: 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/6b929580eb018cfef386db7f7f66b3a5853... ---------- nosy: +steve.dower _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +14557 pull_request: https://github.com/python/cpython/pull/14762 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +14558 pull_request: https://github.com/python/cpython/pull/14763 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Change by Steve Dower <steve.dower@python.org>: ---------- pull_requests: +14559 pull_request: https://github.com/python/cpython/pull/14764 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/e7c114df38eaef0cec3457f55835a2276ec... ---------- nosy: +miss-islington _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/d7caf75c73626d7df4c0628c63761738b70... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Steve Dower <steve.dower@python.org> added the comment: New changeset 68c74d05c1fdaf59d8711431884af975ac2ac5f8 by Steve Dower in branch 'master': bpo-37571: Remove extra space in ctypes docs (GH14764) https://github.com/python/cpython/commit/68c74d05c1fdaf59d8711431884af975ac2... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +14561 pull_request: https://github.com/python/cpython/pull/14766 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +14562 pull_request: https://github.com/python/cpython/pull/14767 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

Steve Dower <steve.dower@python.org> added the comment: Thanks for the patch! ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/4f733f48b48735231b79cd0f6605d3d0a2d... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________

miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/13c89f3c876363c44d35ec5f8dc374aecbc... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue37571> _______________________________________
participants (5)
-
Karthikeyan Singaravelan
-
Michele Angrisano
-
miss-islington
-
Reuben Thomas
-
Steve Dower