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: ctypes: c_char now uses bytes and not str (unicode)
Type: Stage:
Components: Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: theller Nosy List: theller, vstinner
Priority: normal Keywords: patch

Created on 2007-08-08 19:51 by vstinner, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
py3k-struni-ctypes.diff vstinner, 2007-08-08 19:51 c_get calls PyBytes_FromStringAndSize() and not PyUnicode_FromStringAndSize()
Messages (2)
msg53012 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2007-08-08 19:51
Hi,

I hear Guido's request to fix last py3k-struni bugs. I downloaded subversion trunk and started to work on ctypes tests.

The problem is that ctypes c_char (and c_char_p) creates unicode string instead of byte string. I attached a proposition (patch) to change this behaviour (use bytes for c_char).

So in next example, it will display 'bytes' and not 'str':
  from ctypes import c_buffer, c_char
  buf = c_buffer("abcdef")
  print (type(buf[0]))

Other behaviour changes:
 - repr(c_char) adds a "b". Eg. repr(c_char('x')) is "c_char(b'x')" instead of "c_char('x')"
 - bytes is mutable whereas str is not: this may break some modules based on ctypes

Victor Stinner aka haypo
msg53013 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2007-08-08 19:54
Ooops, Thomas Heller already commited it (r56838).
History
Date User Action Args
2022-04-11 14:56:25adminsetgithub: 45290
2008-01-06 22:29:45adminsetkeywords: - py3k
versions: + Python 3.0
2007-08-08 19:51:12vstinnercreate