This commit is contained in:
anilbey 2025-07-10 06:57:32 +03:00 committed by GitHub
commit 115bc399ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 2 deletions

View file

@ -1044,6 +1044,8 @@ Functions
.. versionchanged:: 3.7
Non-empty matches can now start just after a previous empty match.
.. versionchanged:: 3.13
Negative start and end indices will no longer be truncated to zero in Python 3.15.
.. function:: finditer(pattern, string, flags=0)
@ -1059,6 +1061,8 @@ Functions
.. versionchanged:: 3.7
Non-empty matches can now start just after a previous empty match.
.. versionchanged:: 3.13
Negative start and end indices will no longer be truncated to zero in Python 3.15.
.. function:: sub(pattern, repl, string, count=0, flags=0)

View file

@ -503,6 +503,18 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.findall(r"(a|(b))", "aba"),
[("a", ""),("b", "b"),("a", "")])
def test_bug_7940_raises_future_warnings(self):
"""Test to ensure the FutureWarnings are raised."""
pat = re.compile(".")
with self.assertWarns(FutureWarning) as cm:
pat.findall("abcd", -1, 1)
self.assertEqual(str(cm.warning), "Negative start index will not "
"be truncated to zero in Python 3.15")
with self.assertWarns(FutureWarning) as cm:
pat.findall("abcd", 1, -1)
self.assertEqual(str(cm.warning), "Negative end index will not "
"be truncated to zero in Python 3.15")
def test_re_match(self):
for string in 'a', S('a'):
self.assertEqual(re.match('a', string).groups(), ())

View file

@ -0,0 +1,2 @@
Emit a FutureWarning when truncating negative start and end indices in re.finditer
and re.findall.

View file

@ -572,13 +572,22 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
}
/* adjust boundaries */
if (start < 0)
if (start < 0) {
PyErr_WarnEx(PyExc_FutureWarning,
"Negative start index will not be truncated to zero in Python 3.15",
1);
start = 0;
}
else if (start > length)
start = length;
if (end < 0)
if (end < 0) {
PyErr_WarnEx(PyExc_FutureWarning,
"Negative end index will not be truncated to zero in Python 3.15",
1);
end = 0;
}
else if (end > length)
end = length;