From ef0558d912a317f3df4310d4efea6f5ed5366fa1 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 15 Mar 2023 00:01:08 +0100 Subject: [PATCH 1/8] emit FutureWarning for negative start/end indices --- Modules/_sre/sre.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c index ddbdc9f478aab3f..c38e289a55e1022 100644 --- a/Modules/_sre/sre.c +++ b/Modules/_sre/sre.c @@ -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 the future", + 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 the future", + 1); end = 0; + } else if (end > length) end = length; From 1ac29f0bf684c77cf2cbde4a2690e8df0363bb5e Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 11:58:58 +0200 Subject: [PATCH 2/8] added news entry --- .../NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst diff --git a/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst b/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst new file mode 100644 index 000000000000000..93e185c5a80da94 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst @@ -0,0 +1,2 @@ +Emit a FutureWarning when truncating negative end positions in re.finditer +and re.findall. From 0b5980072772be80eb4a609c925339c831551ca9 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 12:04:02 +0200 Subject: [PATCH 3/8] update warning text, mention python3.15 --- Modules/_sre/sre.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c index c38e289a55e1022..3d443ab4782b4cf 100644 --- a/Modules/_sre/sre.c +++ b/Modules/_sre/sre.c @@ -455,7 +455,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, /* adjust boundaries */ if (start < 0) { PyErr_WarnEx(PyExc_FutureWarning, - "Negative start index will not be truncated to zero in the future", + "Negative start index will not be truncated to zero in Python 3.15", 1); start = 0; } @@ -465,7 +465,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, if (end < 0) { PyErr_WarnEx(PyExc_FutureWarning, - "Negative end index will not be truncated to zero in the future", + "Negative end index will not be truncated to zero in Python 3.15", 1); end = 0; } From 14359b613dd268652da3adf2bfa89f5126abfee8 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 12:09:22 +0200 Subject: [PATCH 4/8] use gh issue in the news item instead of bpo --- ...0.sXC663.rst => 2023-07-23-11-57-33.gh-issue-52188.sXC663.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Library/{2023-07-23-11-57-33.bpo-7940.sXC663.rst => 2023-07-23-11-57-33.gh-issue-52188.sXC663.rst} (100%) diff --git a/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst b/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst similarity index 100% rename from Misc/NEWS.d/next/Library/2023-07-23-11-57-33.bpo-7940.sXC663.rst rename to Misc/NEWS.d/next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst From a21c4eeb30fb3c745cf29d4ba82471c1bbc44040 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 12:23:54 +0200 Subject: [PATCH 5/8] update docs of re, mention future-warning --- Doc/library/re.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 3f03f0341d81661..7c1695ca64b5b82 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -951,6 +951,8 @@ Functions .. versionchanged:: 3.7 Non-empty matches can now start just after a previous empty match. + .. versionchanged:: 3.13 + Negative end positions will no longer be truncated to zero in Python 3.15. .. function:: finditer(pattern, string, flags=0) @@ -962,6 +964,8 @@ Functions .. versionchanged:: 3.7 Non-empty matches can now start just after a previous empty match. + .. versionchanged:: 3.13 + Negative end positions will no longer be truncated to zero in Python 3.15. .. function:: sub(pattern, repl, string, count=0, flags=0) From c98347e3dedf3feaf89263507dcc2bf19d12662a Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 12:37:59 +0200 Subject: [PATCH 6/8] add test to make sure FutureWarnings are raised --- Lib/test/test_re.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index a6f5af17d7d51ba..154255cbd30cc1b 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -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(), ()) From 124d3071f47ab6000b4059941faeba2070774960 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 12:38:18 +0200 Subject: [PATCH 7/8] update docs FutureWarning: mention start index as well --- Doc/library/re.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 7c1695ca64b5b82..fd8eadec4589107 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -952,7 +952,7 @@ Functions Non-empty matches can now start just after a previous empty match. .. versionchanged:: 3.13 - Negative end positions will no longer be truncated to zero in Python 3.15. + Negative start and end indices will no longer be truncated to zero in Python 3.15. .. function:: finditer(pattern, string, flags=0) @@ -965,7 +965,7 @@ Functions Non-empty matches can now start just after a previous empty match. .. versionchanged:: 3.13 - Negative end positions will no longer be truncated to zero in Python 3.15. + 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) From 8d661b6807fbcc623536c367ea2fe451708494b5 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Sun, 23 Jul 2023 12:39:18 +0200 Subject: [PATCH 8/8] update news item, mention start index as well --- .../next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst b/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst index 93e185c5a80da94..f8e710b0028e337 100644 --- a/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst +++ b/Misc/NEWS.d/next/Library/2023-07-23-11-57-33.gh-issue-52188.sXC663.rst @@ -1,2 +1,2 @@ -Emit a FutureWarning when truncating negative end positions in re.finditer +Emit a FutureWarning when truncating negative start and end indices in re.finditer and re.findall.