Raise IndexError for deque indices below -len. Fixes oracle/graalpython#923#927
Open
ztawfick-ux wants to merge 1 commit into
Open
Raise IndexError for deque indices below -len. Fixes oracle/graalpython#923#927ztawfick-ux wants to merge 1 commit into
ztawfick-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. |
Author
|
My Company has already signed the OCA. My membership request is currently under review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).