lsst.base  14.0-8-g7f6dd6b+1
LSST Data Management Base Package
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 from __future__ import absolute_import, division, print_function
27 import sys
28 import imp
29 import functools
30 import importlib
31 import os.path
32 
33 # Ensure that the Python3 package library reorganization is available
34 # to Python 2. It's okay if this doesn't work, and it won't work if SCons
35 # is importing this and SCons has no access to the future package.
36 try:
37  from future import standard_library
38  standard_library.install_aliases()
39 except ImportError:
40  pass
41 
42 # List of extensions to set global flags. May need to be extended
43 # for systems other than *nix and OSX.
44 SHARED_LIB_EXTENSION_LIST = ('.so', '.dylib')
45 LIB_EXCEPTION_LIST = ('_lsstcppimport.so',)
46 
47 # Ensure that duplicate allocations--particularly those related to RTTI--are
48 # resolved by setting dynamical library loading flags.
49 RTLD_GLOBAL = None
50 RTLD_NOW = None
51 
52 # For portability we try a number of different options for determining RTLD constants
53 options = ('os', 'DLFCN', 'ctypes')
54 for mod in options:
55  try:
56  m = importlib.import_module(mod)
57  if RTLD_GLOBAL is None and hasattr(m, "RTLD_GLOBAL"):
58  RTLD_GLOBAL = m.RTLD_GLOBAL
59  if RTLD_NOW is None and hasattr(m, "RTLD_NOW"):
60  RTLD_NOW = m.RTLD_NOW
61  except ImportError:
62  pass
63  if RTLD_GLOBAL is not None and RTLD_NOW is not None:
64  break
65 
66 # Failing to find RTLD_GLOBAL is definitely unexpected and needs investigation.
67 if RTLD_GLOBAL is None:
68  raise NameError("RTLD_GLOBAL constant can not be determined")
69 
70 # RTLD_NOW will be missing on Python 2 with OS X.
71 # The value is defined in dlfcn.h and on Mac and Linux has the same value:
72 # #define RTLD_NOW 0x2
73 # Do not issue a warning message as this will happen on every single import.
74 if RTLD_NOW is None:
75  RTLD_NOW = 2
76 
77 DLFLAGS = RTLD_GLOBAL | RTLD_NOW
78 
79 # Note: Unsure if the following is still needed with pybind11
80 
81 # Swigged python libraries that import other swigged python libraries
82 # need to import with RTLD_GLOBAL and RTLD_NOW set. This causes
83 # problems with symbol collisions in third party packages (notably
84 # scipy). This cannot be fixed by using import hooks because python
85 # code generated by swig uses imp.load_module rather than import.
86 # This makes it necessary to over ride imp.load_module. This was
87 # handled in ticket #3055: https://dev.lsstcorp.org/trac/ticket/3055
88 
89 # Don't redefine if it's already been defined.
90 if 'orig_imp_load_module' not in locals():
91  orig_imp_load_module = imp.load_module
92 
93  @functools.wraps(orig_imp_load_module)
94  def imp_load_module(name, *args):
95  pathParts = args[1].split(os.path.sep)
96  extension = os.path.splitext(pathParts[-1])[-1]
97  # Find all swigged LSST libs. Load _lsstcppimport.so by
98  # adding it to the EXCEPTIONLIST since it may not have lsst in
99  # the path (it's in $BASE_DIR/python, not
100  # $BASE_DIR/python/lsst). Also, look for paths that look like
101  # python/lsst as that is how to know if you are in an LSST
102  # package.
103  lsstIdx = [i for i, el in enumerate(pathParts) if el == 'python']
104  if pathParts[-1] in LIB_EXCEPTION_LIST or (extension in SHARED_LIB_EXTENSION_LIST and
105  pathParts[-1].startswith('_') and
106  'lsst' in [pathParts[i + 1] for i in lsstIdx]):
107  # Get currently set flags
108  originalDLFlags = sys.getdlopenflags()
109  # Set flags
110  sys.setdlopenflags(DLFLAGS)
111  try:
112  module = orig_imp_load_module(name, *args)
113  finally:
114  # Set original flags
115  sys.setdlopenflags(originalDLFlags)
116  else:
117  module = orig_imp_load_module(name, *args)
118  return module
119  imp.load_module = imp_load_module
120 
121 try:
122  import lsstcppimport # noqa F401
123 except ImportError:
124  # The lsstcppimport may have failed because we're inside Scons.
125  # If we are, then don't worry about it
126  try:
127  import SCons.Script # noqa F401
128  # If we're not, then
129  # a) we will get an ImportError trying to import SCons.Script
130  # b) and will know that the first ImportError really is a problem
131  # and we should let the user know.
132  except ImportError:
133  print(
134  "Could not import lsstcppimport;"
135  " please ensure the base package has been built (not just setup).\n",
136  file=sys.stderr)
orig_imp_load_module
Definition: lsstimport.py:91
def imp_load_module(name, args)
Definition: lsstimport.py:94