This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients ethan.furman, ezio.melotti, mrabarnett, serhiy.storchaka
Date 2019-04-07.10:28:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1554632881.49.0.427945168441.issue36548@roundup.psfhosted.org>
In-reply-to
Content
Currently the repr of re flags contains the name of the private class and it is not an evaluable expression:

>>> re.I
<RegexFlag.IGNORECASE: 2>
>>> re.I|re.S|re.X
<RegexFlag.VERBOSE|DOTALL|IGNORECASE: 82>

The repr of inverted flags is even more verbose:

<RegexFlag.ASCII|DEBUG|VERBOSE|UNICODE|DOTALL|MULTILINE|LOCALE|TEMPLATE: -3>
>>> ~(re.I|re.S|re.X)
<RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE: -83>

The result of str() starves from the same issues.

>>> print(re.I)
RegexFlag.IGNORECASE
>>> print(re.I|re.S|re.X)
RegexFlag.VERBOSE|DOTALL|IGNORECASE
>>> print(~re.I)
RegexFlag.ASCII|DEBUG|VERBOSE|UNICODE|DOTALL|MULTILINE|LOCALE|TEMPLATE
>>> print(~(re.I|re.S|re.X))
RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE

If the value contains unrecognized flags, it looks even more weird, and this information is not shown for invered flags:

>>> re.I|re.S|re.X|(1<<10)
<RegexFlag.1024|VERBOSE|DOTALL|IGNORECASE: 1106>
>>> ~(re.I|re.S|re.X|(1<<10))
<RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE: -1107>
>>> print(re.I|re.S|re.X|(1<<10))
RegexFlag.1024|VERBOSE|DOTALL|IGNORECASE
>>> print(~(re.I|re.S|re.X|(1<<10)))
RegexFlag.ASCII|DEBUG|UNICODE|MULTILINE|LOCALE|TEMPLATE

This repr is also not consistent with the represenation of flags in the repr of the compiled pattern:

>>> re.compile('x', re.I|re.S|re.X)
re.compile('x', re.IGNORECASE|re.DOTALL|re.VERBOSE)

The proposed PR makes the repr be the same as for flags in the repr of the compiled pattern and represents inverted flags as the result of inversion:

>>> re.I
re.IGNORECASE
>>> re.I|re.S|re.X
re.IGNORECASE|re.DOTALL|re.VERBOSE
>>> ~re.I
~re.IGNORECASE
>>> ~(re.I|re.S|re.X)
~(re.IGNORECASE|re.DOTALL|re.VERBOSE)
>>> re.I|re.S|re.X|(1<<10)
re.IGNORECASE|re.DOTALL|re.VERBOSE|0x400
>>> ~(re.I|re.S|re.X|(1<<10))
~(re.IGNORECASE|re.DOTALL|re.VERBOSE|0x400)

__str__ is set to object.__str__, so that str() will return the same as repr().
History
Date User Action Args
2019-04-07 10:28:01serhiy.storchakasetrecipients: + serhiy.storchaka, ezio.melotti, mrabarnett, ethan.furman
2019-04-07 10:28:01serhiy.storchakasetmessageid: <1554632881.49.0.427945168441.issue36548@roundup.psfhosted.org>
2019-04-07 10:28:01serhiy.storchakalinkissue36548 messages
2019-04-07 10:28:01serhiy.storchakacreate