__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """Python bindings for 0MQ"""
  2. # Copyright (C) PyZMQ Developers
  3. # Distributed under the terms of the Modified BSD License.
  4. from __future__ import annotations
  5. import os
  6. import sys
  7. from contextlib import contextmanager
  8. @contextmanager
  9. def _libs_on_path():
  10. """context manager for libs directory on $PATH
  11. Works around mysterious issue where os.add_dll_directory
  12. does not resolve imports (conda-forge Python >= 3.8)
  13. """
  14. if not sys.platform.startswith("win"):
  15. yield
  16. return
  17. libs_dir = os.path.abspath(
  18. os.path.join(
  19. os.path.dirname(__file__),
  20. os.pardir,
  21. "pyzmq.libs",
  22. )
  23. )
  24. if not os.path.exists(libs_dir):
  25. # no bundled libs
  26. yield
  27. return
  28. path_before = os.environ.get("PATH")
  29. try:
  30. os.environ["PATH"] = os.pathsep.join([path_before or "", libs_dir])
  31. yield
  32. finally:
  33. if path_before is None:
  34. os.environ.pop("PATH")
  35. else:
  36. os.environ["PATH"] = path_before
  37. # zmq top-level imports
  38. # workaround for Windows
  39. with _libs_on_path():
  40. from zmq import backend
  41. from . import constants # noqa
  42. from .constants import * # noqa
  43. from zmq.backend import * # noqa
  44. from zmq import sugar
  45. from zmq.sugar import * # noqa
  46. def get_includes():
  47. """Return a list of directories to include for linking against pyzmq with cython."""
  48. from os.path import abspath, dirname, exists, join, pardir
  49. base = dirname(__file__)
  50. parent = abspath(join(base, pardir))
  51. includes = [parent] + [join(parent, base, subdir) for subdir in ('utils',)]
  52. if exists(join(parent, base, 'include')):
  53. includes.append(join(parent, base, 'include'))
  54. return includes
  55. def get_library_dirs():
  56. """Return a list of directories used to link against pyzmq's bundled libzmq."""
  57. from os.path import abspath, dirname, join, pardir
  58. base = dirname(__file__)
  59. parent = abspath(join(base, pardir))
  60. return [join(parent, base)]
  61. COPY_THRESHOLD = 65536
  62. # zmq.DRAFT_API represents _both_ the current runtime-loaded libzmq
  63. # and pyzmq were built with drafts,
  64. # which is required for pyzmq draft support
  65. DRAFT_API: bool = backend.has('draft') and backend.PYZMQ_DRAFT_API
  66. __all__ = (
  67. [
  68. 'get_includes',
  69. 'COPY_THRESHOLD',
  70. 'DRAFT_API',
  71. ]
  72. + constants.__all__
  73. + sugar.__all__
  74. + backend.__all__
  75. )