# 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 TypeError as e:
s = "argument 1 (impossible<bad format char>)"
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 RuntimeError as e:
s = "impossible<bad format char>: '{}'".format(format)
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((TypeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (1,), {}, '||O', ['a'])
+ self.assertRaises((RuntimeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (1, 2), {}, '|O|O', ['a', 'b'])
+ self.assertRaises((TypeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (), {'a': 1}, '$$O', ['a'])
+ self.assertRaises((RuntimeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (), {'a': 1, 'b': 2}, '$O$O', ['a', 'b'])
+ self.assertRaises((TypeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (), {'a': 1}, '$|O', ['a'])
+ self.assertRaises((RuntimeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (), {'a': 1, 'b': 2}, '$O|O', ['a', 'b'])
+ self.assertRaises((RuntimeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (1,), {}, '|O', ['a', 'b'])
+ self.assertRaises((RuntimeError, SystemError), _testcapi.parse_tuple_and_keywords,
+ (1,), {}, '|OO', ['a'])
+
@unittest.skipUnless(threading, 'Threading required for this test.')
class TestThreadState(unittest.TestCase):