Skip to content

Commit

Permalink
buil.plat: enable strict undefined behavior in Jinja2.
Browse files Browse the repository at this point in the history
By default, if an operation produces an undefined value (a Jinja2
concept that corresponds to Python's KeyError, AttributeError, etc)
then this value may be printed in a template, which is a nop. This
behavior can hide bugs.

This commit changes the Jinja2 behavior to raise an error instead of
producing an undefined value in all cases. (We produce undefined
values deliberately in a few places. Those are unaffected; it is OK
to use several kinds of undefined values in one Jinja2 environment.)

Fixes m-labs#337.
  • Loading branch information
whitequark committed Apr 14, 2020
1 parent ee73d39 commit 3346f2c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nmigen/build/plat.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ def quiet(arg):
def render(source, origin, syntax=None):
try:
source = textwrap.dedent(source).strip()
compiled = jinja2.Template(source, trim_blocks=True, lstrip_blocks=True)
compiled = jinja2.Template(source,
trim_blocks=True, lstrip_blocks=True, undefined=jinja2.StrictUndefined)
compiled.environment.filters["options"] = options
compiled.environment.filters["hierarchy"] = hierarchy
except jinja2.TemplateSyntaxError as e:
Expand Down
3 changes: 2 additions & 1 deletion nmigen/vendor/lattice_ice40.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class LatticeICE40Platform(TemplatedPlatform):
--log {{name}}.tim
{{platform._nextpnr_device_options[platform.device]}}
--package
{{platform.package|lower}}{{platform._nextpnr_package_options[platform.device]}}
{{platform.package|lower}}{{platform._nextpnr_package_options[platform.device]|
default("")}}
--json {{name}}.json
--pcf {{name}}.pcf
--asc {{name}}.asc
Expand Down

0 comments on commit 3346f2c

Please sign in to comment.