classification
Title: re match.group should support __index__
Type: enhancement Stage: resolved
Components: Regular Expressions Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: ezio.melotti, jdemeyer, mrabarnett, python-dev, serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-06-01 14:20 by jdemeyer, last changed 2016-06-18 14:13 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
re_match_index.patch jdemeyer, 2016-06-01 14:20 Patch for Python 2.7
issue27177.patch xiang.zhang, 2016-06-15 07:02 review
Messages (9)
msg266817 - (view) Author: Jeroen Demeyer (jdemeyer) * Date: 2016-06-01 14:20
```
>>> class zero(object):
...     def __index__(self):
...         return 0
... 
>>> z = zero()
>>> import re
>>> p = re.compile('(a)b')
>>> m = p.match('ab')
>>> m.group(0)
'ab'
>>> m.group(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: no such group
```
msg266822 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-01 16:35
This looks as new feature and can go only in 3.6.
msg266884 - (view) Author: Jeroen Demeyer (jdemeyer) * Date: 2016-06-02 12:35
I would still argue that it's a bug. The intention of PEP 357 is that __index__ should be used whenever some object needs to be converted to a Py_ssize_t, which is exactly what you do here.
msg266885 - (view) Author: Matthew Barnett (mrabarnett) * Date: 2016-06-02 14:00
It would be a bug if it was supported but gave the wrong result.

It has never been supported (the re module predates PEP 357), so it's a new feature.
msg268606 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-06-15 07:02
Attach a patch to add this feature to Py3.6.
msg268775 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-18 07:37
What is the use case?
msg268797 - (view) Author: Jeroen Demeyer (jdemeyer) * Date: 2016-06-18 11:40
My use case is SageMath: http://trac.sagemath.org/ticket/20750
msg268800 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-06-18 13:06
Interesting. This is very unusual but reasonable use case.
msg268804 - (view) Author: Roundup Robot (python-dev) Date: 2016-06-18 13:48
New changeset 0303ab246152 by Serhiy Storchaka in branch 'default':
Issue #27177: Match objects in the re module now support index-like objects
https://hg.python.org/cpython/rev/0303ab246152
History
Date User Action Args
2016-06-18 14:13:40serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: needs patch -> resolved
2016-06-18 13:48:29python-devsetnosy: + python-dev
messages: + msg268804
2016-06-18 13:06:55serhiy.storchakasetassignee: serhiy.storchaka
messages: + msg268800
2016-06-18 11:40:28jdemeyersetmessages: + msg268797
2016-06-18 07:37:37serhiy.storchakasetmessages: + msg268775
2016-06-15 07:02:13xiang.zhangsetfiles: + issue27177.patch
nosy: + xiang.zhang
messages: + msg268606

2016-06-02 14:00:55mrabarnettsetmessages: + msg266885
2016-06-02 12:35:59jdemeyersetmessages: + msg266884
2016-06-01 16:35:13serhiy.storchakasetversions: + Python 3.6, - Python 2.7
nosy: + serhiy.storchaka

messages: + msg266822

type: enhancement
stage: needs patch
2016-06-01 14:20:08jdemeyercreate