V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2019-07-03 16:10:59 +08:00 · 2249 次点击
    这是一个创建于 1775 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2019-07-05 00:00:47 +08:00
    est
        1
    est  
       2019-07-03 16:22:19 +08:00
    - 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
        2
    est  
       2019-07-03 16:22:50 +08:00   ❤️ 1
    - cookie is too large, then it may be silently ignored
    + cookie is too large, then it may be silently ignored by the browser
    hzwjz
        3
    hzwjz  
    OP
       2019-07-03 16:34:14 +08:00
    @est #2 意思就是如果 cookie_size 大于 max_size 时,发出警告,但是在浏览器上,对于 cookie 的 size 太大,则忽略。看来是自己粗心了,漏掉了 warn 函数里面的信息了。
    hzwjz
        4
    hzwjz  
    OP
       2019-07-03 16:34:14 +08:00
    @est #2 意思就是如果 cookie_size 大于 max_size 时,发出警告,但是在浏览器上,对于 cookie 的 size 太大,则忽略。看来是自己粗心了,漏掉了 warn 函数里面的信息了。
    ryd994
        5
    ryd994  
       2019-07-03 16:39:25 +08:00
    # 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
        6
    hzwjz  
    OP
       2019-07-03 17:51:06 +08:00
    @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
        7
    zkqiang  
       2019-07-03 18:19:19 +08:00
    可以去提 PR 了
    hzwjz
        9
    hzwjz  
    OP
       2019-07-03 22:14:24 +08:00
    @zkqiang #7 像是#1 的 v 友 @est 已经 PR 了:satisfied:
    est
        10
    est  
       2019-07-05 00:00:47 +08:00
    @hzwjz github.com/pallets/werkzeug/pull/1598 是的。已经 merge 了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2827 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:23 · PVG 17:23 · LAX 02:23 · JFK 05:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.