Skip to content

Strange bug in try - finally block #13562

Closed
@AmirHmZz

Description

@AmirHmZz

This snippet is not working properly on Micropython compared to CPython:

class IDGenerator:
    def __init__(self, max_id: int):
        self._i = 0
        self._mi = max_id

    def get(self):
        try:
            return self._i
        finally:
            self._i += 1
            if self._i > self._mi:
                self._i = 0

id_gen = IDGenerator(10)
print(id_gen.get())

The output on Micropython v1.22.1 is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 11, in get
AttributeError: 'int' object has no attribute '_i'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpy-coreRelates to py/ directory in source

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions