Title: create_string_buffer rejects str init_or_size parameter
Type: behavior Stage: resolved
Components: ctypes, Documentation Versions: Python 3.2, Python 3.3
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, georg.brandl, meador.inge, python-dev, vpelletier
Priority: normal Keywords: patch

Created on 2012-01-23 07:53 by vpelletier, last changed 2012-01-26 14:50 by meador.inge. This issue is now closed.

File name Uploaded Description Edit
issue13840.patch meador.inge, 2012-01-25 03:14 review
Messages (6)
msg151800 - (view) Author: Vincent Pelletier (vpelletier) Date: 2012-01-23 07:53
ctypes.create_string_buffer documentation[1] says init_or_size parameter should accept a string. As of 3.2, it raises:
>>> import ctypes
>>> ctypes.create_string_buffer('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.2/ctypes/", line 59, in create_string_buffer
    buf.value = init
TypeError: str/bytes expected instead of str instance

It works fine as of 2.7 (and very probably any 2.x up to ctypes introduction):
>>> import ctypes
>>> ctypes.create_string_buffer('foo')
<ctypes.c_char_Array_4 object at 0x7fbdcb8b95f0>


Vincent Pelletier
msg151801 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-23 07:58
It should only take bytes; "str" is Unicode in 3.x.

So the docs and the error message are wrong, the behavior is correct.  Reclassifying as a docs issue.
msg151809 - (view) Author: Vincent Pelletier (vpelletier) Date: 2012-01-23 11:20
Thanks for the quick reply.

FWIW, in 2.7 doc ctype.create_string_buffer is said to accept unicode objects as parameter. I don't use this personally, so I don't mind 3.x only working on bytes - and already fixed my code accordingly. It's just that I noticed this after your answer. Also, I didn't try to confirm if it actually works.
msg151931 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-01-25 03:14
The 'create_unicode_buffer' docs are currently wrong too:

If the first parameter is a bytes object, it is converted into an 
unicode string according to ctypes conversion rules.

>>> ctypes.create_unicode_buffer(b'foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/meadori/src/python/cpython/Lib/ctypes/", line 294, in create_unicode_buffer
    buf.value = init
TypeError: unicode string expected instead of bytes instance

The attached patch fixes the documentation and exception messages.

Although, it might be more friendly to implement things according to
the current docs (i.e. do the conversions).
msg152015 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-01-26 14:49
New changeset be9d02536a81 by Meador Inge in branch '3.2':
- Issue #13840: Fix ctypes.create_string_buffer exception message and docs.

New changeset 52f68c95e025 by Meador Inge in branch 'default':
- Issue #13840: Fix ctypes.create_string_buffer exception message and docs.
msg152016 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-01-26 14:50
I just fixed the docs and error message for now.  I might revisit the ASCII decoding later.  Thanks for the bug report Vincent.
Date User Action Args
2012-01-26 14:50:54meador.ingesetstatus: open -> closed
resolution: fixed
messages: + msg152016

stage: patch review -> resolved
2012-01-26 14:49:36python-devsetnosy: + python-dev
messages: + msg152015
2012-01-25 03:14:25meador.ingesetfiles: + issue13840.patch
versions: + Python 3.3
messages: + msg151931

components: + ctypes
keywords: + patch
stage: patch review
2012-01-24 19:23:15meador.ingesetnosy: + meador.inge
2012-01-23 11:20:09vpelletiersetmessages: + msg151809
2012-01-23 07:58:50georg.brandlsetnosy: + georg.brandl, docs@python
messages: + msg151801

assignee: docs@python
components: + Documentation, - ctypes
2012-01-23 07:53:07vpelletiercreate