推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
hzwjz
V2EX  ›  Python

在读 werkzeug 的源码,我有个疑惑?

  •  
  •   hzwjz · Jul 3, 2019 · 3025 views
    This topic created in 2697 days ago, the information mentioned may be changed or developed.

    werkzeug

        # Warn if the final value of the cookie is less than the limit. If the
        # cookie is too large, then it may be silently ignored, which can be quite
        # hard to debug.
        cookie_size = len(rv)
    
        if max_size and cookie_size > max_size:
            value_size = len(value)
            warnings.warn(
                'The "{key}" cookie is too large: the value was {value_size} bytes'
                " but the header required {extra_size} extra bytes. The final size"
                " was {cookie_size} bytes but the limit is {max_size} bytes."
                " Browsers may silently ignore cookies larger than this.".format(
                    key=key,
                    value_size=value_size,
                    extra_size=cookie_size - value_size,
                    cookie_size=cookie_size,
                    max_size=max_size,
                ),
                stacklevel=2,
            )
    
        return rv
    

    说一下自己对注释的理解,如果 cookie 的最终值小于限定的值,则产生警告信息。如果 cookie 太大了,则忽略,因此可能难以进行调试。

    但是可以看到,后续的if语句是判断 max_size 为 True,并且 cookie_size > max_size 时,会发出警告。

    我在 werkzeug 的 discord 里面问过这个问题,也没人回答,不知道是不是自己理解得不对还是怎么的?还请 v 友们多多指教。

    9 replies  •  2019-07-05 00:00:47 +08:00
    est
    est
    Jul 3, 2019
    1
    - Warn if the final value of the cookie is less than the limit
    + Warn if the final value of the cookie is larger than the limit
    est
    est
    Jul 3, 2019❤️1
    2
    - cookie is too large, then it may be silently ignored
    + cookie is too large, then it may be silently ignored by the browser
    hzwjz
    hzwjz
    OP
    Jul 3, 2019
    3
    @est #2 意思就是如果 cookie_size 大于 max_size 时,发出警告,但是在浏览器上,对于 cookie 的 size 太大,则忽略。看来是自己粗心了,漏掉了 warn 函数里面的信息了。
    hzwjz
    hzwjz
    OP
    Jul 3, 2019
    4
    @est #2 意思就是如果 cookie_size 大于 max_size 时,发出警告,但是在浏览器上,对于 cookie 的 size 太大,则忽略。看来是自己粗心了,漏掉了 warn 函数里面的信息了。
    ryd994
    ryd994
    Jul 3, 2019
    5
    # Warn if the final value of the cookie is less than the limit. If the
    # cookie is too large, then it may be silently ignored, which can be quite
    # hard to debug.

    我的理解是,如果 cookie 太大,那可能没有错误信息就挂掉了。这就很难调试。所以如果 cookie 太大,就改用以下错误信息而不显示 cookie 内容。这样至少你能看见一部分错误信息。

    may 此除是有可能的意思,而不是允许、会发生的意思
    hzwjz
    hzwjz
    OP
    Jul 3, 2019
    6
    @ryd994 #5 它的注释跟代码判断逻辑不合呢,注释是 less than,而代码的逻辑是 larger than,这就是我疑惑的地方。

    综上,注释的首行应该改为 1L 的 Warn if the final value of the cookie is larger than the limit,同时我也这么认为。而说到“ If the
    cookie is too large, then it may be silently ignored, which can be quite hard to debug.”指的应该是在浏览器中可能忽略掉这个太大的 cookie。但是警告信息只能在服务端看到吧。
    zkqiang
    zkqiang
    Jul 3, 2019
    7
    可以去提 PR 了
    hzwjz
    hzwjz
    OP
    Jul 3, 2019
    9
    @zkqiang #7 像是#1 的 v 友 @est 已经 PR 了:satisfied:
    est
    est
    Jul 5, 2019
    10
    @hzwjz github.com/pallets/werkzeug/pull/1598 是的。已经 merge 了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2102 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 04:06 · PVG 12:06 · LAX 20:06 · JFK 23:06
    ♥ Do have faith in what you're doing.