Python 为什么';t inspect.getsource是否返回整个类源?
我的Python 为什么';t inspect.getsource是否返回整个类源?,python,inspect,code-inspection,Python,Inspect,Code Inspection,我的表单中有此代码。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() mess
表单中有此代码。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
以编程方式提取代码时,它会忽略fieldset
:
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]:
这似乎不是空行的问题。我在没有空白行的情况下进行了测试,并且在其他属性之间添加了空白行。结果不会改变
你知道为什么inspect只返回
字段集之前的部分,而不返回类的全部源吗?编辑:根据评论进行修订:
内部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',)}))
class联系人表单(forms.Form):
全名=forms.CharField(最大长度=120)
email=forms.EmailField()
website=forms.URLField()
message=forms.CharField(最大长度=500,小部件=forms.Textarea)
发送通知=forms.BooleanField(必需=False)
#对我有用。我的代码中没有“from formfieldset.forms import FieldsetMixin”。可能是这导致了问题。不,我没有重新加载()。我现在也删除了FieldsetMixin并重试,但在源代码输出中仍然没有fielsets
。哪个Python版本?我的版本是Debian Lenny上的2.6.2(release26 maint,2009年4月19日,01:56:41)Python 2.5.2(r252:60911,2009年1月4日,21:59:32),我的版本是2.5.4,但仍然没有出现错误。你能提供py文件作为下载链接吗?可能是一些奇怪的字符,这些字符没有出现在上面的文本中?另一件事:您已经将文件命名为forms.py。这会导致与django表单模块的名称冲突。