From: Fredrik Tolf Date: Tue, 11 Jan 2022 18:29:25 +0000 (+0100) Subject: bin: Make decoder type creation overridable. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=26256ae3920b9d709137e2b2784699969578fde8;hp=82855e293520a6112cbb353d9fb3ae9fff2e049d;p=coe.git bin: Make decoder type creation overridable. --- diff --git a/coe/bin.py b/coe/bin.py index 0aa6e5a..395a209 100644 --- a/coe/bin.py +++ b/coe/bin.py @@ -153,7 +153,10 @@ class referror(fmterror): super().__init__("bad backref") class namedtype(type): - pass + def __new__(cls, *args, typename=None, **kwargs): + self = super().__new__(cls, *args, **kwargs) + self.typename = typename + return self class decoder(object): def __init__(self): @@ -225,6 +228,9 @@ class decoder(object): return buf buf[key] = self.loadtagged(fp, tag) + def makeobjtype(self, nm): + return namedtype(str(nm), (data.obj, object), {}, typename=nm) + def loadobj(self, fp, ref=False): if ref: refid = len(self.reftab) @@ -232,8 +238,7 @@ class decoder(object): nm = self.load(fp) typ = self.namedtypes.get(nm) if typ is None: - typ = self.namedtypes[nm] = namedtype(str(nm), (data.obj, object), {}) - typ.typename = nm + typ = self.namedtypes[nm] = self.makeobjtype(nm) ret = typ() if ref: self.reftab[refid] = ret