0

today when I started coding my web page it worked properly, but suddenly my css file didn't work. My changes didn't update. After reading a bit, I read that I was supposed to clean my cache. I did so, and after that when I run my webpage and click on any button, I see the next output in my console:

127.0.0.1:50860 - - [22/Dec/2019 13:22:20] "HTTP/1.1 POST /check-login" - 200 OK
127.0.0.1:50861 - - [22/Dec/2019 13:22:20] "HTTP/1.1 GET /static/css/swap.css" - 304 Not Modified
RuntimeError('generator raised StopIteration')
127.0.0.1:50860 - - [22/Dec/2019 13:22:20] "HTTP/1.1 GET /" - 200 OK
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/web/httpserver.py", line 239, in __iter__
    raise StopIteration()
StopIteration

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1280, in communicate
    req.respond()
  File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1083, in respond
    self.server.gateway(self).respond()
  File "/usr/local/lib/python3.7/site-packages/cheroot/wsgi.py", line 145, in respond
    for chunk in filter(None, response):
RuntimeError: generator raised StopIteration
127.0.0.1:50860 - - [22/Dec/2019 13:22:20] "HTTP/1.1 GET /static/css/swap.css" - 304 Not Modified
RuntimeError('generator raised StopIteration')
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/web/httpserver.py", line 239, in __iter__
    raise StopIteration()
StopIteration

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1280, in communicate
    req.respond()
  File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1083, in respond
    self.server.gateway(self).respond()
  File "/usr/local/lib/python3.7/site-packages/cheroot/wsgi.py", line 145, in respond
    for chunk in filter(None, response):
RuntimeError: generator raised StopIteration

My main file looks like this:

import web   
from Models import RegisterModel, LoginModel

web.config.debug = False

urls = (
    '/', 'home',
    '/login', 'login',
    '/register', 'register',
    '/postregistration', 'postRegistration',
    '/check-login', 'checkLogin',
    '/logout', 'logout'

)

TEMPLATE_PATH = './Views/Templates'
BASE_FILE = 'MainLayout'


app = web.application(urls, globals())

session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={'user': None})
session_data = session._initializer

render = web.template.render(TEMPLATE_PATH, base=BASE_FILE, globals={'session': session_data, 'current_user': session_data["user"]})
# Classes/Routes

class home:
    def GET(self):
        return render.home()

class login:
    def GET(self):
        return render.login()

class checkLogin:
    def POST(self):
        data = web.input()

        login_model = LoginModel.LoginModel()
        isCorrect = login_model.check_user(data)

        if isCorrect:
            session_data["user"]=isCorrect
            return isCorrect

        return "error"

class logout:
    def GET(self):
        session['user'] = None
        session_data['user'] = None
        session.kill()
        return "success"

class register:
    def GET(self):
        return render.register()

class postRegistration:
    def POST(self):
        data = web.input()

        reg_model = RegisterModel.RegisterModel()
        reg_model.insert_user(data)

        return data.username

if __name__ == "__main__":
    app.run()

Let me know if you need further information about any file.

Thanks in advance.

sergiomahi
  • 964
  • 2
  • 8
  • 21

1 Answers1

1

Similar problem here. In python 3.7, rule of StopIteration has changed.

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.

You would need to use try-except in the error place like above link.

Edit

In your code, it seems that css isn't changed so 304 Not modified raise StopIteration.

In __iter__ function in ../site-packages/web/httpserver.py, you will find this part.

def __iter__(self):
    # ...
    try:
        path = self.translate_path(self.path)
        etag = '"%s"' % os.path.getmtime(path)
        client_etag = environ.get("HTTP_IF_NONE_MATCH")
        self.send_header("ETag", etag)
        if etag == client_etag:
            self.send_response(304, "Not Modified")
            self.start_response(self.status, self.headers)
            return
    except OSError:
        pass  # Probably a 404

Add this at the bottom.

except StopIteration:
    return

or use pass instead of return. That will verbosely rewrite the same things.

shimo
  • 2,156
  • 4
  • 17
  • 21
  • Thanks, I see what is going on now, but I still don't know where I am supposed to add the exception handler. And I also do not understand why it worked without this error yesterday and not today. – sergiomahi Dec 22 '19 at 13:26
  • I have add codes to my answer. Please try. About error you got today, search 304 Not Modified. – shimo Dec 23 '19 at 11:35