catacomb/__init__.py: Make KMAC `copy' work.
[catacomb-python] / catacomb / __init__.py
index eb3344a..8713ab7 100644 (file)
@@ -68,6 +68,15 @@ del _dlflags, _odlflags
 ## For the benefit of the default keyreporter, we need the program name.
 _base._ego(_sys.argv[0])
 
+## Register our module.
+_base._set_home_module(_sys.modules[__name__])
+def default_lostexchook(why, ty, val, tb):
+  """`catacomb.lostexchook(WHY, TY, VAL, TB)' reports lost exceptions."""
+  _sys.stderr.write("\n\n!!! LOST EXCEPTION: %s\n" % why)
+  _sys.excepthook(ty, val, tb)
+  _sys.stderr.write("\n")
+lostexchook = default_lostexchook
+
 ## How to fix a name back into the right identifier.  Alas, the rules are not
 ## consistent.
 def _fixname(name):
@@ -228,8 +237,8 @@ class _ShakeBase (_HashBase):
     me._h = me._SHAKE(perso = perso, func = me._FUNC)
 
   ## Delegate methods...
-  def copy(me): new = me.__class__(); new._copy(me)
-  def _copy(me, other): me._h = other._h
+  def copy(me): new = me.__class__._bare_new(); new._copy(me); return new
+  def _copy(me, other): me._h = other._h.copy()
   def hash(me, m): me._h.hash(m); return me
   def xof(me): me._h.xof(); return me
   def get(me, n): return me._h.get(n)
@@ -242,6 +251,8 @@ class _ShakeBase (_HashBase):
   def buffered(me): return me._h.buffered
   @property
   def rate(me): return me._h.rate
+  @classmethod
+  def _bare_new(cls): return cls()
 
 class _tmp:
   def check(me, h):
@@ -280,6 +291,8 @@ class KMAC (_ShakeBase):
   def xof(me):
     me.rightenc(0)
     return super(KMAC, me).xof()
+  @classmethod
+  def _bare_new(cls): return cls("")
 
 class KMAC128 (KMAC): _SHAKE = Shake128; _TAGSZ = 16
 class KMAC256 (KMAC): _SHAKE = Shake256; _TAGSZ = 32