为什么inspect.getsource不返回整个类源代码?

发布于 2024-07-22 04:32:50 字数 2366 浏览 19 评论 0原文

我的 forms.py 中有这段代码:

from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

当我尝试使用 inspect 以编程方式提取代码时,它遗漏了 fieldsets

In [1]: import inspect

In [2]: import forms

In [3]: print inspect.getsource(forms)
from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

In [4]: print inspect.getsource(forms.ContactForm)
class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)


In [5]:      

这不会似乎是空行的问题。 我已经测试了中间没有空行,并且在其他属性之间放置了额外的空行。 结果不会改变。

任何想法为什么检查只返回fieldsets之前的部分而不是类的整个源代码?

I have this code in my forms.py:

from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

When I try to extract the code programmatically with inspect it leaves out fieldsets:

In [1]: import inspect

In [2]: import forms

In [3]: print inspect.getsource(forms)
from django import forms
from formfieldset.forms import FieldsetMixin


class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)

    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

In [4]: print inspect.getsource(forms.ContactForm)
class ContactForm(forms.Form, FieldsetMixin):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)


In [5]:      

This doesn't seem to be an issue with blank lines. I've tested without the blank line in between and I've put additional blank lines in between other attributes. Results don't change.

Any ideas why inspect is returning only the part before fieldsets and not the whole source of the class?

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

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

发布评论

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

评论(2

浅暮の光 2024-07-29 04:32:50

编辑:根据评论进行修改:

inspect.getsource(forms.ContactForm)内部,方法BlockFinder.tokeneater()用于确定在哪里ContactForm 块停止。 除此之外,它还会检查 tokenize.DEDENT,它会在存储在 github 的版本中的字段集之前找到它。 该行仅包含换行符,因此 inspect 认为当前块已结束。

如果你插入 4 个空格,它又对我有用了。 我无法争论这背后的基本原理,也许是性能。

class ContactForm(forms.Form):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)
    # <-- insert 4 spaces here
    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

inspect.getsource(forms) 工作方式不同的原因是,在这种情况下 inspect 不必确定类定义的开始和结束。 它只是输出整个文件。

edit: revised based on comments:

Inside inspect.getsource(forms.ContactForm) the method BlockFinder.tokeneater() is used to determine where the ContactForm block stops. Besides others, it checks for tokenize.DEDENT, which it finds right before fieldsets in your version stored at github. The line contains only a line break, so inspect thinks the current block has ended.

If you insert 4 spaces, it works for me again. I cannot argue on the rationale behind this, maybe performance.

class ContactForm(forms.Form):
    full_name = forms.CharField(max_length=120)
    email = forms.EmailField()
    website = forms.URLField()
    message = forms.CharField(max_length=500, widget=forms.Textarea)
    send_notification = forms.BooleanField(required=False)
    # <-- insert 4 spaces here
    fieldsets = ((u'Personal Information',
                {'fields': ('full_name', 'email', 'website'),
                'description': u'Your personal information will not ' \
                                u'be shared with 3rd parties.'}),
                (None,
                {'fields': ('message',),
                'description': u'All HTML will be stripped out.'}),
                (u'Preferences',
                {'fields': ('send_notification',)}))

The reason that inspect.getsource(forms) works differently is because inspect in that case does not have to determine the class definition's start and end. It simply outputs the whole file.

骑趴 2024-07-29 04:32:50

对我有用。 我的代码中没有“from formfieldset.forms import FieldsetMixin”。 也许这引起了一个问题..

Works for me. I don't have "from formfieldset.forms import FieldsetMixin" in my code. Maybe that is causing an issue..

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