jinja : fix namespace attributes shadowed by dict methods (#30941)
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Check Pre-Tokenizer Hashes / pre-tokenizer-hashes (push) Has been cancelled
Python check requirements.txt / check-requirements (push) Has been cancelled
Python Type-Check / python type-check (push) Has been cancelled
Update Operations Documentation / update-ops-docs (push) Has been cancelled

This commit is contained in:
Tarek Dakhran
2026-10-21 23:06:56 +02:00
committed by GitHub
parent 7775ffb7e4
commit 99fb997184
2 changed files with 18 additions and 1 deletions
+6 -1
View File
@@ -425,7 +425,12 @@ const func_builtins & global_builtins() {
throw raised_exception("Jinja Exception: " + msg); throw raised_exception("Jinja Exception: " + msg);
}}, }},
{"dict", toobject}, {"dict", toobject},
{"namespace", toobject}, {"namespace", [](const func_args & args) -> value {
// unlike a dict, a namespace has no methods: ns.items is the attribute, as in jinja2
value out = toobject(args);
cast_val<value_object>(out)->has_builtins = false;
return out;
}},
{"strftime_now", [](const func_args & args) -> value { {"strftime_now", [](const func_args & args) -> value {
args.ensure_vals<value_string>(); args.ensure_vals<value_string>();
std::string format = args.get_pos(0)->as_string().str(); std::string format = args.get_pos(0)->as_string().str();
+12
View File
@@ -1299,6 +1299,18 @@ static void test_namespace(testing & t) {
json::object(), json::object(),
"3" "3"
); );
test_template(t, "namespace attribute named like a dict method",
"{% set ns = namespace(items=[]) %}{{ 'yes' if ns.items else 'no' }}{% set ns.items = ns.items + [1] %}{{ ns.items | length }}{{ 'yes' if ns.items else 'no' }}",
json::object(),
"no1yes"
);
test_template(t, "dict keeps its methods",
"{% set d = dict(items=1) %}{% for k, v in d.items() %}{{ k }}={{ v }}{% endfor %}",
json::object(),
"items=1"
);
} }
static void test_tests(testing & t) { static void test_tests(testing & t) {