[Python-Dev] string.replace behaviour change since Unicode patch.

Mark Hammond mhammond@skippinet.com.au
Mon, 13 Mar 2000 14:50:35 +1100


Hi,
	After applying the Unicode changes string.replace() seems to have changed
its behaviour:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> string.replace("foo\nbar", "\n", "")
'foobar'
>>>

But since the Unicode update:

Python 1.5.2+ (#0, Feb  2 2000, 16:46:55) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> string.replace("foo\nbar", "\n", "")
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "L:\src\python-cvs\lib\string.py", line 407, in replace
    return s.replace(old, new, maxsplit)
ValueError: empty replacement string
>>>

The offending check is stringmodule.c, line 1578:
	if (repl_len <= 0) {
		PyErr_SetString(PyExc_ValueError, "empty replacement string");
		return NULL;
	}

Changing the check to "< 0" fixes the immediate problem, but it is unclear
why the check was added at all, so I didnt bother submitting a patch...

Mark.