Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

#! env python 

 

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# Copyright 2015 AURA/LSST. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

"""Configure Python library loader to support LSST shared libraries.""" 

 

__all__ = [] 

 

import sys 

import imp 

import functools 

import importlib 

import os.path 

 

# List of extensions to set global flags. May need to be extended 

# for systems other than *nix and OSX. 

SHARED_LIB_EXTENSION_LIST = ('.so', '.dylib') 

LIB_EXCEPTION_LIST = ('_lsstcppimport.so',) 

 

# Ensure that duplicate allocations--particularly those related to RTTI--are 

# resolved by setting dynamical library loading flags. 

RTLD_GLOBAL = None 

RTLD_NOW = None 

 

# For portability we try a number of different options for determining RTLD constants 

options = ('os', 'DLFCN', 'ctypes') 

48 ↛ 61line 48 didn't jump to line 61, because the loop on line 48 didn't completefor mod in options: 

try: 

m = importlib.import_module(mod) 

51 ↛ 53line 51 didn't jump to line 53, because the condition on line 51 was never false if RTLD_GLOBAL is None and hasattr(m, "RTLD_GLOBAL"): 

RTLD_GLOBAL = m.RTLD_GLOBAL 

53 ↛ 57line 53 didn't jump to line 57, because the condition on line 53 was never false if RTLD_NOW is None and hasattr(m, "RTLD_NOW"): 

RTLD_NOW = m.RTLD_NOW 

except ImportError: 

pass 

57 ↛ 48line 57 didn't jump to line 48, because the condition on line 57 was never false if RTLD_GLOBAL is not None and RTLD_NOW is not None: 

break 

 

# Failing to find RTLD_GLOBAL is definitely unexpected and needs investigation. 

61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never trueif RTLD_GLOBAL is None: 

raise NameError("RTLD_GLOBAL constant can not be determined") 

 

# RTLD_NOW will be missing on Python 2 with OS X. 

# The value is defined in dlfcn.h and on Mac and Linux has the same value: 

# #define RTLD_NOW 0x2 

# Do not issue a warning message as this will happen on every single import. 

68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never trueif RTLD_NOW is None: 

RTLD_NOW = 2 

 

DLFLAGS = RTLD_GLOBAL | RTLD_NOW 

 

# Note: Unsure if the following is still needed with pybind11 

 

# Swigged python libraries that import other swigged python libraries 

# need to import with RTLD_GLOBAL and RTLD_NOW set. This causes 

# problems with symbol collisions in third party packages (notably 

# scipy). This cannot be fixed by using import hooks because python 

# code generated by swig uses imp.load_module rather than import. 

# This makes it necessary to over ride imp.load_module. This was 

# handled in ticket #3055: https://dev.lsstcorp.org/trac/ticket/3055 

 

# Don't redefine if it's already been defined. 

84 ↛ 115line 84 didn't jump to line 115, because the condition on line 84 was never falseif 'orig_imp_load_module' not in locals(): 

orig_imp_load_module = imp.load_module 

 

@functools.wraps(orig_imp_load_module) 

def imp_load_module(name, *args): 

pathParts = args[1].split(os.path.sep) 

extension = os.path.splitext(pathParts[-1])[-1] 

# Find all swigged LSST libs. Load _lsstcppimport.so by 

# adding it to the EXCEPTIONLIST since it may not have lsst in 

# the path (it's in $BASE_DIR/python, not 

# $BASE_DIR/python/lsst). Also, look for paths that look like 

# python/lsst as that is how to know if you are in an LSST 

# package. 

lsstIdx = [i for i, el in enumerate(pathParts) if el == 'python'] 

if pathParts[-1] in LIB_EXCEPTION_LIST or (extension in SHARED_LIB_EXTENSION_LIST and 

pathParts[-1].startswith('_') and 

'lsst' in [pathParts[i + 1] for i in lsstIdx]): 

# Get currently set flags 

originalDLFlags = sys.getdlopenflags() 

# Set flags 

sys.setdlopenflags(DLFLAGS) 

try: 

module = orig_imp_load_module(name, *args) 

finally: 

# Set original flags 

sys.setdlopenflags(originalDLFlags) 

else: 

module = orig_imp_load_module(name, *args) 

return module 

imp.load_module = imp_load_module 

 

try: 

import lsstcppimport # noqa F401 

except ImportError: 

# The lsstcppimport may have failed because we're inside Scons. 

# If we are, then don't worry about it 

try: 

import SCons.Script # noqa F401 

# If we're not, then 

# a) we will get an ImportError trying to import SCons.Script 

# b) and will know that the first ImportError really is a problem 

# and we should let the user know. 

except ImportError: 

print( 

"Could not import lsstcppimport;" 

" please ensure the base package has been built (not just setup).\n", 

file=sys.stderr)