[Python-Dev] Another case for frozendict
R. David Murray
rdmurray at bitdance.com
Wed Jul 16 15:37:55 CEST 2014
On Wed, 16 Jul 2014 03:27:23 +0100, MRAB <python at mrabarnett.plus.com> wrote:
> Here's another use-case.
>
> Using the 're' module:
>
> >>> import re
> >>> # Make a regex.
> ... p = re.compile(r'(?P<first>\w+)\s+(?P<second>\w+)')
> >>>
> >>> # What are the named groups?
> ... p.groupindex
> {'first': 1, 'second': 2}
> >>>
> >>> # Perform a match.
> ... m = p.match('FIRST SECOND')
> >>> m.groupdict()
> {'first': 'FIRST', 'second': 'SECOND'}
> >>>
> >>> # Try modifying the pattern object.
> ... p.groupindex['JUNK'] = 'foobar'
> >>>
> >>> # What are the named groups now?
> ... p.groupindex
> {'first': 1, 'second': 2, 'JUNK': 'foobar'}
> >>>
> >>> # And the match object?
> ... m.groupdict()
> Traceback (most recent call last):
> File "<stdin>", line 2, in <module>
> IndexError: no such group
>
> It can't find a named group called 'JUNK'.
IMO, preventing someone from shooting themselves in the foot by modifying
something they shouldn't modify according to the API is not a Python
use case ("consenting adults").
> And with a bit more tinkering it's possible to crash Python. (I'll
> leave that as an exercise for the reader! :-))
Preventing a Python program from being able to crash the interpreter,
that's a use case :)
--David
More information about the Python-Dev
mailing list