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
index() and count() methods of bytes and bytearray should accept byte ints #56379
Comments
Bytes objects when indexed provide integers, but do not accept them to many functions, making them inconsistent with other sequences. Basic example:
>>> test = b'012'
>>> n = test[1]
>>> n
49
>>> n in test
True
>>> test.index(n)
TypeError: expected an object with the buffer interface. It is certainly unusual for n to be in the sequence, but not to be able to find it. I would expect the result to be 1. This set of commands with list, strings, tuples, but not bytes objects. I suspect, from issue bpo-10616, that all the following functions would be affected: It would make more sense to me that instead of only supporting buffer interface objects, they also accept a single integer, and treat it as if it were provided a length-1 bytes object. The use case I came across this problem was something like this: Given seq1 and seq2, sequences of the same type: This works for strings, lists, tuples, but not bytes. |
"This set of commands with list, strings, tuples, but not bytes objects." |
Agreed. Doc Lib: 4.6. Sequence Types — str, bytes, bytearray, list, tuple, range says ''' >>> test = b'0120'
>>> z = b'0'
>>> zo = ord(z)
>>> z in test
True
>>> zo in test
True
>>> test.index(z)
0
>>> test.index(zo)
...
TypeError: expected an object with the buffer interface
>>> test.count(z)
2
>>> test.count(zo)
...
TypeError: expected an object with the buffer interface
# longer subsequences like b'01' also work So I think the code for 3.2+ bytes.count() and bytes.index() should do the same branching as the code for bytes.__contains__. The other functions you list, including .rindex are not general sequence functions but are string functions defined as taking subsequences as inputs. So they would never be used in generic code like .count and .index can be. |
Fair enough. I think it would make sense for the string methods to also accept single ints where possible as well: For haystack and needles both strings: For both bytes, it's a bit contortionist: One ends up doing a lot of the [i:i+1] bending when using bytes functions. |
This affects bytearray as well as bytes. What comes to supporting integer argument to str methods, I'm -1 on that. str's "contained items" are strings of length 1. |
Attached a patch with the following changes: Allow an integer argument in range(0, 256) for the following bytes and The bytes methods were changed to use the new buffer protocol instead Tests for all the modified functions were expanded to cover the new A paragraph describing the additional semantics of the five methods The error messages of index and rindex were left untouched The docstrings were also left unchanged, as I couldn't find a good And finally, there's one thing that I'm unsure of: When an integer out of range(0, 256) is passed as the first argument, |
ValueError = Inappropriate argument value (of correct type).
TypeError = Inappropriate argument type.
Then the users should check if the value is in range(256) before passing it to (r)index. |
Ok, so the current raising semantics should be good. |
See also bpo-12631 regarding the remove() method for bytearray. |
AFAICS, it's about bytearray.remove() working but bytearray.index() not working as documented, and that's why I marked is as a duplicate of this issue. |
That sounds reasonable. OverflowError would have been another choice, but I agree that consistency with __contains__ is sensible. |
Doc/library/stdtypes.rst needs a "versionadded" tag for the additional semantics. Also, the patch doesn't compile fine on current default: In file included from Objects/unicodeobject.c:487:0: I'd say you need to either define your function as STRINGLIB(parse_args_finds_byte) (to avoid name collisions), or avoid defining it if STRINGLIB_IS_UNICODE. |
Thanks for the review, Antoine. Attached an updated the patch:
|
Fixed a minor inconsistency. |
New changeset c1effa2cdd20 by Antoine Pitrou in branch 'default': |
Patch committed, thank you! |
Just a thought: Would this change be worthy for the "What's new in 3.3" list? |
I think so. |
New changeset 736b0aec412b by Petri Lehtinen in branch 'default': |
New changeset 75648db1b3f3 by Victor Stinner in branch 'default': |
New changeset 75648db1b3f3 by Victor Stinner in branch 'default': |
New changeset 018fe1dee9b3 by Petri Lehtinen in branch 'default': |
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: