Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the repr of re flags more readable #80729

Closed
serhiy-storchaka opened this issue Apr 7, 2019 · 2 comments
Closed

Make the repr of re flags more readable #80729

serhiy-storchaka opened this issue Apr 7, 2019 · 2 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir topic-regex type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 36548
Nosy @ezio-melotti, @ethanfurman, @serhiy-storchaka
PRs
  • bpo-36548: Improve the repr of re flags. #12715
  • 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 = None
    closed_at = <Date 2019-06-10.08:02:04.009>
    created_at = <Date 2019-04-07.10:28:01.405>
    labels = ['expert-regex', '3.8', 'type-feature', 'library']
    title = 'Make the repr of re flags more readable'
    updated_at = <Date 2019-06-10.08:02:04.009>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-06-10.08:02:04.009>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-06-10.08:02:04.009>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)', 'Regular Expressions']
    creation = <Date 2019-04-07.10:28:01.405>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36548
    keywords = ['patch']
    message_count = 2.0
    messages = ['339567', '344031']
    nosy_count = 4.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'ethan.furman', 'serhiy.storchaka']
    pr_nums = ['12715']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue36548'
    versions = ['Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    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().

    @serhiy-storchaka serhiy-storchaka added 3.8 only security fixes stdlib Python modules in the Lib dir topic-regex type-feature A feature request or enhancement labels Apr 7, 2019
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 14a0e16 by Serhiy Storchaka in branch 'master':
    bpo-36548: Improve the repr of re flags. (GH-12715)
    14a0e16

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir topic-regex type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant