-
Notifications
You must be signed in to change notification settings - Fork 121
Improve conditional imports #458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
37cac9f
c635020
31c8ea8
37c61b8
af8016e
a4623d9
f51bda9
3807ebf
1151c91
bb6bd3f
1803a56
553d8cc
4e5ab3f
e7c46ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,3 +24,5 @@ pyoptsparse/pyNLPQLP/source | |
| *.pdb | ||
|
|
||
| *.pyd | ||
|
|
||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,15 +27,15 @@ | |
| INFINITY, | ||
| IROW, | ||
| extractRows, | ||
| import_module, | ||
| mapToCSC, | ||
| scaleRows, | ||
| try_import_compiled_module_from_path, | ||
| ) | ||
|
|
||
| # import the compiled module | ||
| THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
| _IMPORT_SNOPT_FROM = os.environ.get("PYOPTSPARSE_IMPORT_SNOPT_FROM", THIS_DIR) | ||
| snopt = try_import_compiled_module_from_path("snopt", _IMPORT_SNOPT_FROM) | ||
| _IMPORT_SNOPT_FROM = os.environ.get("PYOPTSPARSE_IMPORT_SNOPT_FROM", None) or THIS_DIR | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to handle an empty str set to the var? |
||
| snopt = import_module("snopt", [_IMPORT_SNOPT_FROM]) | ||
|
|
||
|
|
||
| class SNOPT(Optimizer): | ||
|
|
@@ -68,9 +68,9 @@ def __init__(self, raiseError=True, options: Dict = {}): | |
|
|
||
| informs = self._getInforms() | ||
|
|
||
| if isinstance(snopt, str): | ||
| if isinstance(snopt, Exception): | ||
| if raiseError: | ||
| raise ImportError(snopt) | ||
| raise snopt | ||
| else: | ||
| version = None | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| # First party modules | ||
| from pyoptsparse import Optimizers, list_optimizers | ||
| from pyoptsparse.pyOpt_solution import SolutionInform | ||
| from pyoptsparse.pyOpt_utils import try_import_compiled_module_from_path | ||
| from pyoptsparse.pyOpt_utils import import_module | ||
|
|
||
| # we have to unset this environment variable because otherwise | ||
| # the snopt module gets automatically imported, thus failing the import test below | ||
|
|
@@ -19,13 +19,12 @@ def test_nonexistent_path(self): | |
| for key in list(sys.modules.keys()): | ||
| if "snopt" in key: | ||
| sys.modules.pop(key) | ||
| with self.assertWarns(UserWarning): | ||
| module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path", raise_warning=True) | ||
| self.assertTrue(isinstance(module, str)) | ||
| with self.assertRaises(ImportError): | ||
whophil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| module = import_module("snopt", "/a/nonexistent/path", on_error="raise") | ||
|
|
||
| def test_sys_path_unchanged(self): | ||
| path = tuple(sys.path) | ||
| try_import_compiled_module_from_path("snopt", "/some/path") | ||
| import_module("snopt", "/some/path") | ||
|
||
| self.assertEqual(tuple(sys.path), path) | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.