@@ -1389,123 +1389,6 @@ def test_nested_sequence(self):
13891389 "argument 1 must be sequence of length 1, not 0" ):
13901390 parse (([],), {}, '(' + f + ')' , ['a' ])
13911391
1392- def test_specific_type_errors (self ):
1393- parse = _testcapi .parse_tuple_and_keywords
1394-
1395- def check (format , arg , expected , got = 'list' ):
1396- errmsg = f'must be { expected } , not { got } '
1397- with self .assertRaisesRegex (TypeError , errmsg ):
1398- parse ((arg ,), {}, format , ['a' ])
1399-
1400- check ('k' , [], 'int' )
1401- check ('k?' , [], 'int or None' )
1402- check ('K' , [], 'int' )
1403- check ('K?' , [], 'int or None' )
1404- check ('c' , [], 'a byte string of length 1' )
1405- check ('c?' , [], 'a byte string of length 1 or None' )
1406- check ('c' , b'abc' , 'a byte string of length 1' ,
1407- 'a bytes object of length 3' )
1408- check ('c?' , b'abc' , 'a byte string of length 1 or None' ,
1409- 'a bytes object of length 3' )
1410- check ('c' , bytearray (b'abc' ), 'a byte string of length 1' ,
1411- 'a bytearray object of length 3' )
1412- check ('c?' , bytearray (b'abc' ), 'a byte string of length 1 or None' ,
1413- 'a bytearray object of length 3' )
1414- check ('C' , [], 'a unicode character' )
1415- check ('C?' , [], 'a unicode character or None' )
1416- check ('C' , 'abc' , 'a unicode character' ,
1417- 'a string of length 3' )
1418- check ('C?' , 'abc' , 'a unicode character or None' ,
1419- 'a string of length 3' )
1420- check ('s' , [], 'str' )
1421- check ('s?' , [], 'str or None' )
1422- check ('z' , [], 'str or None' )
1423- check ('z?' , [], 'str or None' )
1424- check ('es' , [], 'str' )
1425- check ('es?' , [], 'str or None' )
1426- check ('es#' , [], 'str' )
1427- check ('es#?' , [], 'str or None' )
1428- check ('et' , [], 'str, bytes or bytearray' )
1429- check ('et?' , [], 'str, bytes, bytearray or None' )
1430- check ('et#' , [], 'str, bytes or bytearray' )
1431- check ('et#?' , [], 'str, bytes, bytearray or None' )
1432- check ('w*' , [], 'read-write bytes-like object' )
1433- check ('w*?' , [], 'read-write bytes-like object or None' )
1434- check ('S' , [], 'bytes' )
1435- check ('S?' , [], 'bytes or None' )
1436- check ('U' , [], 'str' )
1437- check ('U?' , [], 'str or None' )
1438- check ('Y' , [], 'bytearray' )
1439- check ('Y?' , [], 'bytearray or None' )
1440- check ('(OO)' , 42 , '2-item tuple' , 'int' )
1441- check ('(OO)?' , 42 , '2-item tuple or None' , 'int' )
1442- check ('(OO)' , (1 , 2 , 3 ), 'tuple of length 2' , '3' )
1443-
1444- def test_nullable (self ):
1445- parse = _testcapi .parse_tuple_and_keywords
1446-
1447- def check (format , arg , allows_none = False ):
1448- # Because some format units (such as y*) require cleanup,
1449- # we force the parsing code to perform the cleanup by adding
1450- # an argument that always fails.
1451- # By checking for an exception, we ensure that the parsing
1452- # of the first argument was successful.
1453- self .assertRaises (OverflowError , parse ,
1454- (arg , 256 ), {}, format + '?b' , ['a' , 'b' ])
1455- self .assertRaises (OverflowError , parse ,
1456- (None , 256 ), {}, format + '?b' , ['a' , 'b' ])
1457- self .assertRaises (OverflowError , parse ,
1458- (arg , 256 ), {}, format + 'b' , ['a' , 'b' ])
1459- self .assertRaises (OverflowError if allows_none else TypeError , parse ,
1460- (None , 256 ), {}, format + 'b' , ['a' , 'b' ])
1461-
1462- check ('b' , 42 )
1463- check ('B' , 42 )
1464- check ('h' , 42 )
1465- check ('H' , 42 )
1466- check ('i' , 42 )
1467- check ('I' , 42 )
1468- check ('n' , 42 )
1469- check ('l' , 42 )
1470- check ('k' , 42 )
1471- check ('L' , 42 )
1472- check ('K' , 42 )
1473- check ('f' , 2.5 )
1474- check ('d' , 2.5 )
1475- check ('D' , 2.5j )
1476- check ('c' , b'a' )
1477- check ('C' , 'a' )
1478- check ('p' , True , allows_none = True )
1479- check ('y' , b'buffer' )
1480- check ('y*' , b'buffer' )
1481- check ('y#' , b'buffer' )
1482- check ('s' , 'string' )
1483- check ('s*' , 'string' )
1484- check ('s#' , 'string' )
1485- check ('z' , 'string' , allows_none = True )
1486- check ('z*' , 'string' , allows_none = True )
1487- check ('z#' , 'string' , allows_none = True )
1488- check ('w*' , bytearray (b'buffer' ))
1489- check ('U' , 'string' )
1490- check ('S' , b'bytes' )
1491- check ('Y' , bytearray (b'bytearray' ))
1492- check ('O' , object , allows_none = True )
1493-
1494- check ('(OO)' , (1 , 2 ))
1495- self .assertEqual (parse ((((1 , 2 ), 3 ),), {}, '((OO)?O)' , ['a' ]), (1 , 2 , 3 ))
1496- self .assertEqual (parse (((None , 3 ),), {}, '((OO)?O)' , ['a' ]), (NULL , NULL , 3 ))
1497- self .assertEqual (parse ((((1 , 2 ), 3 ),), {}, '((OO)O)' , ['a' ]), (1 , 2 , 3 ))
1498- self .assertRaises (TypeError , parse , ((None , 3 ),), {}, '((OO)O)' , ['a' ])
1499-
1500- parse ((None ,), {}, 'es?' , ['a' ])
1501- parse ((None ,), {}, 'es#?' , ['a' ])
1502- parse ((None ,), {}, 'et?' , ['a' ])
1503- parse ((None ,), {}, 'et#?' , ['a' ])
1504- parse ((None ,), {}, 'O!?' , ['a' ])
1505- parse ((None ,), {}, 'O&?' , ['a' ])
1506-
1507- # TODO: More tests for es?, es#?, et?, et#?, O!, O&
1508-
15091392 @unittest .skipIf (_testinternalcapi is None , 'needs _testinternalcapi' )
15101393 def test_gh_119213 (self ):
15111394 rc , out , err = script_helper .assert_python_ok ("-c" , """if True:
0 commit comments