From 26256ae3920b9d709137e2b2784699969578fde8 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Tue, 11 Jan 2022 19:29:25 +0100 Subject: [PATCH] bin: Make decoder type creation overridable. --- coe/bin.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 -- 2.11.0