[Python-Dev] Inconsistent string.replace() behavior (original) (raw)

Tim Peters tim.one@home.com
Wed, 9 May 2001 21:33:52 -0400


[Guido]

HMMMMMM! In Python 1.5, a count of zero always replaces all occurrences, both using string and using strop. In 2.0 and later, strop's replace(..., 0) still replaces all, but string's replaces none. The replace() method of strings and unicode objects agrees with string.py.

I think this change was made in the sake of ease of documenting the behavior: special-casing the count of zero is unexpected.

Yes, -1 == infinity is much clearer .

I very vaguely recall that it was discussed on this list.

So this suggests that teststring is correct, and string.replace() (and the methods) shouldn't be "fixed"!

I didn't change their behavior wrt replace()'s interpretation of count, but to repair an unrelated bug (bogus MemoryError for an empty-string result) that happened to appear in both copies of mymemreplace sitting in the code base (one in stringobject.c, another but out-of-synch one in stropmodule.c). That's how stropmodule got sucked into this: to fix the gross null-string result bug common to both.

But since we're not really supporting strop any more, I think that strop shouldn't be changed either. So we'll have to live with the difference -- sorry!

OK, I've restored the 0 == infinity semantics to strop.replace() and test_strop.py, but have not backed out the null-string result fix, nor the pain to make the mymemreplace clones identical again.