File tree Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Expand file tree Collapse file tree 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):
219219 elif isinstance (init , Sequence ):
220220 iterator = enumerate (init )
221221 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 } " )
224224
225225 int_value = 0
226226 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" )
228232 cast_field_shape = Shape .cast (field .shape )
229233 if isinstance (field .shape , ShapeCastable ):
230234 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):
477477 r"^Layout constant initializer must be a mapping or a sequence, not "
478478 r"<.+?object.+?>$" ):
479479 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 })
480484 sl2 = data .StructLayout ({"f" : unsigned (2 )})
481485 with self .assertRaisesRegex (ValueError ,
482486 r"^Const layout StructLayout.* differs from shape layout StructLayout.*$" ):
You can’t perform that action at this time.
0 commit comments