Skip to content

Commit 62a6e89

Browse files
gh-147856: Allow the 'count' argument of bytes.replace() to be a keyword (#147943)
1 parent 362145c commit 62a6e89

File tree

10 files changed

+115
-41
lines changed

10 files changed

+115
-41
lines changed

Doc/library/stdtypes.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,12 +3735,13 @@ arbitrary binary data.
37353735
The separator to search for may be any :term:`bytes-like object`.
37363736

37373737

3738-
.. method:: bytes.replace(old, new, count=-1, /)
3739-
bytearray.replace(old, new, count=-1, /)
3738+
.. method:: bytes.replace(old, new, /, count=-1)
3739+
bytearray.replace(old, new, /, count=-1)
37403740

37413741
Return a copy of the sequence with all occurrences of subsequence *old*
3742-
replaced by *new*. If the optional argument *count* is given, only the
3743-
first *count* occurrences are replaced.
3742+
replaced by *new*. If *count* is given, only the first *count* occurrences
3743+
are replaced. If *count* is not specified or ``-1``, then all occurrences
3744+
are replaced.
37443745

37453746
The subsequence to search for and its replacement may be any
37463747
:term:`bytes-like object`.
@@ -3750,6 +3751,9 @@ arbitrary binary data.
37503751
The bytearray version of this method does *not* operate in place - it
37513752
always produces a new object, even if no changes were made.
37523753

3754+
.. versionchanged:: next
3755+
*count* is now supported as a keyword argument.
3756+
37533757

37543758
.. method:: bytes.rfind(sub[, start[, end]])
37553759
bytearray.rfind(sub[, start[, end]])

Doc/whatsnew/3.15.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,9 @@ Other language changes
605605
respectively.
606606
(Contributed by Sergey B Kirpichev in :gh:`146151`.)
607607

608+
* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
609+
(Contributed by Stan Ulbrych in :gh:`147856`.)
610+
608611

609612
New modules
610613
===========

Lib/test/test_bytes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,13 @@ def test_replace(self):
878878
self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
879879
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')
880880

881+
def test_replace_count_keyword(self):
882+
b = self.type2test(b'aa')
883+
self.assertEqual(b.replace(b'a', b'b', count=0), b'aa')
884+
self.assertEqual(b.replace(b'a', b'b', count=1), b'ba')
885+
self.assertEqual(b.replace(b'a', b'b', count=2), b'bb')
886+
self.assertEqual(b.replace(b'a', b'b', count=3), b'bb')
887+
881888
def test_replace_int_error(self):
882889
self.assertRaises(TypeError, self.type2test(b'a b').replace, 32, b'')
883890

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow the *count* argument of :meth:`bytes.replace` to be a keyword.

Objects/bytearrayobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,27 +1752,26 @@ bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
17521752

17531753

17541754
/*[clinic input]
1755-
@permit_long_docstring_body
17561755
@critical_section
17571756
bytearray.replace
17581757
17591758
old: Py_buffer
17601759
new: Py_buffer
1760+
/
17611761
count: Py_ssize_t = -1
17621762
Maximum number of occurrences to replace.
17631763
-1 (the default value) means replace all occurrences.
1764-
/
17651764
17661765
Return a copy with all occurrences of substring old replaced by new.
17671766
1768-
If the optional argument count is given, only the first count occurrences are
1769-
replaced.
1767+
If count is given, only the first count occurrences are replaced.
1768+
If count is not specified or -1, then all occurrences are replaced.
17701769
[clinic start generated code]*/
17711770

17721771
static PyObject *
17731772
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
17741773
Py_buffer *new, Py_ssize_t count)
1775-
/*[clinic end generated code: output=d39884c4dc59412a input=66afec32f4e095e0]*/
1774+
/*[clinic end generated code: output=d39884c4dc59412a input=e2591806f954aec3]*/
17761775
{
17771776
return stringlib_replace((PyObject *)self,
17781777
(const char *)old->buf, old->len,

Objects/bytesobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,26 +2403,25 @@ bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to)
24032403

24042404

24052405
/*[clinic input]
2406-
@permit_long_docstring_body
24072406
bytes.replace
24082407
24092408
old: Py_buffer
24102409
new: Py_buffer
2410+
/
24112411
count: Py_ssize_t = -1
24122412
Maximum number of occurrences to replace.
24132413
-1 (the default value) means replace all occurrences.
2414-
/
24152414
24162415
Return a copy with all occurrences of substring old replaced by new.
24172416
2418-
If the optional argument count is given, only the first count occurrences are
2419-
replaced.
2417+
If count is given, only the first count occurrences are replaced.
2418+
If count is not specified or -1, then all occurrences are replaced.
24202419
[clinic start generated code]*/
24212420

24222421
static PyObject *
24232422
bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
24242423
Py_ssize_t count)
2425-
/*[clinic end generated code: output=994fa588b6b9c104 input=8b99a9ab32bc06a2]*/
2424+
/*[clinic end generated code: output=994fa588b6b9c104 input=cdf3cf8639297745]*/
24262425
{
24272426
return stringlib_replace((PyObject *)self,
24282427
(const char *)old->buf, old->len,

Objects/clinic/bytearrayobject.c.h

Lines changed: 41 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/bytesobject.c.h

Lines changed: 41 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/unicodeobject.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/unicodeobject.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12561,7 +12561,6 @@ PyUnicode_Replace(PyObject *str,
1256112561
}
1256212562

1256312563
/*[clinic input]
12564-
@permit_long_docstring_body
1256512564
str.replace as unicode_replace
1256612565
1256712566
old: unicode
@@ -12573,14 +12572,14 @@ str.replace as unicode_replace
1257312572
1257412573
Return a copy with all occurrences of substring old replaced by new.
1257512574
12576-
If the optional argument count is given, only the first count occurrences are
12577-
replaced.
12575+
If count is given, only the first count occurrences are replaced.
12576+
If count is not specified or -1, then all occurrences are replaced.
1257812577
[clinic start generated code]*/
1257912578

1258012579
static PyObject *
1258112580
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
1258212581
Py_ssize_t count)
12583-
/*[clinic end generated code: output=b63f1a8b5eebf448 input=f27ca92ac46b65a1]*/
12582+
/*[clinic end generated code: output=b63f1a8b5eebf448 input=d15a6886b05e2edc]*/
1258412583
{
1258512584
return replace(self, old, new, count);
1258612585
}

0 commit comments

Comments
 (0)