Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

python - "RuntimeError: generator raised StopIteration" every time I try to run app

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:UsersaidkeDesktop>python app.py
Traceback (most recent call last):
  File "C:UsersaidkeAppDataLocalProgramsPythonPython37-32libsite-packageswebutils.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:UsersaidkeAppDataLocalProgramsPythonPython37-32libsite-packageswebapplication.py", line 62, in __init__
    self.init_mapping(mapping)
  File "C:UsersaidkeAppDataLocalProgramsPythonPython37-32libsite-packageswebapplication.py", line 130, in init_mapping
    self.mapping = list(utils.group(mapping, 2))
  File "C:UsersaidkeAppDataLocalProgramsPythonPython37-32libsite-packageswebutils.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...