Skip to content

[Enum] verify'ing a Flag class with negative values results in MemoryError #99248

Closed

Description

@udhayprakash

Bug report

A clear and concise description of what the bug is.
Include a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example), if possible.

Your environment

from enum import Flag, verify, NAMED_FLAGS

@verify(NAMED_FLAGS)
class Color(Flag):
    RED = -1 # 0
    GREEN = 2
    BLUE = 4
    WHITE = 9 
    NEON = 6

In this example, it is working good, if RED has value of 0, but is resulting in Memory Error, if its value is changed to -1

  • CPython versions tested on: 3.11.0
  • Operating system and architecture: Windows 10, 64 bit

Activity

  1. JelleZijlstra commented on Nov 8, 2022

    @JelleZijlstra
    Member

    The problem is that enum._iter_bits_lsb (

    def _iter_bits_lsb(num):
    ) doesn't terminate for negative numbers, the number just grows unbounded.

    Negative numbers don't really make sense as OR-able flags (which are conceptually unsigned), but we should fail with an obvious error instead of using all available memory.

    cc @ethanfurman for enums

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 8, 2022
  3. self-assigned this
    on Nov 8, 2022
  4. changed the title [-]Enum Flag class with one negative value is resulting in Memory Error[/-] [+][Enum] verify'ing a Flag class with negative values results in MemoryError[/+] on Nov 8, 2022
  5. added a commit that references this issue on Nov 8, 2022
  6. added 2 commits that reference this issue on Nov 8, 2022
  7. ethanfurman commented on Nov 8, 2022

    @ethanfurman
    Member

    Thanks for the report. Fix merged into main and 3.11.

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

Metadata

Metadata

Assignees

Labels

performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions