catacomb/__init__.py: Awful bodge for symbol conflict.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 14 Jul 2017 22:19:41 +0000 (23:19 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 22 Nov 2019 22:18:11 +0000 (22:18 +0000)
It seems that, in Debian jessie and later, the main Python binary now
exports `md5_init' and friends.  Unfortunately, this overrides
Catacomb's existing `md5_init' with a rather different version, and the
result is a segfault (on i386) or wrong answers (on amd64).

So, as an unpleasant bodge (while this broken thing makes its way
through Debian, see bug #868366), try to force the `RTLD_DEEPBIND' flag
when loading the module.  This is unfortunate, because Python doesn't
actually advertise this flag, at least in my version.

(cherry picked from commit a3ae4a9f590ef84d8e6eac0bc94873a9fd943073)

catacomb/__init__.py

index 53d7cea..eb3344a 100644 (file)
@@ -27,6 +27,9 @@ from __future__ import with_statement
 
 from binascii import hexlify as _hexify, unhexlify as _unhexify
 from contextlib import contextmanager as _ctxmgr
+try: import DLFCN as _dlfcn
+except ImportError: _dlfcn = None
+import os as _os
 from struct import pack as _pack
 import sys as _sys
 import types as _types
@@ -34,8 +37,31 @@ import types as _types
 ###--------------------------------------------------------------------------
 ### Import the main C extension module.
 
+try:
+  _dlflags = _odlflags = _sys.getdlopenflags()
+except AttributeError:
+  _dlflags = _odlflags = -1
+
+## Set the `deep binding' flag.  Python has its own different MD5
+## implementation, and some distributions export `md5_init' and friends so
+## they override our versions, which doesn't end well.  Figure out how to
+## turn this flag on so we don't have the problem.
+if _dlflags >= 0:
+  try: _dlflags |= _dlfcn.RTLD_DEEPBIND
+  except AttributeError:
+    try: _dlflags |= _os.RTLD_DEEPBIND
+    except AttributeError:
+      if _os.uname()[0] == 'Linux': _dlflags |= 8 # magic knowledge
+      else: pass # can't do this.
+  _sys.setdlopenflags(_dlflags)
+
 import _base
 
+if _odlflags >= 0:
+  _sys.setdlopenflags(_odlflags)
+
+del _dlflags, _odlflags
+
 ###--------------------------------------------------------------------------
 ### Basic stuff.