diff --git a/py/objstr.c b/py/objstr.c index 42a246429c2037d1db045e976ffacae23de17339..d095c8b471b0315ded4a7020762de8a7b32ce060 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -691,6 +691,12 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) { assert(last_good_char_pos >= first_good_char_pos); //+1 to accomodate the last character machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1; + if (stripped_len == orig_str_len) { + // If nothing was stripped, don't bother to dup original string + // TODO: watch out for this case when we'll get to bytearray.strip() + assert(first_good_char_pos == 0); + return args[0]; + } return mp_obj_new_str_of_type(self_type, orig_str + first_good_char_pos, stripped_len); } diff --git a/tests/basics/string_strip.py b/tests/basics/string_strip.py index 70c74b3834c3e4e9e5a790f76d772df0921a3560..5d99a78e51c381fedd34616d40f57eb86a1d47cc 100644 --- a/tests/basics/string_strip.py +++ b/tests/basics/string_strip.py @@ -31,3 +31,7 @@ print(" a".rstrip()) print("a ".strip()) print("a ".lstrip()) print("a ".rstrip()) + +# Test that stripping unstrippable string returns original object +s = "abc" +print(id(s.strip()) == id(s))