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`.
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]])
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
===========
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'')
--- /dev/null
+Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
/*[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,
/*[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,
}
PyDoc_STRVAR(bytearray_replace__doc__,
-"replace($self, old, new, count=-1, /)\n"
+"replace($self, old, new, /, count=-1)\n"
"--\n"
"\n"
"Return a copy with all occurrences of substring old replaced by new.\n"
" Maximum number of occurrences to replace.\n"
" -1 (the default value) means replace all occurrences.\n"
"\n"
-"If the optional argument count is given, only the first count occurrences are\n"
-"replaced.");
+"If count is given, only the first count occurrences are replaced.\n"
+"If count is not specified or -1, then all occurrences are replaced.");
#define BYTEARRAY_REPLACE_METHODDEF \
- {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL, bytearray_replace__doc__},
+ {"replace", _PyCFunction_CAST(bytearray_replace), METH_FASTCALL|METH_KEYWORDS, bytearray_replace__doc__},
static PyObject *
bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
Py_buffer *new, Py_ssize_t count);
static PyObject *
-bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+bytearray_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ Py_hash_t ob_hash;
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_hash = -1,
+ .ob_item = { &_Py_ID(count), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"", "", "count", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "replace",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[3];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Py_buffer old = {NULL, NULL};
Py_buffer new = {NULL, NULL};
Py_ssize_t count = -1;
- if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
+ /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
+ if (!args) {
goto exit;
}
if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
goto exit;
}
- if (nargs < 3) {
- goto skip_optional;
+ if (!noptargs) {
+ goto skip_optional_pos;
}
{
Py_ssize_t ival = -1;
}
count = ival;
}
-skip_optional:
+skip_optional_pos:
Py_BEGIN_CRITICAL_SECTION(self);
return_value = bytearray_replace_impl((PyByteArrayObject *)self, &old, &new, count);
Py_END_CRITICAL_SECTION();
{
return bytearray_sizeof_impl((PyByteArrayObject *)self);
}
-/*[clinic end generated code: output=2d76ef023928424f input=a9049054013a1b77]*/
+/*[clinic end generated code: output=d4976faf6731b8da input=a9049054013a1b77]*/
}
PyDoc_STRVAR(bytes_replace__doc__,
-"replace($self, old, new, count=-1, /)\n"
+"replace($self, old, new, /, count=-1)\n"
"--\n"
"\n"
"Return a copy with all occurrences of substring old replaced by new.\n"
" Maximum number of occurrences to replace.\n"
" -1 (the default value) means replace all occurrences.\n"
"\n"
-"If the optional argument count is given, only the first count occurrences are\n"
-"replaced.");
+"If count is given, only the first count occurrences are replaced.\n"
+"If count is not specified or -1, then all occurrences are replaced.");
#define BYTES_REPLACE_METHODDEF \
- {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL, bytes_replace__doc__},
+ {"replace", _PyCFunction_CAST(bytes_replace), METH_FASTCALL|METH_KEYWORDS, bytes_replace__doc__},
static PyObject *
bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new,
Py_ssize_t count);
static PyObject *
-bytes_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+bytes_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ Py_hash_t ob_hash;
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_hash = -1,
+ .ob_item = { &_Py_ID(count), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"", "", "count", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "replace",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[3];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Py_buffer old = {NULL, NULL};
Py_buffer new = {NULL, NULL};
Py_ssize_t count = -1;
- if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
+ /*minpos*/ 2, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
+ if (!args) {
goto exit;
}
if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) {
if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) {
goto exit;
}
- if (nargs < 3) {
- goto skip_optional;
+ if (!noptargs) {
+ goto skip_optional_pos;
}
{
Py_ssize_t ival = -1;
}
count = ival;
}
-skip_optional:
+skip_optional_pos:
return_value = bytes_replace_impl((PyBytesObject *)self, &old, &new, count);
exit:
exit:
return return_value;
}
-/*[clinic end generated code: output=08b9507244f73638 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5675f7008a84ce6d input=a9049054013a1b77]*/
" Maximum number of occurrences to replace.\n"
" -1 (the default value) means replace all occurrences.\n"
"\n"
-"If the optional argument count is given, only the first count occurrences are\n"
-"replaced.");
+"If count is given, only the first count occurrences are replaced.\n"
+"If count is not specified or -1, then all occurrences are replaced.");
#define UNICODE_REPLACE_METHODDEF \
{"replace", _PyCFunction_CAST(unicode_replace), METH_FASTCALL|METH_KEYWORDS, unicode_replace__doc__},
exit:
return return_value;
}
-/*[clinic end generated code: output=238917fe66120bde input=a9049054013a1b77]*/
+/*[clinic end generated code: output=13eaf65699ea9fc9 input=a9049054013a1b77]*/
}
/*[clinic input]
-@permit_long_docstring_body
str.replace as unicode_replace
old: unicode
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);
}