interop.py 709 B

123456789101112131415161718192021222324252627282930313233
  1. """Utils for interoperability with other libraries.
  2. Just CFFI pointer casting for now.
  3. """
  4. # Copyright (C) PyZMQ Developers
  5. # Distributed under the terms of the Modified BSD License.
  6. try:
  7. long
  8. except NameError:
  9. long = int # Python 3
  10. def cast_int_addr(n):
  11. """Cast an address to a Python int
  12. This could be a Python integer or a CFFI pointer
  13. """
  14. if isinstance(n, (int, long)):
  15. return n
  16. try:
  17. import cffi
  18. except ImportError:
  19. pass
  20. else:
  21. # from pyzmq, this is an FFI void *
  22. ffi = cffi.FFI()
  23. if isinstance(n, ffi.CData):
  24. return int(ffi.cast("size_t", n))
  25. raise ValueError("Cannot cast %r to int" % n)