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

The result type of bytearray formatting is not stable #70953

Closed
berkerpeksag opened this issue Apr 15, 2016 · 15 comments
Closed

The result type of bytearray formatting is not stable #70953

berkerpeksag opened this issue Apr 15, 2016 · 15 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@berkerpeksag
Copy link
Member

BPO 26766
Nosy @vstinner, @ethanfurman, @berkerpeksag, @serhiy-storchaka
Files
  • bytearray_mod.diff
  • writer_bytearray_mod.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 2016-04-15.22:20:34.426>
    created_at = <Date 2016-04-15.09:43:21.037>
    labels = ['interpreter-core', 'type-bug']
    title = 'The result type of bytearray formatting is not stable'
    updated_at = <Date 2016-04-15.22:20:34.425>
    user = 'https://github.com/berkerpeksag'

    bugs.python.org fields:

    activity = <Date 2016-04-15.22:20:34.425>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-04-15.22:20:34.426>
    closer = 'berker.peksag'
    components = ['Interpreter Core']
    creation = <Date 2016-04-15.09:43:21.037>
    creator = 'berker.peksag'
    dependencies = []
    files = ['42466', '42473']
    hgrepos = []
    issue_num = 26766
    keywords = ['patch']
    message_count = 15.0
    messages = ['263462', '263465', '263469', '263473', '263477', '263485', '263486', '263492', '263496', '263500', '263502', '263503', '263504', '263529', '263530']
    nosy_count = 5.0
    nosy_names = ['vstinner', 'ethan.furman', 'python-dev', 'berker.peksag', 'serhiy.storchaka']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26766'
    versions = ['Python 3.6']

    @berkerpeksag
    Copy link
    Member Author

    I noticed this while looking at bpo-26764.

    bytearray_mod() and bytearray_format() both have checks for PyByteArray_Check(v). The check in bytearray_format() looks redundant to me.

    Here is a patch.

    @berkerpeksag berkerpeksag added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Apr 15, 2016
    @serhiy-storchaka
    Copy link
    Member

    Yes, I noticed this, but left for future (post-issue26765) refactoring.

    The patch LGTM, but notice Victor's comment to bpo-26764.

    @serhiy-storchaka
    Copy link
    Member

    And may be bytearray_mod in 3.6 is not correct. In 3.5 it returns bytearray, in 3.6 it returns bytes.

    @berkerpeksag
    Copy link
    Member Author

    Do you have an example code? It returns bytearray for me in both 3.5 and 3.6. use_bytearray parameter of _PyBytes_FormatEx() is 1 in bytearray_mod().

    @serhiy-storchaka
    Copy link
    Member

    Python 3.5:

    >>> bytearray(b'%d') % 42
    bytearray(b'42')

    Python 3.6:

    >>> bytearray(b'%d') % 42
    b'42'

    @berkerpeksag
    Copy link
    Member Author

    Thanks! More examples:

    Python 3.6:

    >>> bytearray(b'hello %b') % b"world"
    bytearray(b'hello world')
    >>> bytearray(b'hello %b') % b"wor"
    b'hello wor'

    Python 3.5:

    >>> bytearray(b'hello %b') % b"world"
    bytearray(b'hello world')
    >>> bytearray(b'hello %b') % b"wor"
    bytearray(b'hello wor')

    @serhiy-storchaka
    Copy link
    Member

    Hmm, looks the result type is very unstable.

    On release build:

    >>> bytearray(b'hello %b') % b"world"
    b'hello world'
    >>> bytearray(b'hello %b') % b"wor"
    b'hello wor'

    On debug build:

    >>> bytearray(b'hello %b') % b"world"
    bytearray(b'hello world')
    >>> bytearray(b'hello %b') % b"wor"
    b'hello wor'

    And current test_bytes.py is passed on release build, but is failed on debug build.

    @serhiy-storchaka serhiy-storchaka changed the title Redundant check in bytearray_mod The result type of bytearray formatting is not stable Apr 15, 2016
    @serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error and removed type-feature A feature request or enhancement labels Apr 15, 2016
    @vstinner
    Copy link
    Member

    It looks like a bug introduced by me in the issue bpo-25399.

    bytearray%args must return a bytearray object.

    I understand that test_bytes lacks tests since it missed the bug.

    @vstinner
    Copy link
    Member

    Here is a fix for bytearray%args. As expected, it was a bug in the new _PyBytesWriter API (introduced in Python 3.6 to implementation optimizations).

    @serhiy-storchaka
    Copy link
    Member

    LGTM, please commit. Why the behavior is vary in release and debug builds?

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 15, 2016

    New changeset 1eb586d5b321 by Victor Stinner in branch 'default':
    Issue bpo-26766: Fix _PyBytesWriter_Finish()
    https://hg.python.org/cpython/rev/1eb586d5b321

    @vstinner
    Copy link
    Member

    LGTM, please commit.

    Ok, done.

    Why the behavior is vary in release and debug builds?

    See _PyBytesWriter_Alloc(): the code is designed to detect bugs earlier in debug mode, it only uses 10 bytes of the "small buffer" (buffer allocated on the stack) rather than the full size of the smaller buffer (512 bytes).

    It looks like this hack helped to find a real bug :-)

    @vstinner
    Copy link
    Member

    @berker: bytearray_mod.diff LGTM, you can push it.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 15, 2016

    New changeset b3e3efc5afa4 by Berker Peksag in branch 'default':
    Issue bpo-26766: Remove redundant bytearray_format() from bytearrayobject.c
    https://hg.python.org/cpython/rev/b3e3efc5afa4

    @berkerpeksag
    Copy link
    Member Author

    Thanks for the reviews!

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants