catacomb/pwsafe.py: Hack around the change in metaclass syntax.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 21 Oct 2019 17:17:21 +0000 (18:17 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 25 Nov 2019 17:43:08 +0000 (17:43 +0000)
Python 2 (all versions) want you to set `__metaclass__' within the class
body.  Python 3 ignores this, treating it like an ordinary class
attribute, and wants you to say `metaclass = ...' in the direct-
superclass list; but the latter is a syntax error to Python 2.

Instead, add a hack which constructs a anonymous class with the right
metaclass to use as a direct superclass.  It's awful, but it works.

catacomb/pwsafe.py

index 7951e0a..5e296e3 100644 (file)
@@ -48,6 +48,10 @@ _NUL = _bin('\0')
 _CIPHER = _bin('cipher:')
 _MAC = _bin('mac:')
 
+def _with_metaclass(meta, *supers):
+  return meta("#<anonymous base %s>" % meta.__name__,
+              supers or (object,), dict())
+
 def _excval(): return SYS.exc_info()[1]
 
 _M600 = int("600", 8)
@@ -235,7 +239,7 @@ class StorageBackendClass (type):
     except AttributeError: pass
     else: StorageBackend.register_concrete_subclass(me)
 
-class StorageBackend (object):
+class StorageBackend (_with_metaclass(StorageBackendClass)):
   """
   I provide basic protocol for password storage backends.
 
@@ -303,7 +307,6 @@ class StorageBackend (object):
                         priority order when opening an existing database.
   """
 
-  __metaclass__ = StorageBackendClass
   PRIO = 10
 
   ## The registry of subclasses.