-
Notifications
You must be signed in to change notification settings - Fork 710
swig: guard against silent attribute typos on SWIG objects #3300
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
Merged
+130
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # SPDX-License-Identifier: LGPL-2.1-or-later | ||
| # This file is part of libnvme. | ||
| # Copyright (c) 2026, Dell Technologies Inc. or its subsidiaries. | ||
| # Authors: Martin Belanger <[email protected]> | ||
| """Tests that __setattr__ guards on SWIG-generated classes raise on bad names.""" | ||
|
|
||
| import unittest | ||
| from libnvme import nvme | ||
|
|
||
|
|
||
| class TestCtrlSetattr(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.ctx = nvme.global_ctx() | ||
| self.ctrl = nvme.ctrl(self.ctx, { | ||
| 'subsysnqn': nvme.NVME_DISC_SUBSYS_NAME, | ||
| 'transport': 'loop', | ||
| }) | ||
|
|
||
| def test_valid_writable_property(self): | ||
| """Writing a valid writable property must not raise.""" | ||
| self.ctrl.discovery_ctrl = True | ||
|
|
||
| def test_typo_raises(self): | ||
| """A typo in a property name must raise AttributeError immediately.""" | ||
| with self.assertRaises(AttributeError): | ||
| self.ctrl.dhchap_key = 'somekey' # correct name is dhchap_ctrl_key | ||
|
|
||
| def test_readonly_raises(self): | ||
| """Writing a read-only (%immutable) property must raise AttributeError.""" | ||
| with self.assertRaises(AttributeError): | ||
| self.ctrl.transport = 'tcp' | ||
|
|
||
| def test_unknown_attr_raises(self): | ||
| """A completely unknown attribute name must raise AttributeError.""" | ||
| with self.assertRaises(AttributeError): | ||
| self.ctrl.does_not_exist = 42 | ||
|
|
||
|
|
||
| class TestCtrlDictValidation(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| self.ctx = nvme.global_ctx() | ||
|
|
||
| def test_unknown_dict_key_raises(self): | ||
| """An unknown key in the constructor dict must raise KeyError.""" | ||
| with self.assertRaises(KeyError): | ||
| nvme.ctrl(self.ctx, { | ||
| 'subsysnqn': nvme.NVME_DISC_SUBSYS_NAME, | ||
| 'transport': 'loop', | ||
| 'typo_key': 'value', | ||
| }) | ||
|
|
||
| def test_missing_required_key_raises(self): | ||
| """Omitting a required key (subsysnqn or transport) must raise KeyError.""" | ||
| with self.assertRaises(KeyError): | ||
| nvme.ctrl(self.ctx, {'transport': 'loop'}) | ||
| with self.assertRaises(KeyError): | ||
| nvme.ctrl(self.ctx, {'subsysnqn': nvme.NVME_DISC_SUBSYS_NAME}) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.