__init__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Import basic exposure of libzmq C API as a backend"""
  2. # Copyright (C) PyZMQ Developers
  3. # Distributed under the terms of the Modified BSD License.
  4. import os
  5. import platform
  6. import sys
  7. from .select import public_api, select_backend
  8. if 'PYZMQ_BACKEND' in os.environ:
  9. backend = os.environ['PYZMQ_BACKEND']
  10. if backend in ('cython', 'cffi'):
  11. backend = 'zmq.backend.%s' % backend
  12. _ns = select_backend(backend)
  13. else:
  14. # default to cython, fallback to cffi
  15. # (reverse on PyPy)
  16. if platform.python_implementation() == 'PyPy':
  17. first, second = ('zmq.backend.cffi', 'zmq.backend.cython')
  18. else:
  19. first, second = ('zmq.backend.cython', 'zmq.backend.cffi')
  20. try:
  21. _ns = select_backend(first)
  22. except Exception:
  23. exc_info = sys.exc_info()
  24. exc = exc_info[1]
  25. try:
  26. _ns = select_backend(second)
  27. except ImportError:
  28. # prevent 'During handling of the above exception...' on py3
  29. # can't use `raise ... from` on Python 2
  30. if hasattr(exc, '__cause__'):
  31. exc.__cause__ = None
  32. # raise the *first* error, not the fallback
  33. from zmq.utils.sixcerpt import reraise
  34. reraise(*exc_info)
  35. globals().update(_ns)
  36. __all__ = public_api