|
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
|
@@ -1241,6 +1243,27 @@ def test_parse_tuple_and_keywords(self):
|
1241 | 1243 | self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords,
|
1242 | 1244 | (), {}, '', [42])
|
1243 | 1245 |
|
| 1246 | + def test_basic(self): |
| 1247 | + parse = _testcapi.parse_tuple_and_keywords |
| 1248 | + |
| 1249 | + self.assertEqual(parse((), {'a': 1}, 'O', ['a']), (1,)) |
| 1250 | + self.assertEqual(parse((), {}, '|O', ['a']), (NULL,)) |
| 1251 | + self.assertEqual(parse((1, 2), {}, 'OO', ['a', 'b']), (1, 2)) |
| 1252 | + self.assertEqual(parse((1,), {'b': 2}, 'OO', ['a', 'b']), (1, 2)) |
| 1253 | + self.assertEqual(parse((), {'a': 1, 'b': 2}, 'OO', ['a', 'b']), (1, 2)) |
| 1254 | + self.assertEqual(parse((), {'b': 2}, '|OO', ['a', 'b']), (NULL, 2)) |
| 1255 | + |
| 1256 | + with self.assertRaisesRegex(TypeError, |
| 1257 | + "function missing required argument 'a'"): |
| 1258 | + parse((), {}, 'O', ['a']) |
| 1259 | + with self.assertRaisesRegex(TypeError, |
| 1260 | + "'b' is an invalid keyword argument"): |
| 1261 | + parse((), {'b': 1}, '|O', ['a']) |
| 1262 | + with self.assertRaisesRegex(TypeError, |
| 1263 | + fr"argument for function given by name \('a'\) " |
| 1264 | + fr"and position \(1\)"): |
| 1265 | + parse((1,), {'a': 2}, 'O|O', ['a', 'b']) |
| 1266 | + |
1244 | 1267 | def test_bad_use(self):
|
1245 | 1268 | # Test handling invalid format and keywords in
|
1246 | 1269 | # PyArg_ParseTupleAndKeywords()
|
@@ -1268,20 +1291,23 @@ def test_bad_use(self):
|
1268 | 1291 | def test_positional_only(self):
|
1269 | 1292 | parse = _testcapi.parse_tuple_and_keywords
|
1270 | 1293 |
|
1271 |
| - parse((1, 2, 3), {}, 'OOO', ['', '', 'a']) |
1272 |
| - parse((1, 2), {'a': 3}, 'OOO', ['', '', 'a']) |
| 1294 | + self.assertEqual(parse((1, 2, 3), {}, 'OOO', ['', '', 'a']), (1, 2, 3)) |
| 1295 | + self.assertEqual(parse((1, 2), {'a': 3}, 'OOO', ['', '', 'a']), (1, 2, 3)) |
1273 | 1296 | with self.assertRaisesRegex(TypeError,
|
1274 | 1297 | r'function takes at least 2 positional arguments \(1 given\)'):
|
1275 | 1298 | parse((1,), {'a': 3}, 'OOO', ['', '', 'a'])
|
1276 |
| - parse((1,), {}, 'O|OO', ['', '', 'a']) |
| 1299 | + self.assertEqual(parse((1,), {}, 'O|OO', ['', '', 'a']), |
| 1300 | + (1, NULL, NULL)) |
1277 | 1301 | with self.assertRaisesRegex(TypeError,
|
1278 | 1302 | r'function takes at least 1 positional argument \(0 given\)'):
|
1279 | 1303 | parse((), {}, 'O|OO', ['', '', 'a'])
|
1280 |
| - parse((1, 2), {'a': 3}, 'OO$O', ['', '', 'a']) |
| 1304 | + self.assertEqual(parse((1, 2), {'a': 3}, 'OO$O', ['', '', 'a']), |
| 1305 | + (1, 2, 3)) |
1281 | 1306 | with self.assertRaisesRegex(TypeError,
|
1282 | 1307 | r'function takes exactly 2 positional arguments \(1 given\)'):
|
1283 | 1308 | parse((1,), {'a': 3}, 'OO$O', ['', '', 'a'])
|
1284 |
| - parse((1,), {}, 'O|O$O', ['', '', 'a']) |
| 1309 | + self.assertEqual(parse((1,), {}, 'O|O$O', ['', '', 'a']), |
| 1310 | + (1, NULL, NULL)) |
1285 | 1311 | with self.assertRaisesRegex(TypeError,
|
1286 | 1312 | r'function takes at least 1 positional argument \(0 given\)'):
|
1287 | 1313 | parse((), {}, 'O|O$O', ['', '', 'a'])
|
|
0 commit comments