New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create_string_buffer rejects str init_or_size parameter #58048
Comments
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/__init__.py", 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> [1] http://docs.python.org/py3k/library/ctypes.html#ctypes.create_string_buffer Regards, |
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. |
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. |
The 'create_unicode_buffer' docs are currently wrong too: """ >>> 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/__init__.py", 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 |
New changeset be9d02536a81 by Meador Inge in branch '3.2':
New changeset 52f68c95e025 by Meador Inge in branch 'default':
|
I just fixed the docs and error message for now. I might revisit the ASCII decoding later. Thanks for the bug report Vincent. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: