Skip to content

Commit

Permalink
Fixed a panic and incorrect debug printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Aug 17, 2023
1 parent 48ce7de commit c872f0a
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to MiniJinja are documented here.

## 1.0.7

- Debug output of `Value::UNDEFINED` and `Value::from(())` is now
`undefined` and `none` rather than `Undefined` and `None`. This was
an accidental inconsistency.
- Fixed a potential panic in debug error printing.
- Added `Environment::set_path_join_callback` and `State::get_template`
to support path joining. This is for greater compatibility with Jinja2
where path joining was overridable. With this you can configure the
Expand Down
4 changes: 3 additions & 1 deletion minijinja/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub(super) fn render_debug_info(
writeln!(f, "{:>4} | {}", idx + 1, line).unwrap();
}

writeln!(f, "{:>4} > {}", idx + 1, lines[idx].1).unwrap();
if let Some(line) = lines.get(idx) {
writeln!(f, "{:>4} > {}", idx + 1, line.1).unwrap();
}
if let Some(span) = span {
if span.start_line == span.end_line {
ok!(writeln!(
Expand Down
4 changes: 2 additions & 2 deletions minijinja/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ pub(crate) enum ValueRepr {
impl fmt::Debug for ValueRepr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ValueRepr::Undefined => f.write_str("Undefined"),
ValueRepr::Undefined => f.write_str("undefined"),
ValueRepr::Bool(val) => fmt::Debug::fmt(val, f),
ValueRepr::U64(val) => fmt::Debug::fmt(val, f),
ValueRepr::I64(val) => fmt::Debug::fmt(val, f),
ValueRepr::F64(val) => fmt::Debug::fmt(val, f),
ValueRepr::None => f.write_str("None"),
ValueRepr::None => f.write_str("none"),
ValueRepr::Invalid(ref val) => write!(f, "<invalid value: {}>", val),
ValueRepr::U128(val) => fmt::Debug::fmt(&{ val.0 }, f),
ValueRepr::I128(val) => fmt::Debug::fmt(&{ val.0 }, f),
Expand Down
5 changes: 3 additions & 2 deletions minijinja/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: minijinja/tests/test_templates.rs
description: "{% with f = range %}{% for x in f(upper) %}{{ debug() }}{% endfor %}{% endwith %}"
info:
upper: 1
input_file: minijinja/tests/inputs/debug.txt
---
State {
name: "debug.txt",
Expand All @@ -20,8 +21,8 @@ State {
last: true,
depth: 1,
depth0: 0,
previtem: Undefined,
nextitem: Undefined,
previtem: undefined,
nextitem: undefined,
},
"f": minijinja::functions::builtins::range,
"upper": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Referenced variables: {
last: false,
depth: 1,
depth0: 0,
previtem: Undefined,
previtem: undefined,
nextitem: 2,
},
seq: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Referenced variables: {
last: false,
depth: 1,
depth0: 0,
previtem: Undefined,
previtem: undefined,
nextitem: 2,
},
seq: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Referenced variables: {
last: false,
depth: 1,
depth0: 0,
previtem: Undefined,
previtem: undefined,
nextitem: 2,
},
seq: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Referenced variables: {
last: false,
depth: 1,
depth0: 0,
previtem: Undefined,
previtem: undefined,
nextitem: 2,
},
seq: [
Expand Down
8 changes: 4 additions & 4 deletions minijinja/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ float-round: 43.0
float-round-prec2: 42.51
select-odd: [1, 3, 5]
select-truthy: [42, 23, "aha"]
reject-truthy: [Undefined, Undefined, 0, ""]
reject-truthy: [undefined, undefined, 0, ""]
reject-odd: [2, 4, 6]
select-attr: [{"active": true, "key": 1}]
reject-attr: [{"active": false, "key": 2}]
select-attr: [{"active": false, "key": 2}]
reject-attr: [{"active": true, "key": 1}]
map-maps: [1, 2, 3, 4, 5]
map-attr: [1, 2, None]
map-attr-undefined: [1, 2, Undefined]
map-attr-deep: [1, 2, None]
map-attr: [1, 2, none]
map-attr-undefined: [1, 2, undefined]
map-attr-deep: [1, 2, none]
map-attr-int: [999, 2]
attr-filter: b
unique-filter: [1, 4, 3, 0, 5]
Expand Down
2 changes: 1 addition & 1 deletion minijinja/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Referenced variables: {
last: false,
depth: 500,
depth0: 499,
previtem: Undefined,
previtem: undefined,
nextitem: 2,
},
seq: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Referenced variables: {
last: false,
depth: 1,
depth0: 0,
previtem: Undefined,
previtem: undefined,
nextitem: 2,
},
seq: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Referenced variables: {
last: false,
depth: 1,
depth0: 0,
previtem: Undefined,
previtem: undefined,
nextitem: [
2,
3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ description: "{% macro foo(a, b, c, d) %}{{ [a, b, c, d] }}{% endmacro -%}\n{{ f
info: {}
input_file: minijinja/tests/inputs/macro_kwargs.txt
---
[1, 2, 3, Undefined]
[{"blub": "blah"}, Undefined, Undefined, Undefined]
[1, 2, 3, Undefined]
[1, 2, 3, undefined]
[{"blub": "blah"}, undefined, undefined, undefined]
[1, 2, 3, undefined]
[1, 2, 3, 4]

8 changes: 4 additions & 4 deletions minijinja/tests/test_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ fn test_sort_different_types() {
v.sort();
insta::assert_debug_snapshot!(&v, @r###"
[
Undefined,
None,
undefined,
none,
-inf,
-100,
-75.0,
Expand Down Expand Up @@ -210,7 +210,7 @@ fn test_map_object_iteration_and_indexing() {
"{{ [point.x, point.z, point.missing_attribute] }}",
point => Value::from_object(Point(1, 2, 3))
);
assert_snapshot!(rv, @r###"[1, 3, Undefined]"###);
assert_snapshot!(rv, @"[1, 3, undefined]");
}

#[test]
Expand Down Expand Up @@ -259,7 +259,7 @@ fn test_seq_object_iteration_and_indexing() {
"{{ [point[0], point[2], point[42]] }}",
point => Value::from_object(Point(1, 2, 3))
);
assert_snapshot!(rv, @r###"[1, 3, Undefined]"###);
assert_snapshot!(rv, @"[1, 3, undefined]");
}

#[test]
Expand Down

0 comments on commit c872f0a

Please sign in to comment.