Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,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)

Expand All @@ -962,6 +964,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)

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,18 @@ def test_bug_117612(self):
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(), ())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Emit a FutureWarning when truncating negative start and end indices in re.finditer
and re.findall.
13 changes: 11 additions & 2 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,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;

Expand Down