Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Enum] minor fixes and cleanup #110905

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

Closed
eendebakpt opened this issue Oct 15, 2023 · 9 comments
Closed

[Enum] minor fixes and cleanup #110905

eendebakpt opened this issue Oct 15, 2023 · 9 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@eendebakpt
Copy link
Contributor

eendebakpt commented Oct 15, 2023

Bug report

Bug description:

The _is_private method has incorrect results on attributes starting with a triple underscore. Example:

import enum
enum._is_private('X', '_X___test') # returns True

The relevant code is:

def _is_private(cls_name, name):
    pattern = '_%s__' % (cls_name, )
    pat_len = len(pattern)
    if (
            len(name) > pat_len
            and name.startswith(pattern)
            and name[pat_len:pat_len+1] != ['_']
            and (name[-1] != '_' or name[-2] != '_')
        ):
        return True
    else:
        return False

The check name[pat_len:pat_len+1] != ['_'] is always False, since name[pat_len:pat_len+1] is a str and ['_'] a list.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

@eendebakpt eendebakpt added the type-bug An unexpected behavior, bug, or error label Oct 15, 2023
@sunmy2019
Copy link
Member

sunmy2019 commented Oct 16, 2023

import enum
enum._is_private('X', '_X___test') # returns True

I don't see a problem with your example.

>>> class X:
...   ___x = 1
...
>>> X._X___x
1
>>> X.___x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'X' has no attribute '___x'. Did you mean: '_X___x'?

_X___x is indeed private since it is mangled.

@sunmy2019
Copy link
Member

sunmy2019 commented Oct 16, 2023

The comparison of str and list is indeed buggy.

It looks like bug + bug equals no bug.

@ethanfurman
Copy link
Member

Indeed - a self-cancelling bug! Or a bug and an anti-bug... hmm.

Anyway, the correct fix is just to remove that line.

@ethanfurman ethanfurman changed the title enum._is_private is invalid for triple underscore names [Enum] minor fixes and cleanup Oct 16, 2023
@ethanfurman
Copy link
Member

The docs about private variables.

@eendebakpt
Copy link
Contributor Author

Documentation: https://docs.python.org/3/tutorial/classes.html?highlight=private%20variable#private-variables:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually
replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 18, 2023
(cherry picked from commit a77180e)

Co-authored-by: Pieter Eendebak <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 18, 2023
(cherry picked from commit a77180e)

Co-authored-by: Pieter Eendebak <[email protected]>
@eendebakpt
Copy link
Contributor Author

Closing the issue as the problem is resolved on the main branch. There are open PRs with backports to 3.11 and 3.12, but those are nice-to-have (can be closed afaic).

@sunmy2019
Copy link
Member

@ethanfurman Will you merge the backport PR?

@ethanfurman
Copy link
Member

Still considering.

@ethanfurman
Copy link
Member

Closed the backports without merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants