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
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:
@@ -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();
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user