-
-
Notifications
You must be signed in to change notification settings - Fork 35k
bpo-30184: Add tests for invalid use of PyArg_ParseTupleAndKeywords. #1316
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
serhiy-storchaka
merged 1 commit into
python:master
from
serhiy-storchaka:test-PyArg_ParseTupleAndKeywords-bad-use
May 3, 2017
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -490,9 +490,8 @@ def test_skipitem(self): | |
| # test the format unit when not skipped | ||
| format = c + "i" | ||
| try: | ||
| # (note: the format string must be bytes!) | ||
| _testcapi.parse_tuple_and_keywords(tuple_1, dict_b, | ||
| format.encode("ascii"), keywords) | ||
| format, keywords) | ||
| when_not_skipped = False | ||
| except SystemError as e: | ||
| s = "argument 1 (impossible<bad format char>)" | ||
|
|
@@ -504,7 +503,7 @@ def test_skipitem(self): | |
| optional_format = "|" + format | ||
| try: | ||
| _testcapi.parse_tuple_and_keywords(empty_tuple, dict_b, | ||
| optional_format.encode("ascii"), keywords) | ||
| optional_format, keywords) | ||
| when_skipped = False | ||
| except SystemError as e: | ||
| s = "impossible<bad format char>: '{}'".format(format) | ||
|
|
@@ -517,40 +516,64 @@ def test_skipitem(self): | |
| self.assertIs(when_skipped, when_not_skipped, message) | ||
|
|
||
| def test_parse_tuple_and_keywords(self): | ||
| # parse_tuple_and_keywords error handling tests | ||
| # Test handling errors in the parse_tuple_and_keywords helper itself | ||
| self.assertRaises(TypeError, _testcapi.parse_tuple_and_keywords, | ||
| (), {}, 42, []) | ||
| self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, | ||
| (), {}, b'', 42) | ||
| (), {}, '', 42) | ||
| self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, | ||
| (), {}, b'', [''] * 42) | ||
| (), {}, '', [''] * 42) | ||
| self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, | ||
| (), {}, b'', [42]) | ||
| (), {}, '', [42]) | ||
|
|
||
| def test_bad_use(self): | ||
| # Test handling invalid format and keywords in | ||
| # PyArg_ParseTupleAndKeywords() | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (1,), {}, '||O', ['a']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (1, 2), {}, '|O|O', ['a', 'b']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (), {'a': 1}, '$$O', ['a']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (), {'a': 1, 'b': 2}, '$O$O', ['a', 'b']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (), {'a': 1}, '$|O', ['a']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (), {'a': 1, 'b': 2}, '$O|O', ['a', 'b']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (1,), {}, '|O', ['a', 'b']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (1,), {}, '|OO', ['a']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (), {}, '|$O', ['']) | ||
| self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, | ||
| (), {}, '|OO', ['a', '']) | ||
|
Contributor
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. May add test case for misuse of self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords,
(1,), {}, ':O', ['a'])
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords,
(1,), {}, ';O', ['a'])
Member
Author
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. These tests don't trigger any error that is not tested. |
||
|
|
||
| def test_positional_only(self): | ||
| parse = _testcapi.parse_tuple_and_keywords | ||
|
|
||
| parse((1, 2, 3), {}, b'OOO', ['', '', 'a']) | ||
| parse((1, 2), {'a': 3}, b'OOO', ['', '', 'a']) | ||
| parse((1, 2, 3), {}, 'OOO', ['', '', 'a']) | ||
| parse((1, 2), {'a': 3}, 'OOO', ['', '', 'a']) | ||
| with self.assertRaisesRegex(TypeError, | ||
| r'function takes at least 2 positional arguments \(1 given\)'): | ||
| parse((1,), {'a': 3}, b'OOO', ['', '', 'a']) | ||
| parse((1,), {}, b'O|OO', ['', '', 'a']) | ||
| parse((1,), {'a': 3}, 'OOO', ['', '', 'a']) | ||
| parse((1,), {}, 'O|OO', ['', '', 'a']) | ||
| with self.assertRaisesRegex(TypeError, | ||
| r'function takes at least 1 positional arguments \(0 given\)'): | ||
| parse((), {}, b'O|OO', ['', '', 'a']) | ||
| parse((1, 2), {'a': 3}, b'OO$O', ['', '', 'a']) | ||
| parse((), {}, 'O|OO', ['', '', 'a']) | ||
| parse((1, 2), {'a': 3}, 'OO$O', ['', '', 'a']) | ||
| with self.assertRaisesRegex(TypeError, | ||
| r'function takes exactly 2 positional arguments \(1 given\)'): | ||
| parse((1,), {'a': 3}, b'OO$O', ['', '', 'a']) | ||
| parse((1,), {}, b'O|O$O', ['', '', 'a']) | ||
| parse((1,), {'a': 3}, 'OO$O', ['', '', 'a']) | ||
| parse((1,), {}, 'O|O$O', ['', '', 'a']) | ||
| with self.assertRaisesRegex(TypeError, | ||
| r'function takes at least 1 positional arguments \(0 given\)'): | ||
| parse((), {}, b'O|O$O', ['', '', 'a']) | ||
| parse((), {}, 'O|O$O', ['', '', 'a']) | ||
| with self.assertRaisesRegex(SystemError, r'Empty parameter name after \$'): | ||
| parse((1,), {}, b'O|$OO', ['', '', 'a']) | ||
| parse((1,), {}, 'O|$OO', ['', '', 'a']) | ||
| with self.assertRaisesRegex(SystemError, 'Empty keyword'): | ||
| parse((1,), {}, b'O|OO', ['', 'a', '']) | ||
| parse((1,), {}, 'O|OO', ['', 'a', '']) | ||
|
|
||
|
|
||
| @unittest.skipUnless(threading, 'Threading required for this test.') | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this use the method below
parser = _testcapi.parse_tuple_and_keywordsto reduce the length?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't matter, in any case the line is wrapped.