windows_dll_manager.py
Go to the documentation of this file.
1 import contextlib
2 import os
3 import sys
4 
5 
7  coal_paths = os.getenv("COAL_WINDOWS_DLL_PATH")
8  if coal_paths is None:
9  # From https://peps.python.org/pep-0250/#implementation
10  # lib/python-version/site-packages/package
11  RELATIVE_DLL_PATH1 = "..\\..\\..\\..\\bin"
12  # lib/site-packages/package
13  RELATIVE_DLL_PATH2 = "..\\..\\..\\bin"
14  # For unit test
15  RELATIVE_DLL_PATH3 = "..\\..\\bin"
16  return [
17  os.path.join(os.path.dirname(__file__), RELATIVE_DLL_PATH1),
18  os.path.join(os.path.dirname(__file__), RELATIVE_DLL_PATH2),
19  os.path.join(os.path.dirname(__file__), RELATIVE_DLL_PATH3),
20  ]
21  else:
22  return coal_paths.split(os.pathsep)
23 
24 
25 class PathManager(contextlib.AbstractContextManager):
26  """Restore PATH state after importing Python module"""
27 
28  def add_dll_directory(self, dll_dir: str):
29  os.environ["PATH"] += os.pathsep + dll_dir
30 
31  def __enter__(self):
32  self.old_path = os.environ["PATH"]
33  return self
34 
35  def __exit__(self, *exc_details):
36  os.environ["PATH"] = self.old_path
37 
38 
39 class DllDirectoryManager(contextlib.AbstractContextManager):
40  """Restore DllDirectory state after importing Python module"""
41 
42  def add_dll_directory(self, dll_dir: str):
43  # add_dll_directory can fail on relative path and non
44  # existing path.
45  # Since we don't know all the fail criterion we just ignore
46  # thrown exception
47  try:
48  self.dll_dirs.append(os.add_dll_directory(dll_dir))
49  except OSError:
50  pass
51 
52  def __enter__(self):
53  self.dll_dirs = []
54  return self
55 
56  def __exit__(self, *exc_details):
57  for d in self.dll_dirs:
58  d.close()
59 
60 
62  if sys.version_info >= (3, 8):
63  return DllDirectoryManager()
64  else:
65  return PathManager()
coal.windows_dll_manager.PathManager.old_path
old_path
Definition: windows_dll_manager.py:32
coal.windows_dll_manager.PathManager
Definition: windows_dll_manager.py:25
coal.windows_dll_manager.DllDirectoryManager.add_dll_directory
def add_dll_directory(self, str dll_dir)
Definition: windows_dll_manager.py:42
coal.windows_dll_manager.PathManager.add_dll_directory
def add_dll_directory(self, str dll_dir)
Definition: windows_dll_manager.py:28
coal.windows_dll_manager.build_directory_manager
def build_directory_manager()
Definition: windows_dll_manager.py:61
coal.windows_dll_manager.get_dll_paths
def get_dll_paths()
Definition: windows_dll_manager.py:6
coal.windows_dll_manager.PathManager.__enter__
def __enter__(self)
Definition: windows_dll_manager.py:31
coal.windows_dll_manager.PathManager.__exit__
def __exit__(self, *exc_details)
Definition: windows_dll_manager.py:35
coal.windows_dll_manager.DllDirectoryManager.dll_dirs
dll_dirs
Definition: windows_dll_manager.py:53
coal.windows_dll_manager.DllDirectoryManager.__enter__
def __enter__(self)
Definition: windows_dll_manager.py:52
coal.windows_dll_manager.DllDirectoryManager
Definition: windows_dll_manager.py:39
coal.windows_dll_manager.DllDirectoryManager.__exit__
def __exit__(self, *exc_details)
Definition: windows_dll_manager.py:56


hpp-fcl
Author(s):
autogenerated on Sat Nov 23 2024 03:44:59