Skip to content

Commit 0e616a5

Browse files
Allow the 'count' argument of bytes.replace() to be a keyword
1 parent 70d1b08 commit 0e616a5

File tree

8 files changed

+109
-34
lines changed

8 files changed

+109
-34
lines changed

Doc/library/stdtypes.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,12 +3735,16 @@ 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.
3745+
3746+
.. versionchanged:: next
3747+
*count* is now supported as a keyword argument.
37443748

37453749
The subsequence to search for and its replacement may be any
37463750
:term:`bytes-like object`.

Doc/whatsnew/3.15.rst

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

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

608611
New modules
609612
===========

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.

0 commit comments

Comments
 (0)