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
Comments
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. |
Yes, I noticed this, but left for future (post-issue26765) refactoring. The patch LGTM, but notice Victor's comment to bpo-26764. |
And may be bytearray_mod in 3.6 is not correct. In 3.5 it returns bytearray, in 3.6 it returns bytes. |
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(). |
Python 3.5: >>> bytearray(b'%d') % 42
bytearray(b'42') Python 3.6: >>> bytearray(b'%d') % 42
b'42' |
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') |
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. |
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. |
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). |
LGTM, please commit. Why the behavior is vary in release and debug builds? |
New changeset 1eb586d5b321 by Victor Stinner in branch 'default': |
Ok, done.
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 :-) |
@berker: bytearray_mod.diff LGTM, you can push it. |
New changeset b3e3efc5afa4 by Berker Peksag in branch 'default': |
Thanks for the reviews! |
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: