Repository navigation
Delete shared default options after context #59
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
0f94d5b
90c7bbc
03e58ed
997a0a7
78b35e5
b58aed5
2940ce0
79055e5
242c815
965a564
9c9937b
6007fc4
916517d
a1e5081
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -499,14 +499,15 @@ def __init__(self, parameters: dict[str, Any], | |||||
| else: | ||||||
| default_options = {} | ||||||
|
|
||||||
| # Start building parameters from the defaults so | ||||||
| # that they will overwritten by any other source. | ||||||
| parameters = default_options | parameters | ||||||
| inherited_options = set(default_options) | ||||||
|
Member
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. If
Suggested change
|
||||||
|
|
||||||
| # The parameters to drop from the global options when we leave the | ||||||
| # inserted_options context. This is everything except for options | ||||||
| # passed on the command line. | ||||||
| to_delete = set(parameters.keys()) | ||||||
|
pbrubeck marked this conversation as resolved.
|
||||||
|
|
||||||
| # Start building parameters from the defaults so | ||||||
| # that they will overwritten by any other source. | ||||||
| parameters = default_options | parameters | ||||||
| unsafe_options = [] | ||||||
| for full_key, v in self.options_object.getAll().items(): | ||||||
| if full_key.startswith(options_prefix): | ||||||
|
|
@@ -516,9 +517,10 @@ def __init__(self, parameters: dict[str, Any], | |||||
| key = full_key[len(options_prefix):] | ||||||
| parameters[key] = v | ||||||
|
|
||||||
| # option is set globally, don't drop when we exit the | ||||||
| # context manager | ||||||
| to_delete.discard(key) | ||||||
| # Keep existing options, except inherited defaults, which | ||||||
| # are temporary copies under this prefix. | ||||||
| if key not in inherited_options: | ||||||
|
Member
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.
Suggested change
|
||||||
| to_delete.discard(key) | ||||||
|
Member
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 comment is not clear, please can you rewrite it? I would suggest two sentences, one explaining what happens for non-default and one for default. |
||||||
| if unsafe_options: | ||||||
| unsafe_options_str = "\n".join( | ||||||
| f" {opt}" for opt in unsafe_options | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,6 +179,31 @@ def test_default_options(): | |
| assert options2.parameters["opt4"] == "6" | ||
|
|
||
|
|
||
| @pytest.mark.skipnopetsc4py | ||
| def test_inserted_options_deletes_default_options(): | ||
| """Check that inserted_options removes the options that the | ||
| OptionsManager inherits from its default options set, and keeps | ||
| the options that the database already held.""" | ||
| from petsc4py import PETSc | ||
|
|
||
| options = PETSc.Options() | ||
| options["parent_ksp_type"] = "cg" | ||
| options["parent_0_pc_type"] = "jacobi" | ||
| opts_before = dict(options.getAll()) | ||
|
Comment on lines
+189
to
+192
Member
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. Please can you test:
|
||
|
|
||
| child = petsctools.OptionsManager( | ||
| parameters={}, | ||
| options_prefix="parent_0", | ||
| default_options_set=petsctools.DefaultOptionSet("parent", [0])) | ||
| assert child.parameters == {"ksp_type": "cg", "pc_type": "jacobi"} | ||
|
|
||
| with child.inserted_options(): | ||
| pass | ||
|
|
||
| assert options.getAll() == opts_before | ||
| assert "parent_0_ksp_type" not in options | ||
|
|
||
|
|
||
| @pytest.mark.skipnopetsc4py | ||
| def test_python_options_with_manager(): | ||
| petsctools.init() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.