You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
re.sub appears not to check count optional argument for integerness #73091
bpo-28082: re: convert re flags to (much friendlier) IntFlag constants
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2016-12-08.18:37:08.796>created_at=<Date2016-12-08.17:02:58.334>labels= ['type-bug', 'library']
title='re.sub appears not to check count optional argument for integerness'updated_at=<Date2016-12-08.18:37:08.794>user='https://bugs.python.org/DannyYoo'
Ugh. I suddenly realize that this is complicated by the fact that flag values are themselves represented as integers, and Python's type system isn't rich enough to label flag values as a distinct type for the purposes.
It may be worthwhile to add a warning in the documentation about this, as it is an easy mistake to make.
DannyYoo commented on Dec 8, 2016
DannyYoomannequin
MannequinAuthor
More actions
Alternatively, change the representation of flag values from integers to some class extension that supports the common bitwise operators.
That way, flag arguments can be determined at runtime to have derived from the proper flag values.
This kind of approach may have some backwards-incompatibility, unfortunately, since other folks have been hardcoding integers rather than use the flag constants. Other concerns might include serialization, in case someone tries to save a FlagInt somewhere and pull it out at some other time.
Activity
DannyYoo commentedon Dec 8, 2016
This comes from diagnosing a beginner's question on Python-tutor.
https://mail.python.org/pipermail/tutor/2016-December/110066.html
It appears that re.sub is not checking whether the count argument is integer or not, and silently accepts a nonsensical argument. For example:
Of course, the user intended to pass re.MULTILINE to flags, not to count, but the fact that this isn't raising a TypeError is error-prone.
DannyYoo commentedon Dec 8, 2016
Ugh. I suddenly realize that this is complicated by the fact that flag values are themselves represented as integers, and Python's type system isn't rich enough to label flag values as a distinct type for the purposes.
It may be worthwhile to add a warning in the documentation about this, as it is an easy mistake to make.
DannyYoo commentedon Dec 8, 2016
Alternatively, change the representation of flag values from integers to some class extension that supports the common bitwise operators.
As a very rough sketch:
That way, flag arguments can be determined at runtime to have derived from the proper flag values.
This kind of approach may have some backwards-incompatibility, unfortunately, since other folks have been hardcoding integers rather than use the flag constants. Other concerns might include serialization, in case someone tries to save a FlagInt somewhere and pull it out at some other time.
bitdancer commentedon Dec 8, 2016
See bpo-28082.