I have a with
statement in my code:
@app.route('/users', methods = ['POST'])
def registerUser():
....
if email is None:
errorsList.append(Error("email","Email address not entered"))
else:
# Check if email address is already in database
with contextlib.closing(DBSession()) as session:
if session.query(USER).filter_by(USEREMAIL=email).count():
errorsList.append(Error("email","This email address already exists"))
# Add user to database
user = USER(email,password)
session.add(user)
session.commit()
When I run this code, it works fine. However, I was expecting an error to occur because I thought session
would be out of the with
statement's scope and hence undefined
?
I haven't defined session
anywhere else within this function or globally.