File tree 2 files changed +11
-3
lines changed
2 files changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -219,12 +219,16 @@ def const(self, init):
219
219
elif isinstance (init , Sequence ):
220
220
iterator = enumerate (init )
221
221
else :
222
- raise TypeError ("Layout constant initializer must be a mapping or a sequence, not {!r} "
223
- . format ( init ) )
222
+ raise TypeError (f "Layout constant initializer must be a mapping or a sequence, not "
223
+ f" { init !r } " )
224
224
225
225
int_value = 0
226
226
for key , key_value in iterator :
227
- field = self [key ]
227
+ try :
228
+ field = self [key ]
229
+ except KeyError :
230
+ raise ValueError (f"Layout constant initializer refers to key { key !r} , which is not "
231
+ f"a part of the layout" )
228
232
cast_field_shape = Shape .cast (field .shape )
229
233
if isinstance (field .shape , ShapeCastable ):
230
234
key_value = hdl .Const .cast (hdl .Const (key_value , field .shape ))
Original file line number Diff line number Diff line change @@ -477,6 +477,10 @@ def test_const_wrong(self):
477
477
r"^Layout constant initializer must be a mapping or a sequence, not "
478
478
r"<.+?object.+?>$" ):
479
479
sl .const (object ())
480
+ with self .assertRaisesRegex (ValueError ,
481
+ r"^Layout constant initializer refers to key 'g', which is not a part "
482
+ r"of the layout$" ):
483
+ sl .const ({"g" : 1 })
480
484
sl2 = data .StructLayout ({"f" : unsigned (2 )})
481
485
with self .assertRaisesRegex (ValueError ,
482
486
r"^Const layout StructLayout.* differs from shape layout StructLayout.*$" ):
You can’t perform that action at this time.
0 commit comments