flask icon indicating copy to clipboard operation
flask copied to clipboard

session.new is never true

Open d-ganchar opened this issue 8 years ago • 4 comments

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

d-ganchar avatar Dec 19 '17 13:12 d-ganchar

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 avatar Dec 19 '17 14:12 davidism

@davidism So it's not a bug, it's a feature?

pawel1830 avatar Dec 20 '17 11:12 pawel1830

It's not implemented for the default cookie session.

davidism avatar Dec 20 '17 13:12 davidism

Closing, see discussion at #2579. Updated documentation at #2580.

davidism avatar Jan 04 '18 21:01 davidism