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