Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 562586e

Browse files
author
Fredrik Lundh
committed
Accept keyword arguments for (most) pattern and match object
methods. Closes buglet #115845.
1 parent d11b5e5 commit 562586e

1 file changed

Lines changed: 45 additions & 31 deletions

File tree

Modules/_sre.c

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* 2000-09-02 fl return -1 instead of None for start/end/span
2121
* 2000-09-20 fl added expand method
2222
* 2000-09-21 fl don't use the buffer interface for unicode strings
23-
* 2000-10-03 fl fixed assert_not primitive
23+
* 2000-10-03 fl fixed assert_not primitive; support keyword arguments
2424
*
2525
* Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
2626
*
@@ -1514,15 +1514,17 @@ pattern_dealloc(PatternObject* self)
15141514
}
15151515

15161516
static PyObject*
1517-
pattern_match(PatternObject* self, PyObject* args)
1517+
pattern_match(PatternObject* self, PyObject* args, PyObject* kw)
15181518
{
15191519
SRE_STATE state;
15201520
int status;
15211521

15221522
PyObject* string;
15231523
int start = 0;
15241524
int end = INT_MAX;
1525-
if (!PyArg_ParseTuple(args, "O|ii:match", &string, &start, &end))
1525+
static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
1526+
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:match", kwlist,
1527+
&string, &start, &end))
15261528
return NULL;
15271529

15281530
string = state_init(&state, self, string, start, end);
@@ -1549,15 +1551,17 @@ pattern_match(PatternObject* self, PyObject* args)
15491551
}
15501552

15511553
static PyObject*
1552-
pattern_search(PatternObject* self, PyObject* args)
1554+
pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
15531555
{
15541556
SRE_STATE state;
15551557
int status;
15561558

15571559
PyObject* string;
15581560
int start = 0;
15591561
int end = INT_MAX;
1560-
if (!PyArg_ParseTuple(args, "O|ii:search", &string, &start, &end))
1562+
static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
1563+
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:search", kwlist,
1564+
&string, &start, &end))
15611565
return NULL;
15621566

15631567
string = state_init(&state, self, string, start, end);
@@ -1607,45 +1611,51 @@ call(char* function, PyObject* args)
16071611
}
16081612

16091613
static PyObject*
1610-
pattern_sub(PatternObject* self, PyObject* args)
1614+
pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
16111615
{
16121616
PyObject* template;
16131617
PyObject* string;
16141618
PyObject* count = Py_False; /* zero */
1615-
if (!PyArg_ParseTuple(args, "OO|O:sub", &template, &string, &count))
1619+
static char* kwlist[] = { "repl", "string", "count", NULL };
1620+
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|O:sub", kwlist,
1621+
&template, &string, &count))
16161622
return NULL;
16171623

16181624
/* delegate to Python code */
16191625
return call("_sub", Py_BuildValue("OOOO", self, template, string, count));
16201626
}
16211627

16221628
static PyObject*
1623-
pattern_subn(PatternObject* self, PyObject* args)
1629+
pattern_subn(PatternObject* self, PyObject* args, PyObject* kw)
16241630
{
16251631
PyObject* template;
16261632
PyObject* string;
16271633
PyObject* count = Py_False; /* zero */
1628-
if (!PyArg_ParseTuple(args, "OO|O:subn", &template, &string, &count))
1634+
static char* kwlist[] = { "repl", "string", "count", NULL };
1635+
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|O:subn", kwlist,
1636+
&template, &string, &count))
16291637
return NULL;
16301638

16311639
/* delegate to Python code */
16321640
return call("_subn", Py_BuildValue("OOOO", self, template, string, count));
16331641
}
16341642

16351643
static PyObject*
1636-
pattern_split(PatternObject* self, PyObject* args)
1644+
pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
16371645
{
16381646
PyObject* string;
16391647
PyObject* maxsplit = Py_False; /* zero */
1640-
if (!PyArg_ParseTuple(args, "O|O:split", &string, &maxsplit))
1648+
static char* kwlist[] = { "source", "maxsplit", NULL };
1649+
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|O:split", kwlist,
1650+
&string, &maxsplit))
16411651
return NULL;
16421652

16431653
/* delegate to Python code */
16441654
return call("_split", Py_BuildValue("OOO", self, string, maxsplit));
16451655
}
16461656

16471657
static PyObject*
1648-
pattern_findall(PatternObject* self, PyObject* args)
1658+
pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
16491659
{
16501660
SRE_STATE state;
16511661
PyObject* list;
@@ -1655,7 +1665,9 @@ pattern_findall(PatternObject* self, PyObject* args)
16551665
PyObject* string;
16561666
int start = 0;
16571667
int end = INT_MAX;
1658-
if (!PyArg_ParseTuple(args, "O|ii:findall", &string, &start, &end))
1668+
static char* kwlist[] = { "source", "pos", "endpos", NULL };
1669+
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:findall", kwlist,
1670+
&string, &start, &end))
16591671
return NULL;
16601672

16611673
string = state_init(&state, self, string, start, end);
@@ -1745,14 +1757,14 @@ pattern_findall(PatternObject* self, PyObject* args)
17451757
}
17461758

17471759
static PyMethodDef pattern_methods[] = {
1748-
{"match", (PyCFunction) pattern_match, 1},
1749-
{"search", (PyCFunction) pattern_search, 1},
1750-
{"sub", (PyCFunction) pattern_sub, 1},
1751-
{"subn", (PyCFunction) pattern_subn, 1},
1752-
{"split", (PyCFunction) pattern_split, 1},
1753-
{"findall", (PyCFunction) pattern_findall, 1},
1760+
{"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS},
1761+
{"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS},
1762+
{"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS},
1763+
{"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS},
1764+
{"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS},
1765+
{"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS},
17541766
/* experimental */
1755-
{"scanner", (PyCFunction) pattern_scanner, 1},
1767+
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
17561768
{NULL, NULL}
17571769
};
17581770

@@ -1914,13 +1926,14 @@ match_group(MatchObject* self, PyObject* args)
19141926
}
19151927

19161928
static PyObject*
1917-
match_groups(MatchObject* self, PyObject* args)
1929+
match_groups(MatchObject* self, PyObject* args, PyObject* kw)
19181930
{
19191931
PyObject* result;
19201932
int index;
19211933

19221934
PyObject* def = Py_None;
1923-
if (!PyArg_ParseTuple(args, "|O:groups", &def))
1935+
static char* kwlist[] = { "default", NULL };
1936+
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def))
19241937
return NULL;
19251938

19261939
result = PyTuple_New(self->groups-1);
@@ -1941,14 +1954,15 @@ match_groups(MatchObject* self, PyObject* args)
19411954
}
19421955

19431956
static PyObject*
1944-
match_groupdict(MatchObject* self, PyObject* args)
1957+
match_groupdict(MatchObject* self, PyObject* args, PyObject* kw)
19451958
{
19461959
PyObject* result;
19471960
PyObject* keys;
19481961
int index;
19491962

19501963
PyObject* def = Py_None;
1951-
if (!PyArg_ParseTuple(args, "|O:groupdict", &def))
1964+
static char* kwlist[] = { "default", NULL };
1965+
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def))
19521966
return NULL;
19531967

19541968
result = PyDict_New();
@@ -2109,13 +2123,13 @@ match_regs(MatchObject* self)
21092123
}
21102124

21112125
static PyMethodDef match_methods[] = {
2112-
{"group", (PyCFunction) match_group, 1},
2113-
{"start", (PyCFunction) match_start, 1},
2114-
{"end", (PyCFunction) match_end, 1},
2115-
{"span", (PyCFunction) match_span, 1},
2116-
{"groups", (PyCFunction) match_groups, 1},
2117-
{"groupdict", (PyCFunction) match_groupdict, 1},
2118-
{"expand", (PyCFunction) match_expand, 1},
2126+
{"group", (PyCFunction) match_group, METH_VARARGS},
2127+
{"start", (PyCFunction) match_start, METH_VARARGS},
2128+
{"end", (PyCFunction) match_end, METH_VARARGS},
2129+
{"span", (PyCFunction) match_span, METH_VARARGS},
2130+
{"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS},
2131+
{"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS},
2132+
{"expand", (PyCFunction) match_expand, METH_VARARGS},
21192133
{NULL, NULL}
21202134
};
21212135

0 commit comments

Comments
 (0)