proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
windows_dll_manager.py
Go to the documentation of this file.
1import os
2import sys
3import contextlib
4
5
7 proxsuite_nlp_paths = os.getenv("PROXSUITE_NLP_WINDOWS_DLL_PATH")
8 if proxsuite_nlp_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 return [
15 os.path.join(os.path.dirname(__file__), RELATIVE_DLL_PATH1),
16 os.path.join(os.path.dirname(__file__), RELATIVE_DLL_PATH2),
17 ]
18 else:
19 return proxsuite_nlp_paths.split(os.pathsep)
20
21
22class PathManager(contextlib.AbstractContextManager):
23 """Restore PATH state after importing Python module"""
24
25 def add_dll_directory(self, dll_dir: str):
26 os.environ["PATH"] += os.pathsep + dll_dir
27
28 def __enter__(self):
29 self.old_path = os.environ["PATH"]
30 return self
31
32 def __exit__(self, *exc_details):
33 os.environ["PATH"] = self.old_path
34
35
36class DllDirectoryManager(contextlib.AbstractContextManager):
37 """Restore DllDirectory state after importing Python module"""
38
39 def add_dll_directory(self, dll_dir: str):
40 # add_dll_directory can fail on relative path and non
41 # existing path.
42 # Since we don't know all the fail criterion we just ignore
43 # thrown exception
44 try:
45 self.dll_dirs.append(os.add_dll_directory(dll_dir))
46 except OSError:
47 pass
48
49 def __enter__(self):
50 self.dll_dirs = []
51 return self
52
53 def __exit__(self, *exc_details):
54 for d in self.dll_dirs:
55 d.close()
56
57
59 if sys.version_info >= (3, 8):
60 return DllDirectoryManager()
61 else:
62 return PathManager()