From 6af90b29725a85f275380d1973b1454e25e6bdbc Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 25 Jul 2016 14:43:04 +0300 Subject: [PATCH] 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. --- py/objstrunicode.c | 9 ++++++++- tests/basics/bytes_find.py | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/py/objstrunicode.c b/py/objstrunicode.c index c6c775d109..c3aa008332 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -116,7 +116,14 @@ STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) { // be capped to the first/last character of the string, depending on is_slice. const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len, mp_obj_t index, bool is_slice) { - (void)type; + // All str functions also handle bytes objects, and they call str_index_to_ptr(), + // so it must handle bytes. + if (type == &mp_type_bytes) { + // Taken from objstr.c:str_index_to_ptr() + mp_uint_t index_val = mp_get_index(type, self_len, index, is_slice); + return self_data + index_val; + } + mp_int_t i; // Copied from mp_get_index; I don't want bounds checking, just give me // the integer as-is. (I can't bounds-check without scanning the whole diff --git a/tests/basics/bytes_find.py b/tests/basics/bytes_find.py index 434669a901..75ef9796cd 100644 --- a/tests/basics/bytes_find.py +++ b/tests/basics/bytes_find.py @@ -21,3 +21,6 @@ print(b"0000".find(b'-1', 3)) print(b"0000".find(b'1', 3)) print(b"0000".find(b'1', 4)) print(b"0000".find(b'1', 5)) + +# Non-ascii values (make sure not treated as unicode-like) +print(b"\x80abc".find(b"a", 1))