interop.py 685 B

1234567891011121314151617181920212223242526272829
  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. from typing import Any
  7. def cast_int_addr(n: Any) -> int:
  8. """Cast an address to a Python int
  9. This could be a Python integer or a CFFI pointer
  10. """
  11. if isinstance(n, int):
  12. return n
  13. try:
  14. import cffi # type: ignore
  15. except ImportError:
  16. pass
  17. else:
  18. # from pyzmq, this is an FFI void *
  19. ffi = cffi.FFI()
  20. if isinstance(n, ffi.CData):
  21. return int(ffi.cast("size_t", n))
  22. raise ValueError(f"Cannot cast {n!r} to int")