flask
flask copied to clipboard
session.new is never true
According to the documentation, new attribute returns True if the session is new. Otherwise False, but it's not like that. How to reproduce:
from flask import Flask, session
app = Flask(__name__)
app.config["SECRET_KEY"] = "thisisasecret"
app.config['SESSION_TYPE'] = 'filesystem'
@app.route("/")
def index():
if 'user_id' not in session:
session['user_id'] = 'jon'
print(session.new)
print(session.modified)
return 'testing sessions'
# just for debugging
@app.route("/clear")
def clear():
session.clear()
return 'clear'
if __name__ == "__main__":
app.run(debug=True)
Run app and call / endpoint twice. Output:
127.0.0.1 - - [19/Dec/2017 16:18:20] "GET /clear HTTP/1.1" 200 -
False # value of session.new
True
127.0.0.1 - - [19/Dec/2017 16:18:25] "GET / HTTP/1.1" 200 -
False # value of session.new
False
As I Understood when we initialize session, session.new should return True(when we call endpoint at first time). Is it right?
Environment
- Python version: 2.7.13
- Flask version: 0.12.2
- Werkzeug version: 0.13
Link to question on SO
From the code:
#: some session backends can tell you if a session is new, but that is
#: not necessarily guaranteed. Use with caution. The default mixin
#: implementation just hardcodes ``False`` in.
new = False
@davidism So it's not a bug, it's a feature?
It's not implemented for the default cookie session.
Closing, see discussion at #2579. Updated documentation at #2580.