Hi everybody,

I'm doing some programming in Flask, and as part of the program I'm doing a basic system of logging in, but that's not where my problem resides. During my programming, the page that is supposed to verify a login absolutely refuses to go past a specific line of code, regardless of what's past that line or what that line is doing. Here's the code:

@app.route('/loginConfirm')
def loginConfirm(): 
    try:

        username = request.args.get('usernameField')
        password = request.args.get('passwordField')

        for user in usersList:
            if(username == user.username):
                print(user.hashedPassword == sha256(password.encode('utf-8')).hexdigest())
                if(user.hashedPassword == sha256(password.encode('utf-8')).hexdigest()):
                    session.pop('uuid')
                    session['uuid'] = user.uuid
                    return redirect('/')

        return redirect('/login')

    except:

        return redirect('/login')

In this example, it will get to the print statement where I print whether or not the password is equivalent to the other (this returns True when its supposed to and is working correctly.). The problem lies in the fact that if I were to add another print statement directly after that, or any other type of statement including the if statement that is already there, then it won't execute, but it also won't throw an error. What confuses me is that the fact that it occurs only on this line makes me think it's something to do with an indentation error or a problem with the fact that its in a try statement, but if either of those are true then I can't find the result. Any help is greatly appreciated.

Dani AI

Generated

This behaviour is almost always a debugging/visibility issue rather than a mysterious Python runtime bug. Two things are likely happening at once: an exception is being swallowed by the bare except and any subsequent output is either buffered or the request handler is exiting early (redirect/return or a worker crash). Follow these concrete steps to find the root cause.

  • Replace the unqualified except: so you can see the real traceback. Log the exception and re-raise (or let it propagate while you debug). Example pattern:
except Exception as e:
    app.logger.exception("loginConfirm failed")
    raise
  • Force-output of debug prints while you investigate: use print(..., flush=True) or the logging module so messages are not lost by buffering or WSGI workers (see the Python print docs linked below).

  • Avoid accidental KeyError on session.pop('uuid') by using a default: session.pop('uuid', None).

  • Use an interactive debugger (breakpoint() / pdb.set_trace()) or run Flask in debug mode so exceptions appear in the browser and you can step through the function (see Flask debugging docs linked below).

  • If running under gunicorn/uWSGI, check their error logs — worker crashes or silent exits will hide tracebacks in the Flask console.

This builds on 's simple-print idea and 's suggestion about the bare except. Start by getting an honest traceback and you will almost certainly find the cause quickly.

Python print docs: print() — built-in function

Flask debugging docs: Debugging — Flask

Recommended Answers

All 3 Replies

What happens if you comment out the print statement and replace it just with something simple like print('hello world')? What if you have two print statements in a row, as so:

print('hello')
print('world')

Do both of them execute?

commented: Despite changing literally nothing else, I did this and it started working, which is somewhat boggling to me. Anyway, the problems fixed. +1

You should never (ever) have an unqualified except. Doing so masks errors. Remove the try/except and let the error crash your code so you can see what the problem is.

commented: The reason the try/except is there is because it stops the page from crashing if there are no arguments present, as well as the session.pop() +1

The reason the try/except is there is because it stops the page from crashing if there are no arguments present, as well as the session.pop()

Then you should add an except clause for that specific exception. By having a generic exception handler you mask all other errors.

Let's try a real world example. You do your banking online but you don't want to get notified every time you do a transaction so you disable all notifications. Now you aren't getting emails for each time you use your debit card. But you also won't get a notification when someone gets access to your account and transfers out $50,000.

Incidentally, my account lets me specify "only notify me for transactions exceeding $x".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.