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

M.-A. Lemburg mal@lemburg.com
Mon, 13 Mar 2000 10:13:50 +0100


Mark Hammond wrote:
> 
> 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...

Dang. Must have been my mistake -- it should read:

        if (sub_len <= 0) {
                PyErr_SetString(PyExc_ValueError, "empty pattern string");
                return NULL;
        }

Thanks for reporting this... I'll include the fix in the
next patch set.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/