Repository navigation
Conversation
PyObjectSetItem and PyObjectDelItem raised TypeError as soon as the type had neither mp_ass_subscript nor sq_ass_item, without looking at the key. CPython first converts an index key with PyNumber_AsSsize_t(key, IndexError) when the type has a sequence protocol, so an exception from __index__, or IndexError for a key that does not fit in 64 bits, is raised instead. Add that branch to both nodes, checking the 64-bit range rather than GraalPy's 32-bit index size, since the converted value is not used. Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
| static void doSequenceNoAssItem(VirtualFrame frame, Node inliningTarget, Object object, @SuppressWarnings("unused") TpSlots slots, Object key, | ||
| @Cached PyIndexCheckNode indexCheckNode, | ||
| @Cached PyNumberIndexNode indexNode) { | ||
| if (indexCheckNode.execute(inliningTarget, key) && indexNode.execute(frame, inliningTarget, key) instanceof PInt pint && pint.bitLength() >= Long.SIZE) { |
There was a problem hiding this comment.
Can you do the PyNumberAsSsize conversion instead of the PyNumberIndexNode call followed by the PInt check? I don't want to hardcode that PInt is the only possible way to represent a large integer, because we might support native integers.
There was a problem hiding this comment.
I used PyLongAsLongAndOverflowNode instead of PyNumberAsSizeNode, so there is no PInt check left and native integers would go through that node. PyNumberAsSizeNode is 32-bit here, so a key such as 2**40 would raise IndexError where CPython raises TypeError (CPython converts to a 64-bit Py_ssize_t and only uses the result for the overflow check). SliceLossyCastToLong does the same conversion.
There was a problem hiding this comment.
Our sizes are 32 bit because of Java limitations, so raising IndexError in that case is the correct thing to do. We shouldn't pretend our indices are 64-bit, the test needs to respect sys.maxsize
Use PyLongAsLongAndOverflowNode instead of PyNumberIndexNode and a PInt bit-length check, so the overflow check does not assume PInt is the only representation of a large integer, as requested in review. PyNumberAsSizeNode would check against GraalPy's 32-bit index size and turn the TypeError for a key such as 2**40 into IndexError. CPython converts to a 64-bit Py_ssize_t only for the errors and discards the value, so keep that boundary, as SliceLossyCastToLong does. Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
Description
Fixes #1255
PyObjectSetItemandPyObjectDelItemconverted an index key only for types withsq_ass_item, so on a type with sequence slots but nosq_ass_item(tuple,str,bytes,range,frozenset, a plain class) they raisedTypeErrorwithout calling__index__. CPython'sPyObject_SetItemandPyObject_DelItemconvert the key whenever the type hastp_as_sequence, so an error from__index__, orIndexErrorfor an oversized int, comes first (abstract.c#L237-L252,#L271-L286).AS-IS
TO-BE
(matching CPython)
Changes
doSequenceNoAssItemspecialization toPyObjectSetItemGenericandPyObjectDelItemGenericformp_ass_subscript == null,sq_ass_item == nullandhas_as_sequence(). It converts an index key withPyLongAsLongAndOverflowNode, raisesIndexErrorwhen it does not fit in 64 bits, and otherwise raises the existingTypeError. The converted value is not used, so the check follows CPython's 64-bitPy_ssize_trather than GraalPy's 32-bit index size, and a key such as2**40keeps raisingTypeError.test_setitem_delitem_convert_index_before_type_errortotests/test_operator.py.Testing
mx python-jvmon darwin-aarch64.tests/test_operator.pypasses (4 tests), and the new test fails on the unpatched build. The taggedlib-pythonsuitestest_operator,test_index,test_list,test_tuple,test_bytes,test_genericalias,test_types,test_userlist,test_dict,test_arrayandtest_dequepass. Setting and deleting with four keys (a raising__index__,2**100,0and"k") on 34 builtin and user-defined objects now gives the same exception class as CPython 3.13.14, as do the boundary keys2**31,2**40,2**63 - 1,2**63,-2**63and-2**63 - 1, apart fromitertools.count, which has no sequence slots in GraalPy. Types without sequence slots, such astypes.GenericAliasandtypes.UnionType, still raiseTypeError.