summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-03-06 09:28:32 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-03-06 09:28:32 (GMT)
commitccdf352370da4acf2c3f1c77e4a21bc954b4dcf6 (patch)
treeba099c336d0c194e7ebf5678a470a2e8115633ef
parent25dded041fe532fcb041b6e68582bf76b4968132 (diff)
downloadcpython-ccdf352370da4acf2c3f1c77e4a21bc954b4dcf6.zip
cpython-ccdf352370da4acf2c3f1c77e4a21bc954b4dcf6.tar.gz
cpython-ccdf352370da4acf2c3f1c77e4a21bc954b4dcf6.tar.bz2
Issue #20283: RE pattern methods now accept the string keyword parameters
as documented. The pattern and source keyword parameters are left as deprecated aliases.
-rw-r--r--Lib/test/test_re.py16
-rw-r--r--Misc/NEWS4
-rw-r--r--Modules/_sre.c84
3 files changed, 84 insertions, 20 deletions
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 1c6f45d..5466b20 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1076,6 +1076,22 @@ class ReTests(unittest.TestCase):
self.assertEqual(out.getvalue().splitlines(),
['literal 102 ', 'literal 111 ', 'literal 111 '])
+ def test_keyword_parameters(self):
+ # Issue #20283: Accepting the string keyword parameter.
+ pat = re.compile(r'(ab)')
+ self.assertEqual(
+ pat.match(string='abracadabra', pos=7, endpos=10).span(), (7, 9))
+ self.assertEqual(
+ pat.search(string='abracadabra', pos=3, endpos=10).span(), (7, 9))
+ self.assertEqual(
+ pat.findall(string='abracadabra', pos=3, endpos=10), ['ab'])
+ self.assertEqual(
+ pat.split(string='abracadabra', maxsplit=1),
+ ['', 'ab', 'racadabra'])
+ self.assertEqual(
+ pat.scanner(string='abracadabra', pos=3, endpos=10).search().span(),
+ (7, 9))
+
def run_re_tests():
from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
diff --git a/Misc/NEWS b/Misc/NEWS
index e32613f..62211f4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,10 @@ Core and Builtins
Library
-------
+- Issue #20283: RE pattern methods now accept the string keyword parameters
+ as documented. The pattern and source keyword parameters are left as
+ deprecated aliases.
+
- Issue #20778: Fix modulefinder to work with bytecode-only modules.
- Issue #20791: copy.copy() now doesn't make a copy when the input is
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 1c76d24..b1258ee 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1877,17 +1877,45 @@ pattern_dealloc(PatternObject* self)
}
static PyObject*
+fix_string_param(PyObject *string, PyObject *string2, const char *oldname)
+{
+ if (string2 != NULL) {
+ if (string != NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "Argument given by name ('%s') and position (1)",
+ oldname);
+ return NULL;
+ }
+ if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
+ "The '%s' keyword parameter name is deprecated. "
+ "Use 'string' instead.", oldname) < 0)
+ return NULL;
+ return string2;
+ }
+ if (string == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "Required argument 'string' (pos 1) not found");
+ return NULL;
+ }
+ return string;
+}
+
+static PyObject*
pattern_match(PatternObject* self, PyObject* args, PyObject* kw)
{
SRE_STATE state;
int status;
- PyObject* string;
+ PyObject *string = NULL, *string2 = NULL;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
- static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:match", kwlist,
- &string, &start, &end))
+ static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:match", kwlist,
+ &string, &start, &end, &string2))
+ return NULL;
+
+ string = fix_string_param(string, string2, "pattern");
+ if (!string)
return NULL;
string = state_init(&state, self, string, start, end);
@@ -1919,12 +1947,16 @@ pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
SRE_STATE state;
int status;
- PyObject* string;
+ PyObject *string = NULL, *string2 = NULL;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
- static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:search", kwlist,
- &string, &start, &end))
+ static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:search", kwlist,
+ &string, &start, &end, &string2))
+ return NULL;
+
+ string = fix_string_param(string, string2, "pattern");
+ if (!string)
return NULL;
string = state_init(&state, self, string, start, end);
@@ -2052,12 +2084,16 @@ pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
int status;
Py_ssize_t i, b, e;
- PyObject* string;
+ PyObject *string = NULL, *string2 = NULL;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
- static char* kwlist[] = { "source", "pos", "endpos", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:findall", kwlist,
- &string, &start, &end))
+ static char* kwlist[] = { "string", "pos", "endpos", "source", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:findall", kwlist,
+ &string, &start, &end, &string2))
+ return NULL;
+
+ string = fix_string_param(string, string2, "source");
+ if (!string)
return NULL;
string = state_init(&state, self, string, start, end);
@@ -2180,11 +2216,15 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
Py_ssize_t i;
void* last;
- PyObject* string;
+ PyObject *string = NULL, *string2 = NULL;
Py_ssize_t maxsplit = 0;
- static char* kwlist[] = { "source", "maxsplit", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O|n:split", kwlist,
- &string, &maxsplit))
+ static char* kwlist[] = { "string", "maxsplit", "source", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|On$O:split", kwlist,
+ &string, &maxsplit, &string2))
+ return NULL;
+
+ string = fix_string_param(string, string2, "source");
+ if (!string)
return NULL;
string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
@@ -3882,12 +3922,16 @@ pattern_scanner(PatternObject* pattern, PyObject* args, PyObject* kw)
ScannerObject* self;
- PyObject* string;
+ PyObject *string = NULL, *string2 = NULL;
Py_ssize_t start = 0;
Py_ssize_t end = PY_SSIZE_T_MAX;
- static char* kwlist[] = { "source", "pos", "endpos", NULL };
- if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:scanner", kwlist,
- &string, &start, &end))
+ static char* kwlist[] = { "string", "pos", "endpos", "source", NULL };
+ if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:scanner", kwlist,
+ &string, &start, &end, &string2))
+ return NULL;
+
+ string = fix_string_param(string, string2, "source");
+ if (!string)
return NULL;
/* create scanner object */