buil.plat: enable strict undefined behavior in Jinja2.
authorwhitequark <whitequark@whitequark.org>
Tue, 14 Apr 2020 06:17:16 +0000 (06:17 +0000)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 31 Dec 2021 13:27:30 +0000 (13:27 +0000)
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 #337.

nmigen/build/plat.py
nmigen/vendor/lattice_ice40.py

index 291d90c1dd0308081f60f65505cbabfb13b58e4a..d9f2ab2a9daedf5b48387aabd3a4bccf7be08c1e 100644 (file)
@@ -383,7 +383,8 @@ class TemplatedPlatform(Platform):
         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:
index 56da2e87826812c6c0b719136c4c2509af60c665..94dd02afc91ba836eb8cfb724e495a4a2258e694 100644 (file)
@@ -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