1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

flaskのソースコードを読んでいく(3)

Posted at

以下のソースコードを読んでいきます。

SecureCookieSessionのクラスの実装を読んでいきます。

class SecureCookieSession(CallbackDict, SessionMixin):
    """Base class for sessions based on signed cookies.
    This session backend will set the :attr:`modified` and
    :attr:`accessed` attributes. It cannot reliably track whether a
    session is new (vs. empty), so :attr:`new` remains hard coded to
    ``False``.
    """

    #: When data is changed, this is set to ``True``. Only the session
    #: dictionary itself is tracked; if the session contains mutable
    #: data (for example a nested dict) then this must be set to
    #: ``True`` manually when modifying that data. The session cookie
    #: will only be written to the response if this is ``True``.
    modified = False

    #: When data is read or written, this is set to ``True``. Used by
    # :class:`.SecureCookieSessionInterface` to add a ``Vary: Cookie``
    #: header, which allows caching proxies to cache different pages for
    #: different users.
    accessed = False

    def __init__(self, initial=None):
        def on_update(self):
            self.modified = True
            self.accessed = True

        super(SecureCookieSession, self).__init__(initial, on_update)

    def __getitem__(self, key):
        self.accessed = True
        return super(SecureCookieSession, self).__getitem__(key)

    def get(self, key, default=None):
        self.accessed = True
        return super(SecureCookieSession, self).get(key, default)

    def setdefault(self, key, default=None):
        self.accessed = True
        return super(SecureCookieSession, self).setdefault(key, default)
    """Base class for sessions based on signed cookies.
    This session backend will set the :attr:`modified` and
    :attr:`accessed` attributes. It cannot reliably track whether a
    session is new (vs. empty), so :attr:`new` remains hard coded to
    ``False``.
    """

SecureCookieSessionはCallbackDictとSessionMixinを継承します。Pythonは多重継承をサポートするようですね。
署名されたクッキーの上のセッションのための基底クラスです。セッションのバックエンドはmodified属性とaccessed属性を設定します。しかしながら、セッションが新しいかどうかを信頼性を持って追跡できません。よって、属性newはFalseにハードコードされています。

    #: When data is changed, this is set to ``True``. Only the session
    #: dictionary itself is tracked; if the session contains mutable
    #: data (for example a nested dict) then this must be set to
    #: ``True`` manually when modifying that data. The session cookie
    #: will only be written to the response if this is ``True``.
    modified = False

modified属性については、SessionMixinクラスから継承されています。データが変更された場合は、これはTrueに設定されます。セッションの辞書自体が追跡されているばあいのみ、もしも、セッションが、ミュータブルなデータが格納されていた場合、(例えばネストされた辞書)データを変更する時に、Trueに手動で変更されるべきです。もしこれがTrueだった場合、セッションクッキーはそれに反応して書き換えられるべきです。

    #: When data is read or written, this is set to ``True``. Used by
    # :class:`.SecureCookieSessionInterface` to add a ``Vary: Cookie``
    #: header, which allows caching proxies to cache different pages for
    #: different users.
    accessed = False

accessed属性は、SessionMixinクラスから継承されています。データが読まれたまたは書き込まれた時、Trueに設定されます。SecureCookieSessionInterfaceのクラスがVary: Cookieヘッダーを追加するのに使われます。これはキャッシングプロクシが違うページを違うユーザーのためのキャッシュするのを許します。

    def __init__(self, initial=None):
        def on_update(self):
            self.modified = True
            self.accessed = True

        super(SecureCookieSession, self).__init__(initial, on_update)

これはコンストラクタです。関数内の関数、つまりクロージャを使っています。スーパークラスを参照していますが、多重継承を使用しているため、どちらのスーパークラスを参照しているかどうかが謎になるわけです。ここで、CallbackDictのドキュメントを確認してみます。

メソッドの一つに、__init__([initial, on_update])がありますね。よって、CallbackDictのコンストラクタを呼び出していることがわかります。
クロージャのdef on_update(self):メソッドでは、modifiedプロパティと、accessedプロパティが両方とも、Trueに設定されています。updateしたときの挙動が見えます。
super(SecureCookieSession, self).__init__(initial, on_update)においての、SecureCookieSessionは、自分のクラスであるため省略しても大丈夫です。(逆になぜこれがあるのかわからない。自明なのに)

    def __getitem__(self, key):
        self.accessed = True
        return super(SecureCookieSession, self).__getitem__(key)

SecureCookieSessionはCallbackDictを継承しているため、基本的には辞書です。よって、キーに対応する値を取得するためのメソッドが必要です。これがまさにそれになります。
self.accessed = Trueのコードにより、このメソッドが実行された時、accessed属性がTrueになります。
スーパークラスであるCallbackDictクラスの__getitem__(key)メソッドを呼び出します。__getitem__は特殊なメソッドで、角カッコ[]でアクセスした時の値を返します。

    def get(self, key, default=None):
        self.accessed = True
        return super(SecureCookieSession, self).get(key, default)

キーに対応する値を返すメソッドです。CallbackDictのメソッドを呼び出しています。角カッコでアクセスした時の値を返す仕組みと、メソッドでアクセスした時の値を返す仕組み両方を準備しているんですね。

    def setdefault(self, key, default=None):
        self.accessed = True
        return super(SecureCookieSession, self).setdefault(key, default)

setdefaultは、キーが存在ない場合は、デフォルトの値を格納、キーが存在した場合は、元々の値を保持して値を変更しないメソッドです。これもCallbackDictのメソッドを呼び出しています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?