LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 #
26 import sys
27 import imp
28 import functools
29 import os.path
30 
31 #List of extensions to set global flags. Mayb need to be extended
32 #for systems other than *nix and OSX.
33 SHARED_LIB_EXTENSION_LIST = ('.so', '.dylib')
34 LIB_EXCEPTION_LIST = ('_lsstcppimport.so',)
35 
36 # Ensure that duplicate allocations--particularly those related to RTTI--are
37 # resolved by setting dynamical library loading flags.
38 RTLD_GLOBAL = -1
39 RTLD_NOW = -1
40 try:
41  import dl
42  if hasattr(dl, 'RTLD_GLOBAL'): RTLD_GLOBAL = dl.RTLD_GLOBAL
43  if hasattr(dl, 'RTLD_NOW'): RTLD_NOW = dl.RTLD_NOW
44 except ImportError:
45  # 64bit linux does not have a dl module...
46  pass
47 except SystemError:
48  # ...if it does it should throw a SystemError
49  pass
50 
51 try:
52  if RTLD_GLOBAL < 0:
53  import lsst64defs
54  RTLD_GLOBAL = lsst64defs.RTLD_GLOBAL # usually 0x00100
55  if RTLD_NOW < 0:
56  import lsst64defs
57  RTLD_NOW = lsst64defs.RTLD_NOW # usually 0x00002
58  DLFLAGS = RTLD_GLOBAL|RTLD_NOW
59 except ImportError:
60  sys.stderr.write(
61  "Could not import lsst64defs; please ensure the base package has been built (not just setup).\n"
62  )
63 
64 #Swigged python libraries that import other swigged python libraries need to import with RTLD_GLOBAL
65 #and RTLD_NOW set. This causes problems with symbol collisions in third party packages (notably scipy).
66 #This cannot be fixed by using import hooks because python code generated by swig uses imp.load_module
67 #rather than import. This makes it necessary to over ride imp.load_module.
68 #This was handled in ticket #3055:
69 #https://dev.lsstcorp.org/trac/ticket/3055
70 
71 #Don't redefine if it's already been defined.
72 if 'orig_imp_load_module' not in locals():
73  orig_imp_load_module = imp.load_module
74  @functools.wraps(orig_imp_load_module)
75  def imp_load_module(name, *args):
76  pathParts = args[1].split(os.path.sep)
77  extension = os.path.splitext(pathParts[-1])[-1]
78  #Find all swigged LSST libs. Load _lsstcppimport.so by adding it to the EXCEPTIONLIST
79  #since it may not have lsst in the path (it's in $BASE_DIR/python, not $BASE_DIR/python/lsst).
80  #Also, look for paths that look like python/lsst as that is how to know if you are in an LSST
81  #package.
82  lsstIdx = [i for i, el in enumerate(pathParts) if el == 'python']
83  if pathParts[-1] in LIB_EXCEPTION_LIST or (extension in SHARED_LIB_EXTENSION_LIST and pathParts[-1].startswith('_')\
84  and 'lsst' in [pathParts[i+1] for i in lsstIdx]):
85  #Get currently set flags
86  originalDLFlags = sys.getdlopenflags()
87  #Set flags
88  sys.setdlopenflags(DLFLAGS)
89  try:
90  module = orig_imp_load_module(name, *args)
91  finally:
92  #Set original flags
93  sys.setdlopenflags(originalDLFlags)
94  else:
95  module = orig_imp_load_module(name, *args)
96  return module
97  imp.load_module = imp_load_module
98 
99 try:
100  import lsstcppimport
101 except ImportError:
102  sys.stderr.write(
103  "Could not import lsstcppimport; please ensure the base package has been built (not just setup).\n"
104  )
def imp_load_module
Definition: lsstimport.py:75
orig_imp_load_module
Definition: lsstimport.py:73