From f864aa8fd9afc1410dd7dc4d147c3bd8b7d5342a Mon Sep 17 00:00:00 2001 From: Fredrik Lundh Date: Mon, 22 Oct 2001 06:01:56 +0000 Subject: [PATCH] sre.split should return the last segment, even if empty (sorry, barry) --- Lib/test/test_sre.py | 1 + Modules/_sre.c | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_sre.py b/Lib/test/test_sre.py index e8791519d50..75a168cb9c0 100644 --- a/Lib/test/test_sre.py +++ b/Lib/test/test_sre.py @@ -155,6 +155,7 @@ if verbose: print 'Running tests on sre.split' test(r"""sre.split(r":", ":a:b::c")""", ['', 'a', 'b', '', 'c']) +test(r"""sre.split(r":+", ":a:b:::")""", ['', 'a', 'b', '']) test(r"""sre.split(r":*", ":a:b::c")""", ['', 'a', 'b', 'c']) test(r"""sre.split(r"(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c']) test(r"""sre.split(r"(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c']) diff --git a/Modules/_sre.c b/Modules/_sre.c index 5573046d1fa..73ffa70d1e4 100644 --- a/Modules/_sre.c +++ b/Modules/_sre.c @@ -2007,17 +2007,16 @@ pattern_split(PatternObject* self, PyObject* args, PyObject* kw) } - /* get segment following last match */ - i = STATE_OFFSET(&state, last); - if (i < state.endpos) { - item = PySequence_GetSlice(string, i, state.endpos); - if (!item) - goto error; - status = PyList_Append(list, item); - Py_DECREF(item); - if (status < 0) - goto error; - } + /* get segment following last match (even if empty) */ + item = PySequence_GetSlice( + string, STATE_OFFSET(&state, last), state.endpos + ); + if (!item) + goto error; + status = PyList_Append(list, item); + Py_DECREF(item); + if (status < 0) + goto error; state_fini(&state); return list;