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