【问题标题】:Why doesn't inspect.getsource return the whole class source?为什么 inspect.getsource 不返回整个类源?
【发布时间】:2009-05-30 10:01:19
【问题描述】:

我的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 之前的部分而不是类的整个源?

【问题讨论】:

    标签: python inspect code-inspection


    【解决方案1】:

    编辑:根据 cmets 修改:

    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 在这种情况下不必确定类定义的开始和结束。它只是输出整个文件。

    【讨论】:

    • 没有。我没有重新加载()。我现在也删除了 FieldsetMixin 并再次尝试,源输出中仍然没有 fielsets
    • 哪个 Python 版本?我的是 2.6.2(release26-maint,2009 年 4 月 19 日,01:56:41)
    • Debian Lenny 上的 Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32)
    • 在我的 2.5.4 版本中,该错误仍然没有出现。您可以在某处提供 py 文件作为下载链接吗?可能是一些奇怪的字符,没有出现在上面的文字中?
    • 另一件事:您已将文件命名为 forms.py。这与 django forms 模块产生了名称冲突。
    【解决方案2】:

    为我工作。我的代码中没有“from formfieldset.forms import FieldsetMixin”。也许这会导致问题..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-27
      相关资源
      最近更新 更多