2017-03-29 13:44:02 +00:00
|
|
|
## @package extension_loader
|
|
|
|
|
# Module caffe2.python.extension_loader
|
2020-09-24 00:55:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-21 18:26:41 +00:00
|
|
|
import contextlib
|
2017-03-30 21:27:35 +00:00
|
|
|
import ctypes
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_set_global_flags = (
|
|
|
|
|
hasattr(sys, 'getdlopenflags') and hasattr(sys, 'setdlopenflags'))
|
|
|
|
|
|
2016-07-21 18:26:41 +00:00
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2018-08-21 03:20:13 +00:00
|
|
|
def DlopenGuard(extra_flags=ctypes.RTLD_GLOBAL):
|
2017-03-30 21:27:35 +00:00
|
|
|
if _set_global_flags:
|
|
|
|
|
old_flags = sys.getdlopenflags()
|
2018-08-21 03:20:13 +00:00
|
|
|
sys.setdlopenflags(old_flags | extra_flags)
|
2019-07-17 17:17:59 +00:00
|
|
|
|
|
|
|
|
# in case we dlopen something that doesn't exist, yield will fail and throw;
|
|
|
|
|
# we need to remember reset the old flags to clean up, otherwise RTLD_GLOBAL
|
|
|
|
|
# flag will stick around and create symbol conflict problems
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
if _set_global_flags:
|
|
|
|
|
sys.setdlopenflags(old_flags)
|