Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py: 79%

33 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2020 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================= 

15"""A Python wrapper that loads _pywrap_tensorflow_internal.so.""" 

16 

17import ctypes 

18import sys 

19import traceback 

20 

21from tensorflow.python.platform import self_check 

22 

23# TODO(mdan): Cleanup antipattern: import for side effects. 

24 

25# Perform pre-load sanity checks in order to produce a more actionable error. 

26self_check.preload_check() 

27 

28# pylint: disable=wildcard-import,g-import-not-at-top,unused-import,line-too-long 

29 

30try: 

31 # This import is expected to fail if there is an explicit shared object 

32 # dependency (with_framework_lib=true), since we do not need RTLD_GLOBAL. 

33 from tensorflow.python import pywrap_dlopen_global_flags 

34 _use_dlopen_global_flags = True 

35except ImportError: 

36 _use_dlopen_global_flags = False 

37 

38# On UNIX-based platforms, pywrap_tensorflow is a python library that 

39# dynamically loads _pywrap_tensorflow.so. 

40_can_set_rtld_local = ( 

41 hasattr(sys, 'getdlopenflags') and hasattr(sys, 'setdlopenflags')) 

42if _can_set_rtld_local: 

43 _default_dlopen_flags = sys.getdlopenflags() 

44 

45try: 

46 if _use_dlopen_global_flags: 

47 pywrap_dlopen_global_flags.set_dlopen_flags() 

48 elif _can_set_rtld_local: 

49 # Ensure RTLD_LOCAL behavior for platforms where it isn't the default 

50 # (macOS). On Linux RTLD_LOCAL is 0, so this does nothing (and would not 

51 # override an RTLD_GLOBAL in _default_dlopen_flags). 

52 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_LOCAL) 

53 

54 # Python2.7 does not have a ModuleNotFoundError. 

55 try: 

56 ModuleNotFoundError 

57 except NameError: 

58 ModuleNotFoundError = ImportError # pylint: disable=redefined-builtin 

59 

60 # pylint: disable=wildcard-import,g-import-not-at-top,line-too-long,undefined-variable 

61 try: 

62 from tensorflow.python._pywrap_tensorflow_internal import * 

63 # This try catch logic is because there is no bazel equivalent for py_extension. 

64 # Externally in opensource we must enable exceptions to load the shared object 

65 # by exposing the PyInit symbols with pybind. This error will only be 

66 # caught internally or if someone changes the name of the target _pywrap_tensorflow_internal. 

67 

68 # This logic is used in other internal projects using py_extension. 

69 except ModuleNotFoundError: 

70 pass 

71 

72 if _use_dlopen_global_flags: 

73 pywrap_dlopen_global_flags.reset_dlopen_flags() 

74 elif _can_set_rtld_local: 

75 sys.setdlopenflags(_default_dlopen_flags) 

76except ImportError: 

77 raise ImportError( 

78 f'{traceback.format_exc()}' 

79 f'\n\nFailed to load the native TensorFlow runtime.\n' 

80 f'See https://www.tensorflow.org/install/errors ' 

81 f'for some common causes and solutions.\n' 

82 f'If you need help, create an issue ' 

83 f'at https://github.com/tensorflow/tensorflow/issues ' 

84 f'and include the entire stack trace above this error message.') 

85 

86# pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long