Message347915
Sorry, I was wrong. re.findall accepts negative indices for both start and end but they silently get converted to 0, which is arguably an unexpected behavior.
This is an example of the current behavior:
>>> s, e = 1, 4; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
(['b', 'c', 'd'], 'bcd')
>>> s, e = -4, 4; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
(['a', 'b', 'c', 'd'], 'bcd')
>>> s, e = 1, -1; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
([], 'bcd')
>>> s, e = -4, -1; re.compile('.').findall('abcde', s, e), 'abcde'[s:e]
([], 'bcd')
With the patch, all these return ['b', 'c', 'd']. This change might indeed cause issues because it's a change in behavior, but I'm also not sure there are many cases where one would want a negative index to be treated as 0. Maybe we could raise a FutureWarning in the next release and change the behavior afterwards? |
|
Date |
User |
Action |
Args |
2019-07-14 14:01:58 | ezio.melotti | set | recipients:
+ ezio.melotti, rhettinger, tlynn, timehorse, mrabarnett, docs@python, serhiy.storchaka |
2019-07-14 14:01:58 | ezio.melotti | set | messageid: <1563112918.31.0.236227535878.issue7940@roundup.psfhosted.org> |
2019-07-14 14:01:58 | ezio.melotti | link | issue7940 messages |
2019-07-14 14:01:58 | ezio.melotti | create | |
|