bin: Make decoder type creation overridable.
authorFredrik Tolf <fredrik@dolda2000.com>
Tue, 11 Jan 2022 18:29:25 +0000 (19:29 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Tue, 11 Jan 2022 18:29:25 +0000 (19:29 +0100)
coe/bin.py

index 0aa6e5a..395a209 100644 (file)
@@ -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