Pydev 控制台:检查变量值似乎导致代码意外执行

发布于 2024-12-29 04:17:12 字数 743 浏览 1 评论 0原文

我正在尝试使用 Eclipse/Pydev 在 django 中逐步完成表单验证的整个过程。我得到了一个完全出乎意料的结果。

parent_form = form_class(request.POST, initial=initial)
debug_type = type(parent_form._errors)
msg = _('created successfully')  # <------- Set breakpoint here

这是我在控制台中摸索得到的结果。

debug_type
<type 'NoneType'>
type(parent_form._errors)
<class 'django.forms.util.ErrorDict'>

我不明白为什么这两个值不同;第一个值应该是“正确的”。

在django源代码中,存在名为parent_form.errors的东西(注意错误之前缺少前导下划线),它是parent_form的属性;获取该属性会运行一些代码,该代码会导致 _errors 从 None 变为 ErrorDict。但我没有得到parent_form.errors,我要求parent_form._errors。

PyDev 是否​​会在不询问的情况下评估parent_form.errors?如果是这样为什么?为什么我不能通过在parent_form.errors 的getter 中设置断点来捕获这种静默评估?

I'm trying to step through the whole process of form validation in django, using Eclipse/Pydev. I'm getting a totally unexpected result.

parent_form = form_class(request.POST, initial=initial)
debug_type = type(parent_form._errors)
msg = _('created successfully')  # <------- Set breakpoint here

Here's what I get from poking around in the console.

debug_type
<type 'NoneType'>
type(parent_form._errors)
<class 'django.forms.util.ErrorDict'>

I don't understand why the two values are different; the first value is what should be "correct".

In the django source, there exists something called parent_form.errors (note the lack of leading underscore before errors) which is a property of parent_form; getting that property runs the bit of code that would cause _errors to go from None to ErrorDict. But I'm not getting parent_form.errors, I'm asking for parent_form._errors.

Could it be that PyDev evaluates parent_form.errors without asking for it? If so why? And why can't I catch this silent evaluation by setting breakpoints in the getter for parent_form.errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2025-01-05 04:17:12

当您遇到断点并且 PyDev 填充变量视图(您可以在其中看到每个变量的值)时,它将对范围内的所有变量执行 dir() 操作,并对找到的每个变量执行 getattr() 操作(这对调试器的作用有一点简化,但它很接近)...

所以,它不能真正猜测某个变量是否会产生副作用...您可以尝试关闭变量视图检查是否会仅在实际请求变量时才执行此操作(尽管它可能会请求某些内容,即使它已关闭,但不确定)。

When you hit a breakpoint and PyDev fills the variables view (where you can see the value of each variable), it'll do a dir() on all the variables in the scope and a getattr() for each of those variables found (that's a bit of a simplification on what the debugger does, but it's close to it)...

So, it can't really guess if some variable would have some side-effect or not... You can try closing the variables view to check if it'll only do that if the variables are actually requested (although it could be that it requests some things even if it's closed, not sure about that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文