GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AttributeError when copying flags in the `re` module. · Issue #93820 · python/cpython

AttributeError when copying flags in the re module. #93820

New issue
Copy link
New issue
Copy link
Closed
Closed
AttributeError when copying flags in the re module.#93820
Copy link
Assignees
ethanfurman
Labels
3.11only security fixesonly security fixesrelease-blockertopic-regextype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@pschanely
pschanely
opened on Jun 14, 2022
Issue body actions

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:

>>> import copy, re
>>> copy.copy(re.A | re.I)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lib/python3.11/copy.py", line 102, in copy
    return _reconstruct(x, None, *rv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.11/copy.py", line 265, in _reconstruct
    y = func(*args)
        ^^^^^^^^^^^
  File "lib/python3.11/enum.py", line 862, in __getattr__
    raise AttributeError(name) from None
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: ASCII|IGNORECASE

Your environment

  • CPython versions tested on: 3.11.0b3
  • Operating system and architecture: macOS 10.15.7

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)

Reacted by Shantanu

Activity

  1. pschanely
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 14, 2022
  2. JelleZijlstra
    added
    3.11only security fixesonly security fixes
    release-blocker
    on Jun 14, 2022
  3. JelleZijlstra commented on Jun 14, 2022

    @JelleZijlstra
    JelleZijlstra
    on Jun 14, 2022
    Member
    More actions

    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 Schanely
  4. JelleZijlstra
    added
    topic-regex
    on Jun 14, 2022
  5. JelleZijlstra commented on Jun 14, 2022

    @JelleZijlstra
    JelleZijlstra
    on Jun 14, 2022
    Member
    More actions

    Also repros with enum.IntFlag directly:

    $ ./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|b
    
  6. kumaraditya303
    assigned
    ethanfurman
    on Jun 15, 2022
  7. dignissimus commented on Jun 15, 2022

    @dignissimus
    dignissimus
    on Jun 15, 2022
    Contributor
    More actions

    Also occurs on 3.12

  8. tiran commented on Jun 15, 2022

    @tiran
    tiran
    on Jun 15, 2022
    Member
    More actions

    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'
  9. tiran commented on Jun 15, 2022

    @tiran
    tiran
    on Jun 15, 2022
    Member
    More actions

    I guess the regression was introduced by GH-26658 and commit 62f1d2b .

    A quick fix would be to reduce flags by value instead of by name.

    class Flag(Enum, boundary=STRICT):
        def __reduce_ex__(self, proto):
            return self.__class__, (self._value_, )
    
  10. dignissimus commented on Jun 15, 2022

    @dignissimus
    dignissimus
    on Jun 15, 2022
    Contributor
    More actions

    Running the example in 0507303 so works fine, so commit 62f1d2b introduced the issue

  11. dignissimus commented on Jun 15, 2022

    @dignissimus
    dignissimus
    on Jun 15, 2022
    Contributor
    More actions

    Oh, it looks like we came across it at the same time 😂

    Reacted by Christian Heimes
  12. tiran commented on Jun 15, 2022

    @tiran
    tiran
    on Jun 15, 2022
    Member
    More actions

    PR #93876 is a potential fix. Waiting for @ethanfurman to confirm.

  13. miss-islington
    added 2 commits that reference this issue on Jun 16, 2022

    gh-93820: Fix copy() regression in enum.Flag (GH-93876)

    05b32c1

    pythongh-93820: Fix copy() regression in enum.Flag (pythonGH-93876)

    00d46e7
  14. serhiy-storchaka commented on Jun 16, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Jun 16, 2022
    Member
    More actions

    Ideally, re.I | re.A should be reconstructed as operator.or_(re.ASCII, re.IGNORECASE).

  15. tiran commented on Jun 16, 2022

    @tiran
    tiran
    on Jun 16, 2022
    Member
    More actions

    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.

  16. serhiy-storchaka
    added 2 commits that reference this issue on Jun 16, 2022

    pythongh-93820: Pickle enum.Flag by name

    0814335

    gh-93820: Fix copy() regression in enum.Flag (GH-93876) (#93886)

    3fbf5c6
  17. ethanfurman
    closed this as completedon Jun 21, 2022
  18. serhiy-storchaka
    added 4 commits that reference this issue on Jun 26, 2022

    Revert 30935 defer bpo45162 to 312 (#94285)

    1ffde3e

    gh-93820: Pickle enum.Flag by name (GH-93891)

    5369858

    [3.11] pythongh-93820: Pickle enum.Flag by name (pythonGH-93891).

    632a1eb

    [3.11] gh-93820: Pickle enum.Flag by name (GH-93891). (GH-94288)

    1b27ec5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @ethanfurman
    ethanfurman

Labels

3.11only security fixesonly security fixesrelease-blockertopic-regextype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.