Skip to content

Commit 149f6e4

Browse files
committed
Simplify, don't have duplicate definitions.
`match = prefixmatch`
1 parent bab8e8b commit 149f6e4

File tree

4 files changed

+38
-172
lines changed

4 files changed

+38
-172
lines changed

Lib/re/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,8 @@ def prefixmatch(pattern, string, flags=0):
167167
a Match object, or None if no match was found."""
168168
return _compile(pattern, flags).prefixmatch(string)
169169

170-
def match(pattern, string, flags=0):
171-
"""The original name for prefixmatch. Equivalent behavior.
172-
Try to apply the pattern at the start of the string, returning
173-
a Match object, or None if no match was found."""
174-
return _compile(pattern, flags).prefixmatch(string)
170+
# Our original less explicitly clear about the behavior name for prefixmatch.
171+
match = prefixmatch
175172

176173
def fullmatch(pattern, string, flags=0):
177174
"""Try to apply the pattern to all of the string, returning

Lib/test/test_inspect/test_inspect.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5812,7 +5812,10 @@ def test_pwd_module_has_signatures(self):
58125812

58135813
def test_re_module_has_signatures(self):
58145814
import re
5815-
methods_no_signature = {'Match': {'group'}}
5815+
methods_no_signature = {
5816+
'Match': {'group'},
5817+
'Pattern': {'match'}, # It is now an alias for prefixmatch
5818+
}
58165819
self._test_module_has_signatures(re,
58175820
methods_no_signature=methods_no_signature,
58185821
good_exceptions={'error', 'PatternError'})

Modules/_sre/clinic/sre.c.h

Lines changed: 1 addition & 121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_sre/sre.c

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -811,32 +811,6 @@ _sre_SRE_Pattern_prefixmatch_impl(PatternObject *self, PyTypeObject *cls,
811811
return match;
812812
}
813813

814-
/*[clinic input]
815-
_sre.SRE_Pattern.match
816-
817-
cls: defining_class
818-
/
819-
string: object
820-
pos: Py_ssize_t = 0
821-
endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize
822-
823-
Matches zero or more characters at the beginning of the string.
824-
825-
This is the legacy method name. Modern Python also provides it under the name
826-
'prefixmatch' to allow code to be explicitly clear about the intended behavior.
827-
828-
[clinic start generated code]*/
829-
830-
static PyObject *
831-
_sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,
832-
PyObject *string, Py_ssize_t pos,
833-
Py_ssize_t endpos)
834-
/*[clinic end generated code: output=ec6208ea58a0cca0 input=7541a63c722fcfdf]*/
835-
{
836-
/* TODO - https://github.com/python/cpython/issues/86519 - If we ever
837-
* want a PendingDeprecationWarning here, wait until year >=2030. */
838-
return _sre_SRE_Pattern_prefixmatch_impl(self, cls, string, pos, endpos);
839-
}
840814

841815
/*[clinic input]
842816
_sre.SRE_Pattern.fullmatch
@@ -2929,23 +2903,6 @@ _sre_SRE_Scanner_prefixmatch_impl(ScannerObject *self, PyTypeObject *cls)
29292903
return match;
29302904
}
29312905

2932-
/*[clinic input]
2933-
_sre.SRE_Scanner.match
2934-
2935-
cls: defining_class
2936-
/
2937-
2938-
[clinic start generated code]*/
2939-
2940-
static PyObject *
2941-
_sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
2942-
/*[clinic end generated code: output=6e22c149dc0f0325 input=b5146e1f30278cb7]*/
2943-
{
2944-
/* TODO(https://bugs.python.org/issue42353): Plan if we EVER want to
2945-
* issue a PendingDeprecationWarning here. */
2946-
return _sre_SRE_Scanner_prefixmatch_impl(self, cls);
2947-
}
2948-
29492906

29502907
/*[clinic input]
29512908
_sre.SRE_Scanner.search
@@ -3206,7 +3163,7 @@ pattern_richcompare(PyObject *lefto, PyObject *righto, int op)
32063163

32073164
static PyMethodDef pattern_methods[] = {
32083165
_SRE_SRE_PATTERN_PREFIXMATCH_METHODDEF
3209-
_SRE_SRE_PATTERN_MATCH_METHODDEF
3166+
{"match", NULL}, /* filled in by sre_exec() */
32103167
_SRE_SRE_PATTERN_FULLMATCH_METHODDEF
32113168
_SRE_SRE_PATTERN_SEARCH_METHODDEF
32123169
_SRE_SRE_PATTERN_SUB_METHODDEF
@@ -3334,7 +3291,7 @@ static PyType_Spec match_spec = {
33343291

33353292
static PyMethodDef scanner_methods[] = {
33363293
_SRE_SRE_SCANNER_PREFIXMATCH_METHODDEF
3337-
_SRE_SRE_SCANNER_MATCH_METHODDEF
3294+
{"match", NULL}, /* filled in by sre_exec() */
33383295
_SRE_SRE_SCANNER_SEARCH_METHODDEF
33393296
{NULL, NULL}
33403297
};
@@ -3438,11 +3395,40 @@ do { \
34383395
} \
34393396
} while (0)
34403397

3398+
3399+
static void
3400+
copy_prefixmatch_method_def_to_match(PyMethodDef *method_defs)
3401+
{
3402+
/* We could implement logic to scan the null filled sentry
3403+
* terminated list for the two method names. But we're a
3404+
* bunch of static structs. We just guarantee their position
3405+
* and flag deviation from this via debug build assertions.
3406+
*/
3407+
assert(method_defs);
3408+
PyMethodDef *prefixmatch_md = &method_defs[0];
3409+
assert(prefixmatch_md->ml_name != NULL);
3410+
assert(strcmp(prefixmatch_md->ml_name, "prefixmatch") == 0);
3411+
3412+
PyMethodDef *match_md = &method_defs[1];
3413+
assert(match_md->ml_name != NULL);
3414+
assert(strcmp(match_md->ml_name, "match") == 0);
3415+
/* If the public stable C API struct ever changed (!) and
3416+
* somehow wound up with unexpected layout and alignment
3417+
* constraints, fix the memcpy below. */
3418+
assert(offsetof(PyMethodDef, ml_meth) == sizeof(char *));
3419+
memcpy(&match_md->ml_meth, &prefixmatch_md->ml_meth,
3420+
sizeof(PyMethodDef) - offsetof(PyMethodDef, ml_meth));
3421+
}
3422+
3423+
34413424
static int
34423425
sre_exec(PyObject *m)
34433426
{
34443427
_sremodulestate *state;
34453428

3429+
copy_prefixmatch_method_def_to_match(pattern_methods);
3430+
copy_prefixmatch_method_def_to_match(scanner_methods);
3431+
34463432
/* Create heap types */
34473433
state = get_sre_module_state(m);
34483434
CREATE_TYPE(m, state->Pattern_Type, &pattern_spec);

0 commit comments

Comments
 (0)