Repository navigation
AttributeError when copying flags in the re module. #93820
Copy link
Copy link
Closed
Labels
3.11only security fixesonly security fixesrelease-blockertopic-regextype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 14, 2022 Thanks for reporting! Confirmed that
copy.copy(re.A | re.I)fails on 3.11 but works on 3.10. Marking as a release blocker since this is a regression.cc @ethanfurman for enums
Reacted by Phillip SchanelyAlso repros with
enum.IntFlagdirectly:$ ./python.exe Python 3.11.0b3+ (heads/3.11:7aa4038a6e, Jun 14 2022, 09:35:10) [Clang 13.0.0 (clang-1300.0.29.3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import copy, enum >>> class F(enum.IntFlag): ... a = 2 ... b = 4 ... >>> copy.copy(F.a | F.b) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/jelle/py/cpython/Lib/copy.py", line 102, in copy return _reconstruct(x, None, *rv) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/jelle/py/cpython/Lib/copy.py", line 265, in _reconstruct y = func(*args) ^^^^^^^^^^^ File "/Users/jelle/py/cpython/Lib/enum.py", line 862, in __getattr__ raise AttributeError(name) from None ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: a|bAlso occurs on 3.12
In 3.11 the
__reduce__and__reduce_ex__methods returns different values:# 3.10 >>> import re >>> flags = re.I | re.A >>> flags.__reduce__() (<function _reconstructor at 0x7f58bb775ea0>, (<enum 'RegexFlag'>, <class 'int'>, 258), {'_name_': None, '_value_': 258}) >>> flags.__reduce_ex__(1) (<enum 'RegexFlag'>, (258,)) >>> flags._name_ is None True >>> flags.name is None True
# 3.11 >>> import re >>> flags = re.I | re.A >>> flags.__reduce__() (<function _reconstructor at 0x7fd0b8d4f920>, (<flag 'RegexFlag'>, <class 'int'>, 258), {'_value_': 258, '_name_': 'ASCII|IGNORECASE'}) >>> flags.__reduce_ex__(1) (<built-in function getattr>, (<flag 'RegexFlag'>, 'ASCII|IGNORECASE')) >>> flags._name_ 'ASCII|IGNORECASE' >>> flags.name 'ASCII|IGNORECASE'
Oh, it looks like we came across it at the same time 😂
Reacted by Christian HeimesPR #93876 is a potential fix. Waiting for @ethanfurman to confirm.
Ideally,
re.I | re.Ashould be reconstructed asoperator.or_(re.ASCII, re.IGNORECASE).It gets a bit tricky when the flags have a residual member, e.g.
re.A | re.I | 0x10000. IntFlag supports additional values that are not covered by an enum member.- added 4 commits that reference this issue
on Jun 26, 2022
Metadata
Metadata
Assignees
Labels
3.11only security fixesonly security fixesrelease-blockertopic-regextype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
In CPython 3.11, I've noticed that an attempt to copy (combined) regular expression flags fails with an attribute error; like so:
Your environment
Background
This behavior was detected from a CrossHair test failure here, in which we realize possibly symbolic arguments prior to regex compilation. (it's an esoteric use case, and I expect not very high priority)