Skip to content
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

Closed
vpelletier mannequin opened this issue Jan 23, 2012 · 6 comments
Closed

create_string_buffer rejects str init_or_size parameter #58048

vpelletier mannequin opened this issue Jan 23, 2012 · 6 comments
Labels
docs Documentation in the Doc dir topic-ctypes type-bug An unexpected behavior, bug, or error

Comments

@vpelletier
Copy link
Mannequin

vpelletier mannequin commented Jan 23, 2012

BPO 13840
Nosy @birkenfeld, @meadori, @vpelletier
Files
  • issue13840.patch
  • 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:

    assignee = None
    closed_at = <Date 2012-01-26.14:50:54.820>
    created_at = <Date 2012-01-23.07:53:07.462>
    labels = ['ctypes', 'type-bug', 'docs']
    title = 'create_string_buffer rejects str init_or_size parameter'
    updated_at = <Date 2012-01-26.14:50:54.819>
    user = 'https://github.com/vpelletier'

    bugs.python.org fields:

    activity = <Date 2012-01-26.14:50:54.819>
    actor = 'meador.inge'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2012-01-26.14:50:54.820>
    closer = 'meador.inge'
    components = ['Documentation', 'ctypes']
    creation = <Date 2012-01-23.07:53:07.462>
    creator = 'vpelletier'
    dependencies = []
    files = ['24319']
    hgrepos = []
    issue_num = 13840
    keywords = ['patch']
    message_count = 6.0
    messages = ['151800', '151801', '151809', '151931', '152015', '152016']
    nosy_count = 5.0
    nosy_names = ['georg.brandl', 'meador.inge', 'docs@python', 'python-dev', 'vpelletier']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13840'
    versions = ['Python 3.2', 'Python 3.3']

    @vpelletier
    Copy link
    Mannequin Author

    vpelletier mannequin commented Jan 23, 2012

    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,
    Vincent Pelletier

    @vpelletier vpelletier mannequin added topic-ctypes type-bug An unexpected behavior, bug, or error labels Jan 23, 2012
    @birkenfeld
    Copy link
    Member

    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.

    @birkenfeld birkenfeld added docs Documentation in the Doc dir and removed topic-ctypes labels Jan 23, 2012
    @vpelletier
    Copy link
    Mannequin Author

    vpelletier mannequin commented Jan 23, 2012

    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.

    @meadori
    Copy link
    Member

    meadori commented Jan 25, 2012

    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/__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
    the current docs (i.e. do the conversions).

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 26, 2012

    New changeset be9d02536a81 by Meador Inge in branch '3.2':

    New changeset 52f68c95e025 by Meador Inge in branch 'default':

    @meadori
    Copy link
    Member

    meadori commented Jan 26, 2012

    I just fixed the docs and error message for now. I might revisit the ASCII decoding later. Thanks for the bug report Vincent.

    @meadori meadori closed this as completed Jan 26, 2012
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    docs Documentation in the Doc dir topic-ctypes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants