Raise IndexError for deque indices below -len. Fixes oracle/graalpython#923#927
Raise IndexError for deque indices below -len. Fixes oracle/graalpython#923#927ztawfick-ux wants to merge 1 commit into
Conversation
Signed-off-by: Zac Tawfick <ztawfick@netflix.com>
|
Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application. When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated. If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public. |
|
My Company has already signed the OCA. My membership request is currently under review. |
|
Looks good! Thank you for your contribution. I'll wait for the OCA to go though and then integrate. |
Summary
Subscripting a
dequewith an index less than-len(d)wrapped around a second time instead of raisingIndexError. On a length-5 deque,d[-6]returnedd[4]rather than raising, andIndexErroronly appeared once the index went below-2*len. This affected reads, assignments, and deletions via the subscript operator. Fixes #923.Root cause
The negative index was normalized twice:
PyObjectGetItem/PyObjectSetItem/PyObjectDelItem→IndexForSqSlot) already addslen(self)to a negative index, mirroring CPython'sPySequence_GetItem.DequeGetItemNode(sq_item) andDequeSetItemNode(sq_ass_item) then addedlen(self)again viaNormalizeIndexCustomMessageNode.So
d[-6]on a length-5 deque became-6 + 5 = -1(step 1) and then-1 + 5 = 4(step 2), reading a valid slot instead of raising.This is deque-specific: every other builtin sequence (
list,tuple,str,bytes,array, …) also definesmp_subscript, so its subscripting goes through the mapping slot (normalized once).dequeis the only builtin sequence that definessq_item/sq_ass_itemwithout themp_*counterparts, so its subscripting goes through the sequence-slot path that double-normalized.Fix
Aligns deque with CPython's division of labor (the caller normalizes; the slot only bounds-checks):
DequeGetItemNode/DequeSetItemNodenow only perform a bounds check and raiseIndexError, matching_collectionsmodule.c:deque_item/deque_ass_item(and howlist'ssq_itemslot,SequenceStorageSqItemNode, already behaves).sq_*wrappersWrapSqItemBuiltinNode,WrapSqSetItemBuiltinNode, andWrapSqDelItemBuiltinNodenow passself(not the index object) toFixNegativeIndex, so the adjustment useslen(self), matching CPython'stypeobject.c:getindex. Previously they passed the index object, makingFixNegativeIndexcomputelen(index)— a no-op for integer indices. That latent bug was masked while the slot still normalized; once the slot stops normalizing, the wrapper must do the single normalization so the__getitem__/__setitem__/__delitem__method path stays correct.Testing
test_negative_index_out_of_rangeintest_deque.py(coversd[i],d[i] = x,del d[i]fori < -len).