“RuntimeError: generator raised StopIteration” eve

2019-03-27 07:58发布

问题:

I am trying to run this code:

import web

urls = (
    '/', 'index'
)

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

But it gives me this error everytime

C:\Users\aidke\Desktop>python app.py
Traceback (most recent call last):
  File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 526, in take
    yield next(seq)
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app.py", line 14, in <module>
    app = web.application(urls, globals())
  File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\application.py", line 62, in __init__
    self.init_mapping(mapping)
  File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\application.py", line 130, in init_mapping
    self.mapping = list(utils.group(mapping, 2))
  File "C:\Users\aidke\AppData\Local\Programs\Python\Python37-32\lib\site-packages\web\utils.py", line 531, in group
    x = list(take(seq, size))
RuntimeError: generator raised StopIteration

I tried someone else's code and the exact same thing happened. Additionally I tried reinstalling web.py(experimental) but it still didn't work.

回答1:

To judge from the file paths, it looks like you're running Python 3.7. If so, you're getting caught by new-in-3.7 behavior described here:

PEP 479 is enabled for all code in Python 3.7, meaning that StopIteration exceptions raised directly or indirectly in coroutines and generators are transformed into RuntimeError exceptions. (Contributed by Yury Selivanov in bpo-32670.)

Before this change, a StopIteration raised by, or passing through, a generator simply ended the generator's useful life (the exception was silently swallowed). The module you're using will have to be recoded to work as intended with 3.7.

Chances are they'll need to change:

yield next(seq)

to:

try:
    yield next(seq)
except StopIteration:
    return