#33134: Form with __repr__ crashes.
--------------------------------------------+------------------------
               Reporter:  Mariusz Felisiak  |          Owner:  nobody
                   Type:  Bug               |         Status:  new
              Component:  Forms             |        Version:  4.0
               Severity:  Release blocker   |       Keywords:
           Triage Stage:  Unreviewed        |      Has patch:  0
    Needs documentation:  0                 |    Needs tests:  0
Patch needs improvement:  0                 |  Easy pickings:  0
                  UI/UX:  0                 |
--------------------------------------------+------------------------
 I noticed really strange error when trying to test `django-debug-toolbar`
 with Django 4.0a1. The following form:
 {{{
 class TemplateReprForm(forms.Form):
     user = forms.ModelChoiceField(queryset=User.objects.all())

     def __repr__(self):
         return str(self)
 }}}
 causes `Fatal Python error: Cannot recover from stack overflow`:
 {{{
 $ export TOXENV=py38-dj40-sqlite
 $ tox tests.panels.test_template.TemplatesPanelTestCase.test_template_repr
 ...
 test_template_repr (tests.panels.test_template.TemplatesPanelTestCase) ...
 Fatal Python error: Cannot recover from stack overflow.
 Python runtime state: initialized

 Current thread 0x00007f28e059d740 (most recent call first):
   File "/django-debug-toolbar/tests/forms.py", line 9 in __repr__
   File "/usr/lib/python3.8/pprint.py", line 569 in _safe_repr
   File "/usr/lib/python3.8/pprint.py", line 67 in saferepr
   File "/django-debug-toolbar/debug_toolbar/panels/templates/panel.py",
 line 123 in _store_template_info
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/dispatch/dispatcher.py", line 171 in <listcomp>
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/dispatch/dispatcher.py", line 170 in send
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/test/utils.py", line 100 in instrumented_test_render
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/template/base.py", line 176 in render
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/template/backends/django.py", line 61 in render
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/forms/renderers.py", line 23 in render
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/forms/utils.py", line 53 in render
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/forms/boundfield.py", line 186 in label_tag
   File "/django-debug-toolbar/.tox/py38-dj40-sqlite/lib/python3.8/site-
 packages/django/template/base.py", line 891 in _resolve_lookup
   ...
 Aborted (core dumped)
 make: *** [Makefile:41: coverage] Error 134
 }}}

 Removing `form` from `context` in `BoundField.label_tag()` fixes this
 issue for me:
 {{{
 diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py
 index d1e98719d2..5bbfcbe41c 100644
 --- a/django/forms/boundfield.py
 +++ b/django/forms/boundfield.py
 @@ -177,7 +177,6 @@ class BoundField:
                  else:
                      attrs['class'] = self.form.required_css_class
          context = {
 -            'form': self.form,
              'field': self,
              'label': contents,
              'attrs': attrs,
 }}}

 I will try to debug it later.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33134>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.5d7d54e3fe6ff3e7ecf8ec6585396e0a%40djangoproject.com.

Reply via email to