|
55 | 55 | LLONG_MIN = -2**63
|
56 | 56 | ULLONG_MAX = 2**64-1
|
57 | 57 |
|
| 58 | +NULL = None |
| 59 | + |
58 | 60 | class Index:
|
59 | 61 | def __index__(self):
|
60 | 62 | return 99
|
@@ -1160,6 +1162,27 @@ def test_parse_tuple_and_keywords(self):
|
1160 | 1162 | self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords,
|
1161 | 1163 | (), {}, '', [42])
|
1162 | 1164 |
|
| 1165 | + def test_basic(self): |
| 1166 | + parse = _testcapi.parse_tuple_and_keywords |
| 1167 | + |
| 1168 | + self.assertEqual(parse((), {'a': 1}, 'O', ['a']), (1,)) |
| 1169 | + self.assertEqual(parse((), {}, '|O', ['a']), (NULL,)) |
| 1170 | + self.assertEqual(parse((1, 2), {}, 'OO', ['a', 'b']), (1, 2)) |
| 1171 | + self.assertEqual(parse((1,), {'b': 2}, 'OO', ['a', 'b']), (1, 2)) |
| 1172 | + self.assertEqual(parse((), {'a': 1, 'b': 2}, 'OO', ['a', 'b']), (1, 2)) |
| 1173 | + self.assertEqual(parse((), {'b': 2}, '|OO', ['a', 'b']), (NULL, 2)) |
| 1174 | + |
| 1175 | + with self.assertRaisesRegex(TypeError, |
| 1176 | + "function missing required argument 'a'"): |
| 1177 | + parse((), {}, 'O', ['a']) |
| 1178 | + with self.assertRaisesRegex(TypeError, |
| 1179 | + "'b' is an invalid keyword argument"): |
| 1180 | + parse((), {'b': 1}, '|O', ['a']) |
| 1181 | + with self.assertRaisesRegex(TypeError, |
| 1182 | + fr"argument for function given by name \('a'\) " |
| 1183 | + fr"and position \(1\)"): |
| 1184 | + parse((1,), {'a': 2}, 'O|O', ['a', 'b']) |
| 1185 | + |
1163 | 1186 | def test_bad_use(self):
|
1164 | 1187 | # Test handling invalid format and keywords in
|
1165 | 1188 | # PyArg_ParseTupleAndKeywords()
|
@@ -1187,20 +1210,23 @@ def test_bad_use(self):
|
1187 | 1210 | def test_positional_only(self):
|
1188 | 1211 | parse = _testcapi.parse_tuple_and_keywords
|
1189 | 1212 |
|
1190 |
| - parse((1, 2, 3), {}, 'OOO', ['', '', 'a']) |
1191 |
| - parse((1, 2), {'a': 3}, 'OOO', ['', '', 'a']) |
| 1213 | + self.assertEqual(parse((1, 2, 3), {}, 'OOO', ['', '', 'a']), (1, 2, 3)) |
| 1214 | + self.assertEqual(parse((1, 2), {'a': 3}, 'OOO', ['', '', 'a']), (1, 2, 3)) |
1192 | 1215 | with self.assertRaisesRegex(TypeError,
|
1193 | 1216 | r'function takes at least 2 positional arguments \(1 given\)'):
|
1194 | 1217 | parse((1,), {'a': 3}, 'OOO', ['', '', 'a'])
|
1195 |
| - parse((1,), {}, 'O|OO', ['', '', 'a']) |
| 1218 | + self.assertEqual(parse((1,), {}, 'O|OO', ['', '', 'a']), |
| 1219 | + (1, NULL, NULL)) |
1196 | 1220 | with self.assertRaisesRegex(TypeError,
|
1197 | 1221 | r'function takes at least 1 positional argument \(0 given\)'):
|
1198 | 1222 | parse((), {}, 'O|OO', ['', '', 'a'])
|
1199 |
| - parse((1, 2), {'a': 3}, 'OO$O', ['', '', 'a']) |
| 1223 | + self.assertEqual(parse((1, 2), {'a': 3}, 'OO$O', ['', '', 'a']), |
| 1224 | + (1, 2, 3)) |
1200 | 1225 | with self.assertRaisesRegex(TypeError,
|
1201 | 1226 | r'function takes exactly 2 positional arguments \(1 given\)'):
|
1202 | 1227 | parse((1,), {'a': 3}, 'OO$O', ['', '', 'a'])
|
1203 |
| - parse((1,), {}, 'O|O$O', ['', '', 'a']) |
| 1228 | + self.assertEqual(parse((1,), {}, 'O|O$O', ['', '', 'a']), |
| 1229 | + (1, NULL, NULL)) |
1204 | 1230 | with self.assertRaisesRegex(TypeError,
|
1205 | 1231 | r'function takes at least 1 positional argument \(0 given\)'):
|
1206 | 1232 | parse((), {}, 'O|O$O', ['', '', 'a'])
|
|
0 commit comments