types/struct: fix context when calculating default value

This commit is contained in:
Shiz 2022-08-22 18:04:50 +02:00
parent 721ae26f40
commit 6b5e028e52
1 changed files with 10 additions and 4 deletions

View File

@ -140,7 +140,7 @@ class StructType(G[T], Type[T]):
stream.seek(pos + n, os.SEEK_SET)
def default(self, context: Context) -> T:
return self.cls()
return self.cls(_sx_context_=context)
def get_sizes(self, context: Context, value: O[Any], n: str) -> List[Mapping[str, int]]:
sizes = []
@ -178,7 +178,7 @@ class StructType(G[T], Type[T]):
raise ValueError('path element for struct must be string')
if field not in self.fields:
raise ValueError(f'field {field!r} invalid for {self.cls.__name__}')
child = self.fields[field]
with self.enter():
if self.union:
@ -220,11 +220,17 @@ class Struct:
def __init__(self, **kwargs) -> None:
super().__init__()
ctx = kwargs.pop('_sx_context_', None)
st = self._sx_type_
with st.enter():
for k, t in st.fields.items():
if k not in kwargs:
v = sx.default(t)
if ctx:
with ctx.enter(k, t):
v = ctx.default(to_type(t))
else:
v = sx.default(t)
else:
v = kwargs.pop(k)
setattr(self, k, v)
@ -273,7 +279,7 @@ class Struct:
val = next(v for v in val.__metadata__ if isinstance(v, Type))
fields[name] = val
proxy._sx_fields_[name] = []
del localns
for name, exprs in proxy._sx_fields_.items():
count = len(exprs)