Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ee77d9

Browse files
committed
Guido has Spoken. Restore strop.replace()'s treatment of a 0 count as
meaning infinity -- but at least warn about it in the code! I pissed away a couple hours on this today, and don't wish the same on the next in line. Bugfix candidate.
1 parent da45d55 commit 1ee77d9

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

Lib/test/test_strop.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def __getitem__(self, i): return self.seq[i]
7777
test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2)
7878
test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3)
7979
test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4)
80-
test('replace', 'one!two!three!', 'one!two!three!', '!', '@', 0)
80+
# CAUTION: a replace count of 0 means infinity only to strop, not to the
81+
# string .replace() method or to the string.replace() function.
82+
test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0)
8183
test('replace', 'one!two!three!', 'one@two@three@', '!', '@')
8284
test('replace', 'one!two!three!', 'one!two!three!', 'x', '@')
8385
test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2)

Modules/stropmodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,12 @@ strop_replace(PyObject *self, PyObject *args)
11321132
PyErr_SetString(PyExc_ValueError, "empty pattern string");
11331133
return NULL;
11341134
}
1135+
/* CAUTION: strop treats a replace count of 0 as infinity, unlke
1136+
* current (2.1) string.py and string methods. Preserve this for
1137+
* ... well, hard to say for what <wink>.
1138+
*/
1139+
if (count == 0)
1140+
count = -1;
11351141
new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
11361142
if (new_s == NULL) {
11371143
PyErr_NoMemory();

0 commit comments

Comments
 (0)