Skip to content

Commit 6af90b2

Browse files
committed
py/objstrunicode: str_index_to_ptr: Should handle bytes too.
There's single str_index_to_ptr() function, called for both bytes and unicode objects, so should handle each properly.
1 parent 16f3246 commit 6af90b2

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

py/objstrunicode.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,14 @@ STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) {
116116
// be capped to the first/last character of the string, depending on is_slice.
117117
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
118118
mp_obj_t index, bool is_slice) {
119-
(void)type;
119+
// All str functions also handle bytes objects, and they call str_index_to_ptr(),
120+
// so it must handle bytes.
121+
if (type == &mp_type_bytes) {
122+
// Taken from objstr.c:str_index_to_ptr()
123+
mp_uint_t index_val = mp_get_index(type, self_len, index, is_slice);
124+
return self_data + index_val;
125+
}
126+
120127
mp_int_t i;
121128
// Copied from mp_get_index; I don't want bounds checking, just give me
122129
// the integer as-is. (I can't bounds-check without scanning the whole

tests/basics/bytes_find.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
print(b"0000".find(b'1', 3))
2222
print(b"0000".find(b'1', 4))
2323
print(b"0000".find(b'1', 5))
24+
25+
# Non-ascii values (make sure not treated as unicode-like)
26+
print(b"\x80abc".find(b"a", 1))

0 commit comments

Comments
 (0)