Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
array.array: support negative indices in getitem/setitem
a[-1] and a[-1] = v raised IndexError because the index was used directly
as a JS array subscript.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
  • Loading branch information
zalo and claude committed Aug 13, 2026
commit 63596f41a594436076217705c425e6b7d9de30a5
8 changes: 8 additions & 0 deletions www/src/libs/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ array.bf_getbuffer = function(_self, flag) {

array.mp_subscript = function(_self, key) {
if (_self.obj) {
if ($B.is_int(key) && key < 0) {
// Python semantics: negative indices count from the end
key = _self.obj.length + Number(key)
}
if (_self.obj[key] !== undefined) {
return _self.obj[key]
} else if ($B.$isinstance(key, _b_.slice)) {
Expand Down Expand Up @@ -120,6 +124,10 @@ array.bf_release_buffer = function(_self, buffer) {

array.mp_ass_subscript = function(_self, index, value) {
if ($B.is_int(index)) {
if (index < 0) {
// Python semantics: negative indices count from the end
index = _self.obj.length + Number(index)
}
if (_self.obj[index] === undefined) {
$B.RAISE(_b_.IndexError, "array index out of range")
}
Expand Down