Skip to content
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

iterating or extending Function produces an infinite loop #2606

Closed
joshkaplan opened this issue Aug 18, 2022 · 2 comments
Closed

iterating or extending Function produces an infinite loop #2606

joshkaplan opened this issue Aug 18, 2022 · 2 comments

Comments

@joshkaplan
Copy link

Problem

Both of these examples produce an infinite loop:

import peewee

pwf = peewee.fn.count()

# example 1:
for i in pwf:
    print(f'hello {str(i)}')

# example 2:
x = []
x.extend(pwf)

The offending code is ColumnBase.__getitem__: https://github.com/coleifer/peewee/blob/master/peewee.py#L1236-L1242

I'm not sure if this only applies to Function, or if it affects all non-slice ColumnBase classes.

For context, we did run into this in a real-world use-case.

Expected Behavior

I'm not 100% sure, but at the very least this probably should raise rather than infinitely loop, which is worse because it is silent and effectively a memory leak.

Proposed Solutions

(Disclaimer: I'm not deeply familiar with peewee's internals, so this is more directional than a fully thought-out solution.)

From the Python docs:

Note for loops expect that an IndexError will be raised for illegal indexes to allow proper detection of the end of the sequence.

So if raising in all non-slice cases doesn't make sense, perhaps just raising an IndexError at the appropriate time (e.g. after 1 loop)?

@coleifer coleifer reopened this Aug 18, 2022
@coleifer
Copy link
Owner

We don't want to raise an IndexError since it is not inherently an iterable type, so I'm going with NotImplementedError for now.

@coleifer
Copy link
Owner

3.15.2 released now, which contains this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants