为什么inspect.getsource不返回整个类源代码?
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:根据评论进行修改:
在
inspect.getsource(forms.ContactForm)
内部,方法BlockFinder.tokeneater()
用于确定在哪里ContactForm
块停止。 除此之外,它还会检查tokenize.DEDENT
,它会在存储在 github 的版本中的字段集之前找到它。 该行仅包含换行符,因此inspect
认为当前块已结束。如果你插入 4 个空格,它又对我有用了。 我无法争论这背后的基本原理,也许是性能。
inspect.getsource(forms)
工作方式不同的原因是,在这种情况下inspect
不必确定类定义的开始和结束。 它只是输出整个文件。edit: revised based on comments:
Inside
inspect.getsource(forms.ContactForm)
the methodBlockFinder.tokeneater()
is used to determine where theContactForm
block stops. Besides others, it checks fortokenize.DEDENT
, which it finds right before fieldsets in your version stored at github. The line contains only a line break, soinspect
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.
The reason that
inspect.getsource(forms)
works differently is becauseinspect
in that case does not have to determine the class definition's start and end. It simply outputs the whole file.对我有用。 我的代码中没有“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..