gh-106487: Allow the 'count' argument of `str.replace` to be a keyword (#106488)

This commit is contained in:
Hugo van Kemenade 2023-07-10 12:52:36 +03:00 committed by GitHub
parent dac1e36490
commit 34c14147a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 14 deletions

View File

@ -2014,11 +2014,14 @@ expression support in the :mod:`re` module).
.. versionadded:: 3.9
.. method:: str.replace(old, new[, count])
.. method:: str.replace(old, new, count=-1)
Return a copy of the string with all occurrences of substring *old* replaced by
*new*. If the optional argument *count* is given, only the first *count*
occurrences are replaced.
*new*. If *count* is given, only the first *count* occurrences are replaced.
If *count* is not specified or ``-1``, then all occurrences are replaced.
.. versionchanged:: 3.13
*count* is now supported as a keyword argument.
.. method:: str.rfind(sub[, start[, end]])

View File

@ -76,7 +76,8 @@ New Features
Other Language Changes
======================
* Allow the *count* argument of :meth:`str.replace` to be a keyword.
(Contributed by Hugo van Kemenade in :gh:`106487`.)
New Modules
===========

View File

@ -154,6 +154,12 @@ class BaseTest:
self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, i))
def test_count_keyword(self):
self.assertEqual('aa'.replace('a', 'b', 0), 'aa'.replace('a', 'b', count=0))
self.assertEqual('aa'.replace('a', 'b', 1), 'aa'.replace('a', 'b', count=1))
self.assertEqual('aa'.replace('a', 'b', 2), 'aa'.replace('a', 'b', count=2))
self.assertEqual('aa'.replace('a', 'b', 3), 'aa'.replace('a', 'b', count=3))
def test_find(self):
self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)

View File

@ -0,0 +1,2 @@
Allow the *count* argument of :meth:`str.replace` to be a keyword. Patch by
Hugo van Kemenade.

View File

@ -736,7 +736,7 @@ exit:
}
PyDoc_STRVAR(unicode_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"
@ -749,21 +749,49 @@ PyDoc_STRVAR(unicode_replace__doc__,
"replaced.");
#define UNICODE_REPLACE_METHODDEF \
{"replace", _PyCFunction_CAST(unicode_replace), METH_FASTCALL, unicode_replace__doc__},
{"replace", _PyCFunction_CAST(unicode_replace), METH_FASTCALL|METH_KEYWORDS, unicode_replace__doc__},
static PyObject *
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
Py_ssize_t count);
static PyObject *
unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
unicode_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
PyObject *ob_item[NUM_KEYWORDS];
} _kwtuple = {
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
.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;
PyObject *old;
PyObject *new;
Py_ssize_t count = -1;
if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) {
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
if (!args) {
goto exit;
}
if (!PyUnicode_Check(args[0])) {
@ -776,8 +804,8 @@ unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
new = args[1];
if (nargs < 3) {
goto skip_optional;
if (!noptargs) {
goto skip_optional_pos;
}
{
Py_ssize_t ival = -1;
@ -791,7 +819,7 @@ unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
}
count = ival;
}
skip_optional:
skip_optional_pos:
return_value = unicode_replace_impl(self, old, new, count);
exit:
@ -1476,4 +1504,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=0a71c4aeffdf0bc5 input=a9049054013a1b77]*/
/*[clinic end generated code: output=ee76a1b49cd4cbb3 input=a9049054013a1b77]*/

View File

@ -12025,10 +12025,10 @@ str.replace as unicode_replace
old: unicode
new: unicode
/
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.
@ -12039,7 +12039,7 @@ replaced.
static PyObject *
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
Py_ssize_t count)
/*[clinic end generated code: output=b63f1a8b5eebf448 input=147d12206276ebeb]*/
/*[clinic end generated code: output=b63f1a8b5eebf448 input=3345c455d60a5499]*/
{
return replace(self, old, new, count);
}