summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-05-03 21:03:23 (GMT)
committerGitHub <noreply@github.com>2017-05-03 21:03:23 (GMT)
commit5f161fd86dd5bb936a1a2a13391b13b7e59ec201 (patch)
treec76e72829f65ab72c23c312cb42a36ab5853491c /Lib/test
parentfeec3dc9c308052754f9e4848c1c1ddb007e9f66 (diff)
downloadcpython-5f161fd86dd5bb936a1a2a13391b13b7e59ec201.zip
cpython-5f161fd86dd5bb936a1a2a13391b13b7e59ec201.tar.gz
cpython-5f161fd86dd5bb936a1a2a13391b13b7e59ec201.tar.bz2
bpo-30184: Add tests for invalid use of PyArg_ParseTupleAndKeywords. (#1316)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_capi.py59
1 files changed, 41 insertions, 18 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index a95e4c5..eb3e2c5 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -490,9 +490,8 @@ class SkipitemTest(unittest.TestCase):
# 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 @@ class SkipitemTest(unittest.TestCase):
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 @@ class SkipitemTest(unittest.TestCase):
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', ''])
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.')