Repository: pallets/jinja (python)

Issue:
TypeError: sequence item 0: expected str instance, int found
```python
import jinja2
from jinja2.nativetypes import NativeEnvironment
NativeEnvironment().from_string('{% block test %}{% for i in range(1) %}{{ loop.index }}{% endfor %}{% endblock %}{{ self.test() }}').render()
```

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/phemmer/.local/lib/python3.10/site-packages/jinja2/nativetypes.py", line 112, in render
    return self.environment.handle_exception()
  File "/home/phemmer/.local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/home/phemmer/.local/lib/python3.10/site-packages/jinja2/nativetypes.py", line 25, in native_concat
    head = list(islice(values, 2))
  File "<template>", line 1, in top-level template code
TypeError: sequence item 0: expected str instance, int found
```

Environment:

- Python version: Python 3.10.5

- Jinja version: 3.1.2


Pull request description:
Use correct concat function for blocks evaluation
- fixes #1701

<!--
Ensure each step in CONTRIBUTING.rst is complete by adding an "x" to each box below.

If only docs were changed, these aren't relevant and can be removed.
-->

Checklist:

- [ ] Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
- [ ] Add or update relevant docs, in the docs folder and in code.
- [ ] Add an entry in `CHANGES.rst` summarizing the change and linking to the issue.
- [ ] Add `.. versionchanged::` entries in any relevant code docs.
- [ ] Run `pre-commit` hooks and fix any issues.
- [ ] Run `pytest` and `tox`, no tests failed.


Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index feb1e6c3..540d5ccc 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -14,6 +14,8 @@ Unreleased
     ``Template.generate_async``. :pr:`1960`
 -   Avoid leaving async generators unclosed in blocks, includes and extends.
     :pr:`1960`
+-   The runtime uses the correct ``concat`` function for the current environment
+    when calling block references. :issue:`1701`
 
 
 Version 3.1.4
diff --git a/src/jinja2/runtime.py b/src/jinja2/runtime.py
index d10fe9d0..c2c7c193 100644
--- a/src/jinja2/runtime.py
+++ b/src/jinja2/runtime.py
@@ -367,7 +367,7 @@ class BlockReference:
 
     @internalcode
     async def _async_call(self) -> str:
-        rv = concat(
+        rv = self._context.environment.concat(  # type: ignore
             [x async for x in self._stack[self._depth](self._context)]  # type: ignore
         )
 
@@ -381,7 +381,9 @@ class BlockReference:
         if self._context.environment.is_async:
             return self._async_call()  # type: ignore
 
-        rv = concat(self._stack[self._depth](self._context))
+        rv = self._context.environment.concat(  # type: ignore
+            self._stack[self._depth](self._context)
+        )
 
         if self._context.eval_ctx.autoescape:
             return Markup(rv)


Test patch:
diff --git a/tests/test_nativetypes.py b/tests/test_nativetypes.py
index 8c852525..13690818 100644
--- a/tests/test_nativetypes.py
+++ b/tests/test_nativetypes.py
@@ -160,3 +160,13 @@ def test_macro(env):
     result = t.render()
     assert result == 2
     assert isinstance(result, int)
+
+
+def test_block(env):
+    t = env.from_string(
+        "{% block b %}{% for i in range(1) %}{{ loop.index }}{% endfor %}"
+        "{% endblock %}{{ self.b() }}"
+    )
+    result = t.render()
+    assert result == 11
+    assert isinstance(result, int)