LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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