__init__.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """Python bindings for 0MQ."""
  2. # Copyright (C) PyZMQ Developers
  3. # Distributed under the terms of the Modified BSD License.
  4. # load bundled libzmq, if there is one:
  5. def _load_libzmq():
  6. """load bundled libzmq if there is one"""
  7. import sys, platform, os
  8. dlopen = hasattr(sys, 'getdlopenflags') # unix-only
  9. # RTLD flags are added to os in Python 3
  10. # get values from os because ctypes values are WRONG on pypy
  11. PYPY = platform.python_implementation().lower() == 'pypy'
  12. if dlopen:
  13. import ctypes
  14. dlflags = sys.getdlopenflags()
  15. # set RTLD_GLOBAL, unset RTLD_LOCAL
  16. flags = ctypes.RTLD_GLOBAL | dlflags
  17. # ctypes.RTLD_LOCAL is 0 on pypy, which is *wrong*
  18. flags &= ~ getattr(os, 'RTLD_LOCAL', 4)
  19. # pypy on darwin needs RTLD_LAZY for some reason
  20. if PYPY and sys.platform == 'darwin':
  21. flags |= getattr(os, 'RTLD_LAZY', 1)
  22. flags &= ~ getattr(os, 'RTLD_NOW', 2)
  23. sys.setdlopenflags(flags)
  24. try:
  25. from . import libzmq
  26. except ImportError:
  27. pass
  28. else:
  29. # store libzmq as zmq._libzmq for backward-compat
  30. globals()['_libzmq'] = libzmq
  31. if PYPY:
  32. # should already have been imported above, so reimporting is as cheap as checking
  33. import ctypes
  34. # some versions of pypy (5.3 < ? < 5.8) needs explicit CDLL load for some reason,
  35. # otherwise symbols won't be globally available
  36. # do this unconditionally because it should be harmless (?)
  37. ctypes.CDLL(libzmq.__file__, ctypes.RTLD_GLOBAL)
  38. finally:
  39. if dlopen:
  40. sys.setdlopenflags(dlflags)
  41. _load_libzmq()
  42. # zmq top-level imports
  43. from zmq import backend
  44. from zmq.backend import *
  45. from zmq import sugar
  46. from zmq.sugar import *
  47. def get_includes():
  48. """Return a list of directories to include for linking against pyzmq with cython."""
  49. from os.path import join, dirname, abspath, pardir, exists
  50. base = dirname(__file__)
  51. parent = abspath(join(base, pardir))
  52. includes = [ parent ] + [ join(parent, base, subdir) for subdir in ('utils',) ]
  53. if exists(join(parent, base, 'include')):
  54. includes.append(join(parent, base, 'include'))
  55. return includes
  56. def get_library_dirs():
  57. """Return a list of directories used to link against pyzmq's bundled libzmq."""
  58. from os.path import join, dirname, abspath, pardir
  59. base = dirname(__file__)
  60. parent = abspath(join(base, pardir))
  61. return [ join(parent, base) ]
  62. COPY_THRESHOLD = 65536
  63. __all__ = ['get_includes', 'COPY_THRESHOLD'] + sugar.__all__ + backend.__all__