diff options
| author | cheng <chengyuan.china@gmail.com> | 2022-05-19 21:01:10 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-05-20 07:53:05 +0200 |
| commit | 0dd29209091280ccf34e07c9468746c396b7778e (patch) | |
| tree | 4fffdeff50b89662eddf3d392f8195e55c33470b | |
| parent | 1a78ef2b85467a18ea6d7eaa4b27f67d11c87b9e (diff) | |
Fixed #33653 -- Fixed template crash when calling methods for built-in types without required arguments.
Regression in 09341856ed9008875c1cc883dc0c287670131458.
| -rw-r--r-- | django/template/base.py | 17 | ||||
| -rw-r--r-- | tests/template_tests/tests.py | 8 |
2 files changed, 18 insertions, 7 deletions
diff --git a/django/template/base.py b/django/template/base.py index a1ab437eca..afbdc7391d 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -913,15 +913,18 @@ class Variable: try: # method call (assuming no args required) current = current() except TypeError: - signature = inspect.signature(current) try: - signature.bind() - except TypeError: # arguments *were* required - current = ( - context.template.engine.string_if_invalid - ) # invalid method call + signature = inspect.signature(current) + except ValueError: # No signature found. + current = context.template.engine.string_if_invalid else: - raise + try: + signature.bind() + except TypeError: # Arguments *were* required. + # Invalid method call. + current = context.template.engine.string_if_invalid + else: + raise except Exception as e: template_name = getattr(context, "template_name", None) or "unknown" logger.debug( diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index b849b4a2e7..f35ae49ca3 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -183,6 +183,14 @@ class TemplateTestMixin: for node in template.nodelist: self.assertEqual(node.origin, template.origin) + def test_render_built_in_type_method(self): + """ + Templates should not crash when rendering methods for built-in types + without required arguments. + """ + template = self._engine().from_string("{{ description.count }}") + self.assertEqual(template.render(Context({"description": "test"})), "") + class TemplateTests(TemplateTestMixin, SimpleTestCase): debug_engine = False |
