なぜinspect.getsourceクラス全体のソースを返しませんか?
-
06-09-2019 - |
質問
私は私の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
前に一部だけを返すとされていない?
解決
の編集:コメントに基づいて改訂:の
はinspect.getsource(forms.ContactForm)
の内部メソッドBlockFinder.tokeneater()
はContactForm
ブロックが停止する場所を決定するために使用されます。他の人のほかに、それは右のgithubのに保存されているバージョンでfieldsetの前に見つかっtokenize.DEDENT
、チェックします。行は改行が含まれているので、inspect
は、現在のブロックが終了したと思います。
、それが再び私のために動作します。私は、この理論的根拠、多分パフォーマンスに主張することはできません。
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
理由があります。これは単に全体のファイルを出力します。
他のヒント
私のために動作します。私のコードで「formfieldset.formsはFieldsetMixinをインポートから」私は持っていません。たぶんそれが問題を引き起こしている..
所属していません StackOverflow