8000 add a warning to with_iter documentation for pypy users · Issue #895 · more-itertools/more-itertools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
add a warning to with_iter documentation for pypy users #895
Open
@olliemath

Description

@olliemath

Specifically, use of with_iter is dangerous for pypy users because if the entire iterator isn't consumed the __exit__ statement won't be called until the next major garbage collection. This could be never and the program can exit without ever calling __exit__. We have already seen some resource starvation in a production application from this.

Unfortunately this is just a quite niche difference between cpython and pypy, which is not going to change, so it seems best just to warn folks about this.

To see the difference, run the following under python3 / pypy3:

from more_itertools import with_iter

class Manager:
    def __iter__(self):
        yield
    def __enter__(self, *args, **kwargs):
        print("enter")
        return self
    def __exit__(self, *args, **kwargs):
        print("exit")

for _ in with_iter(Manager()):
    break  # exit called under python3, but not pypy3

try:
    for _ in with_iter(Manager()):
        raise Exception("oops")  # same
except Exception:
    pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0