LSSTApplications  16.0-10-g0ee56ad+5,16.0-11-ga33d1f2+5,16.0-12-g3ef5c14+3,16.0-12-g71e5ef5+18,16.0-12-gbdf3636+3,16.0-13-g118c103+3,16.0-13-g8f68b0a+3,16.0-15-gbf5c1cb+4,16.0-16-gfd17674+3,16.0-17-g7c01f5c+3,16.0-18-g0a50484+1,16.0-20-ga20f992+8,16.0-21-g0e05fd4+6,16.0-21-g15e2d33+4,16.0-22-g62d8060+4,16.0-22-g847a80f+4,16.0-25-gf00d9b8+1,16.0-28-g3990c221+4,16.0-3-gf928089+3,16.0-32-g88a4f23+5,16.0-34-gd7987ad+3,16.0-37-gc7333cb+2,16.0-4-g10fc685+2,16.0-4-g18f3627+26,16.0-4-g5f3a788+26,16.0-5-gaf5c3d7+4,16.0-5-gcc1f4bb+1,16.0-6-g3b92700+4,16.0-6-g4412fcd+3,16.0-6-g7235603+4,16.0-69-g2562ce1b+2,16.0-8-g14ebd58+4,16.0-8-g2df868b+1,16.0-8-g4cec79c+6,16.0-8-gadf6c7a+1,16.0-8-gfc7ad86,16.0-82-g59ec2a54a+1,16.0-9-g5400cdc+2,16.0-9-ge6233d7+5,master-g2880f2d8cf+3,v17.0.rc1
LSSTDataManagementBasePackage
lsstimport.py
Go to the documentation of this file.
1 #! env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 # Copyright 2015 AURA/LSST.
7 #
8 # This product includes software developed by the
9 # LSST Project (http://www.lsst.org/).
10 #
11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the LSST License Statement and
22 # the GNU General Public License along with this program. If not,
23 # see <http://www.lsstcorp.org/LegalNotices/>.
24 #
25 
26 """Configure Python library loader to support LSST shared libraries."""
27 
28 __all__ = []
29 
30 import sys
31 import imp
32 import functools
33 import importlib
34 import os.path
35 
36 # List of extensions to set global flags. May need to be extended
37 # for systems other than *nix and OSX.
38 SHARED_LIB_EXTENSION_LIST = ('.so', '.dylib')
39 LIB_EXCEPTION_LIST = ('_lsstcppimport.so',)
40 
41 # Ensure that duplicate allocations--particularly those related to RTTI--are
42 # resolved by setting dynamical library loading flags.
43 RTLD_GLOBAL = None
44 RTLD_NOW = None
45 
46 # For portability we try a number of different options for determining RTLD constants
47 options = ('os', 'DLFCN', 'ctypes')
48 for mod in options:
49  try:
50  m = importlib.import_module(mod)
51  if RTLD_GLOBAL is None and hasattr(m, "RTLD_GLOBAL"):
52  RTLD_GLOBAL = m.RTLD_GLOBAL
53  if RTLD_NOW is None and hasattr(m, "RTLD_NOW"):
54  RTLD_NOW = m.RTLD_NOW
55  except ImportError:
56  pass
57  if RTLD_GLOBAL is not None and RTLD_NOW is not None:
58  break
59 
60 # Failing to find RTLD_GLOBAL is definitely unexpected and needs investigation.
61 if RTLD_GLOBAL is None:
62  raise NameError("RTLD_GLOBAL constant can not be determined")
63 
64 # RTLD_NOW will be missing on Python 2 with OS X.
65 # The value is defined in dlfcn.h and on Mac and Linux has the same value:
66 # #define RTLD_NOW 0x2
67 # Do not issue a warning message as this will happen on every single import.
68 if RTLD_NOW is None:
69  RTLD_NOW = 2
70 
71 DLFLAGS = RTLD_GLOBAL | RTLD_NOW
72 
73 # Note: Unsure if the following is still needed with pybind11
74 
75 # Swigged python libraries that import other swigged python libraries
76 # need to import with RTLD_GLOBAL and RTLD_NOW set. This causes
77 # problems with symbol collisions in third party packages (notably
78 # scipy). This cannot be fixed by using import hooks because python
79 # code generated by swig uses imp.load_module rather than import.
80 # This makes it necessary to over ride imp.load_module. This was
81 # handled in ticket #3055: https://dev.lsstcorp.org/trac/ticket/3055
82 
83 # Don't redefine if it's already been defined.
84 if 'orig_imp_load_module' not in locals():
85  orig_imp_load_module = imp.load_module
86 
87  @functools.wraps(orig_imp_load_module)
88  def imp_load_module(name, *args):
89  pathParts = args[1].split(os.path.sep)
90  extension = os.path.splitext(pathParts[-1])[-1]
91  # Find all swigged LSST libs. Load _lsstcppimport.so by
92  # adding it to the EXCEPTIONLIST since it may not have lsst in
93  # the path (it's in $BASE_DIR/python, not
94  # $BASE_DIR/python/lsst). Also, look for paths that look like
95  # python/lsst as that is how to know if you are in an LSST
96  # package.
97  lsstIdx = [i for i, el in enumerate(pathParts) if el == 'python']
98  if pathParts[-1] in LIB_EXCEPTION_LIST or (extension in SHARED_LIB_EXTENSION_LIST and
99  pathParts[-1].startswith('_') and
100  'lsst' in [pathParts[i + 1] for i in lsstIdx]):
101  # Get currently set flags
102  originalDLFlags = sys.getdlopenflags()
103  # Set flags
104  sys.setdlopenflags(DLFLAGS)
105  try:
106  module = orig_imp_load_module(name, *args)
107  finally:
108  # Set original flags
109  sys.setdlopenflags(originalDLFlags)
110  else:
111  module = orig_imp_load_module(name, *args)
112  return module
113  imp.load_module = imp_load_module
114 
115 try:
116  import lsstcppimport # noqa F401
117 except ImportError:
118  # The lsstcppimport may have failed because we're inside Scons.
119  # If we are, then don't worry about it
120  try:
121  import SCons.Script # noqa F401
122  # If we're not, then
123  # a) we will get an ImportError trying to import SCons.Script
124  # b) and will know that the first ImportError really is a problem
125  # and we should let the user know.
126  except ImportError:
127  print(
128  "Could not import lsstcppimport;"
129  " please ensure the base package has been built (not just setup).\n",
130  file=sys.stderr)
orig_imp_load_module
Definition: lsstimport.py:85
def imp_load_module(name, args)
Definition: lsstimport.py:88