mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-106487: Allow the 'count' argument of str.replace
to be a keyword (#106488)
This commit is contained in:
parent
dac1e36490
commit
34c14147a2
6 changed files with 54 additions and 14 deletions
|
@ -2014,11 +2014,14 @@ expression support in the :mod:`re` module).
|
||||||
.. versionadded:: 3.9
|
.. 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
|
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*
|
*new*. If *count* is given, only the first *count* occurrences are replaced.
|
||||||
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]])
|
.. method:: str.rfind(sub[, start[, end]])
|
||||||
|
|
|
@ -76,7 +76,8 @@ New Features
|
||||||
Other Language Changes
|
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
|
New Modules
|
||||||
===========
|
===========
|
||||||
|
|
|
@ -154,6 +154,12 @@ class BaseTest:
|
||||||
self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
|
self.assertEqual(rem, 0, '%s != 0 for %s' % (rem, i))
|
||||||
self.assertEqual(r1, r2, '%s != %s for %s' % (r1, r2, 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):
|
def test_find(self):
|
||||||
self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
|
self.checkequal(0, 'abcdefghiabc', 'find', 'abc')
|
||||||
self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
|
self.checkequal(9, 'abcdefghiabc', 'find', 'abc', 1)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Allow the *count* argument of :meth:`str.replace` to be a keyword. Patch by
|
||||||
|
Hugo van Kemenade.
|
44
Objects/clinic/unicodeobject.c.h
generated
44
Objects/clinic/unicodeobject.c.h
generated
|
@ -736,7 +736,7 @@ exit:
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(unicode_replace__doc__,
|
PyDoc_STRVAR(unicode_replace__doc__,
|
||||||
"replace($self, old, new, count=-1, /)\n"
|
"replace($self, old, new, /, count=-1)\n"
|
||||||
"--\n"
|
"--\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Return a copy with all occurrences of substring old replaced by new.\n"
|
"Return a copy with all occurrences of substring old replaced by new.\n"
|
||||||
|
@ -749,21 +749,49 @@ PyDoc_STRVAR(unicode_replace__doc__,
|
||||||
"replaced.");
|
"replaced.");
|
||||||
|
|
||||||
#define UNICODE_REPLACE_METHODDEF \
|
#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 *
|
static PyObject *
|
||||||
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
|
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
|
||||||
Py_ssize_t count);
|
Py_ssize_t count);
|
||||||
|
|
||||||
static PyObject *
|
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;
|
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 *old;
|
||||||
PyObject *new;
|
PyObject *new;
|
||||||
Py_ssize_t count = -1;
|
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;
|
goto exit;
|
||||||
}
|
}
|
||||||
if (!PyUnicode_Check(args[0])) {
|
if (!PyUnicode_Check(args[0])) {
|
||||||
|
@ -776,8 +804,8 @@ unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
new = args[1];
|
new = args[1];
|
||||||
if (nargs < 3) {
|
if (!noptargs) {
|
||||||
goto skip_optional;
|
goto skip_optional_pos;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
Py_ssize_t ival = -1;
|
Py_ssize_t ival = -1;
|
||||||
|
@ -791,7 +819,7 @@ unicode_replace(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
|
||||||
}
|
}
|
||||||
count = ival;
|
count = ival;
|
||||||
}
|
}
|
||||||
skip_optional:
|
skip_optional_pos:
|
||||||
return_value = unicode_replace_impl(self, old, new, count);
|
return_value = unicode_replace_impl(self, old, new, count);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
|
@ -1476,4 +1504,4 @@ skip_optional_pos:
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=0a71c4aeffdf0bc5 input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=ee76a1b49cd4cbb3 input=a9049054013a1b77]*/
|
||||||
|
|
|
@ -12025,10 +12025,10 @@ str.replace as unicode_replace
|
||||||
|
|
||||||
old: unicode
|
old: unicode
|
||||||
new: unicode
|
new: unicode
|
||||||
|
/
|
||||||
count: Py_ssize_t = -1
|
count: Py_ssize_t = -1
|
||||||
Maximum number of occurrences to replace.
|
Maximum number of occurrences to replace.
|
||||||
-1 (the default value) means replace all occurrences.
|
-1 (the default value) means replace all occurrences.
|
||||||
/
|
|
||||||
|
|
||||||
Return a copy with all occurrences of substring old replaced by new.
|
Return a copy with all occurrences of substring old replaced by new.
|
||||||
|
|
||||||
|
@ -12039,7 +12039,7 @@ replaced.
|
||||||
static PyObject *
|
static PyObject *
|
||||||
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
|
unicode_replace_impl(PyObject *self, PyObject *old, PyObject *new,
|
||||||
Py_ssize_t count)
|
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);
|
return replace(self, old, new, count);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue