extension_loader.py 744 B

1234567891011121314151617181920212223242526272829
  1. ## @package extension_loader
  2. # Module caffe2.python.extension_loader
  3. import contextlib
  4. import ctypes
  5. import sys
  6. _set_global_flags = (
  7. hasattr(sys, 'getdlopenflags') and hasattr(sys, 'setdlopenflags'))
  8. @contextlib.contextmanager
  9. def DlopenGuard(extra_flags=ctypes.RTLD_GLOBAL):
  10. if _set_global_flags:
  11. old_flags = sys.getdlopenflags()
  12. sys.setdlopenflags(old_flags | extra_flags)
  13. # in case we dlopen something that doesn't exist, yield will fail and throw;
  14. # we need to remember reset the old flags to clean up, otherwise RTLD_GLOBAL
  15. # flag will stick around and create symbol conflict problems
  16. try:
  17. yield
  18. finally:
  19. if _set_global_flags:
  20. sys.setdlopenflags(old_flags)