Skip to content

Strange bug in try - finally block #13562

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy