Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3735,12 +3735,13 @@ arbitrary binary data.
The separator to search for may be any :term:`bytes-like object`.


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

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

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

.. versionchanged:: next
*count* is now supported as a keyword argument.


.. method:: bytes.rfind(sub[, start[, end]])
bytearray.rfind(sub[, start[, end]])
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ Other language changes
respectively.
(Contributed by Sergey B Kirpichev in :gh:`146151`.)

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


New modules
===========
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,13 @@ def test_replace(self):
self.assertEqual(b.replace(b'i', b'a'), b'massassappa')
self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi')

def test_replace_count_keyword(self):
b = self.type2test(b'aa')
self.assertEqual(b.replace(b'a', b'b', count=0), b'aa')
self.assertEqual(b.replace(b'a', b'b', count=1), b'ba')
self.assertEqual(b.replace(b'a', b'b', count=2), b'bb')
self.assertEqual(b.replace(b'a', b'b', count=3), b'bb')

def test_replace_int_error(self):
self.assertRaises(TypeError, self.type2test(b'a b').replace, 32, b'')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
9 changes: 4 additions & 5 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1752,27 +1752,26 @@ bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)


/*[clinic input]
@permit_long_docstring_body
@critical_section
bytearray.replace

old: Py_buffer
new: Py_buffer
/
count: Py_ssize_t = -1
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
/

Return a copy with all occurrences of substring old replaced by new.

If the optional argument count is given, only the first count occurrences are
replaced.
If count is given, only the first count occurrences are replaced.
If count is not specified or -1, then all occurrences are replaced.
[clinic start generated code]*/

static PyObject *
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
Py_buffer *new, Py_ssize_t count)
/*[clinic end generated code: output=d39884c4dc59412a input=66afec32f4e095e0]*/
/*[clinic end generated code: output=d39884c4dc59412a input=e2591806f954aec3]*/
{
return stringlib_replace((PyObject *)self,
(const char *)old->buf, old->len,
Expand Down
9 changes: 4 additions & 5 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2403,26 +2403,25 @@ bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to)


/*[clinic input]
@permit_long_docstring_body
bytes.replace

old: Py_buffer
new: Py_buffer
/
count: Py_ssize_t = -1
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
/

Return a copy with all occurrences of substring old replaced by new.

If the optional argument count is given, only the first count occurrences are
replaced.
If count is given, only the first count occurrences are replaced.
If count is not specified or -1, then all occurrences are replaced.
[clinic start generated code]*/

static PyObject *
bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Py_ssize_t count)
/*[clinic end generated code: output=994fa588b6b9c104 input=8b99a9ab32bc06a2]*/
/*[clinic end generated code: output=994fa588b6b9c104 input=cdf3cf8639297745]*/
{
return stringlib_replace((PyObject *)self,
(const char *)old->buf, old->len,
Expand Down
51 changes: 41 additions & 10 deletions Objects/clinic/bytearrayobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 41 additions & 10 deletions Objects/clinic/bytesobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Objects/clinic/unicodeobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -12561,7 +12561,6 @@ PyUnicode_Replace(PyObject *str,
}

/*[clinic input]
@permit_long_docstring_body
str.replace as unicode_replace

old: unicode
Expand All @@ -12573,14 +12572,14 @@ str.replace as unicode_replace

Return a copy with all occurrences of substring old replaced by new.

If the optional argument count is given, only the first count occurrences are
replaced.
If count is given, only the first count occurrences are replaced.
If count is not specified or -1, then all occurrences are replaced.
[clinic start generated code]*/

static PyObject *
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
Py_ssize_t count)
/*[clinic end generated code: output=b63f1a8b5eebf448 input=f27ca92ac46b65a1]*/
/*[clinic end generated code: output=b63f1a8b5eebf448 input=d15a6886b05e2edc]*/
{
return replace(self, old, new, count);
}
Expand Down
Loading