1717
1818_CURRENT_BRIDGE = threading .local ()
1919_CURRENT_BRIDGE .type = 'native'
20+ _GLOBAL_BRIDGE_TYPE = 'native' # child threads will derive from the global type but not overwrite
2021
2122def reset_bridge ():
2223 _CURRENT_BRIDGE .type = 'native'
24+ if threading .current_thread ().name == 'MainThread' :
25+ _GLOBAL_BRIDGE_TYPE = 'native'
2326
2427def set_bridge (new_bridge ):
2528 assert isinstance (new_bridge , str ), (
@@ -28,11 +31,17 @@ def set_bridge(new_bridge):
2831 "valid bridges: {}" .format (_BRIDGE_TYPES .keys ()))
2932 global _CURRENT_BRIDGE
3033 _CURRENT_BRIDGE .type = new_bridge
34+ if threading .current_thread ().name == 'MainThread' :
35+ _GLOBAL_BRIDGE_TYPE = new_bridge
3136
3237def bridge_out (native_arr ):
38+ if not hasattr (_CURRENT_BRIDGE , 'type' ):
39+ _CURRENT_BRIDGE .type = _GLOBAL_BRIDGE_TYPE
3340 return _BRIDGE_TYPES [_CURRENT_BRIDGE .type ][0 ](native_arr )
3441
3542def bridge_in (arr ):
43+ if not hasattr (_CURRENT_BRIDGE , 'type' ):
44+ _CURRENT_BRIDGE .type = _GLOBAL_BRIDGE_TYPE
3645 return _BRIDGE_TYPES [_CURRENT_BRIDGE .type ][1 ](arr )
3746
3847class _BridgeScope (object ):
@@ -42,6 +51,8 @@ def __init__(self, bridge_type='native'):
4251
4352 def __enter__ (self ):
4453 global _CURRENT_BRIDGE
54+ if not hasattr (_CURRENT_BRIDGE , 'type' ):
55+ _CURRENT_BRIDGE .type = _GLOBAL_BRIDGE_TYPE
4556 try :
4657 self ._prev = _CURRENT_BRIDGE .type
4758 except AttributeError :
0 commit comments