Bug 14917

Summary: Syntax error when using dicts in ${@...}
Product: [Build System, Metadata & Runtime] BitBake Reporter: Enrico Scholz <enrico.scholz>
Component: bitbakeAssignee: Mark <mark.asselstine>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: Medium CC: mark.asselstine, poky.bs.watcher, poky.watcher
Version: unspecified   
Target Milestone: 4.99   
Hardware: x86   
OS: Multiple   
Whiteboard:
OS type for building Yocto: --- Type of Regression: ---
Verified: Documentation change: No (bug/feature does not impact docs)

Description Enrico Scholz 2022-09-26 10:05:19 UTC
```
LICENSE = "CLOSED"

IDX = "green"
VAL = "${@{ 'green': 1, 'blue': 2 }[d.getVar('IDX')]}"
```

fails to expand with

```
expansion of VAL threw ExpansionError: Failure expanding variable VAL, expression was ${@{ 'green': 1, 'blue': 2 }[d.getVar('IDX')]} which triggered exception SyntaxError: '{' was never closed (Var <VAL>, line 1)
```


A workaround could be something like

```
VAL = "${@dict((lambda x: zip(x,x))(iter([ \
    'green', 1, \
    'blue',  2, \
    ])))[d.getVar('IDX')]}"
```

but it is non trivial, difficulty to understand and error prone.
Comment 1 Mark 2022-11-03 00:47:24 UTC
The regex in use will not handle nested curly brackets, the first close curly it sees will end the match, even if it passes over an open curly along the way. It should be possible to ignore a single set of nested curly brackets so I will see if I can derive something that will do the trick. Attempting to have multiple levels of nesting would involve moving to something else, like pyparsing, so we will leave that to a later exercise, should a usecase pop up for it.
Comment 2 Mark 2022-11-10 15:32:37 UTC
Fixed by commit 94e49b9b9e409c29eb04603b1305d96ebe661a4b [data_smart: allow python snippets to include a dictionary]. Enrico please feel free to try it out and raise any issues.