Skip to content
Merged
Show file tree
Hide file tree
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
Fix iteration protocol: tp_iter inheritance and sequence-protocol fal…
…lback

1. make_iter followed tp_base only, so a dict subclass whose first base is
a plain class (class PolyElement(DomainElement, ..., dict)) lost dict's
__iter__ and iter() failed. Resolve tp_iter with search_slot (full MRO).
2. The __getitem__ fallback iterator required __len__ and crashed with a
raw JS error when it was missing. CPython semantics: only __getitem__ is
required, and IndexError ends the iteration.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
  • Loading branch information
zalo and claude committed Aug 13, 2026
commit 91adf780b3243a88df9b5752ffd10c317567d2c0
36 changes: 27 additions & 9 deletions www/src/py_builtin_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,10 @@ $B.$hash = function(obj) {
if (! $B.is_int(res)) {
$B.RAISE(_b_.TypeError, '__hash__ method should return an integer')
}
if (typeof res == 'object') {
// int subclass: hash of its integer value
res = _b_.int.tp_hash(res)
}
} else {
$B.RAISE(_b_.TypeError, "unhashable type: '" +
_b_.str.$factory($B.jsobj2pyobj(obj)) + "'"
Expand Down Expand Up @@ -1215,18 +1219,31 @@ var issubclass = _b_.issubclass = function(cls, class_or_tuple) {
/* iterator start */
$B.iterator.tp_iter = function(self) {
var ob_type = $B.get_class(self.it_seq)
self.len = $B.search_in_mro(ob_type, '__len__')(self.it_seq)
self.getitem = $B.search_in_mro(ob_type, '__getitem__')
var getitem = $B.search_in_mro(ob_type, '__getitem__', $B.NULL)
if (getitem === $B.NULL) {
$B.RAISE(_b_.TypeError,
`'${$B.class_name(self.it_seq)}' object is not iterable`)
}
self.getitem = getitem
self.it_index = 0
return self
}

$B.iterator.tp_iternext = function*(self){
if (self.it_index < self.len) {
var res = self.getitem(self.it_seq, self.it_index)
self.it_index++
yield res
// "sequence protocol" iteration: call __getitem__ with increasing
// indices; IndexError ends the iteration (__len__ is not used, same
// as CPython)
var res
try {
res = $B.$call(self.getitem, self.it_seq, self.it_index)
} catch (err) {
if ($B.is_exc(err, [_b_.IndexError, _b_.StopIteration])) {
return
}
throw err
}
self.it_index++
yield res
}

var iterator_funcs = $B.iterator.tp_funcs = {}
Expand Down Expand Up @@ -1307,13 +1324,14 @@ $B.$iter = function(obj, sentinel) {
}
return res
}
// fallback for the "sequence protocol": objects with __getitem__
// are iterated with increasing indices until IndexError; __len__ is
// not required (same as CPython)
var getitem_func = $B.search_in_mro(klass, '__getitem__', $B.NULL)
var len_func = $B.search_in_mro(klass, '__len__', $B.NULL)
if (test) {
console.log('getitem_func', getitem_func)
console.log('len_func', len_func)
}
if (getitem_func !== $B.NULL && len_func !== $B.NULL) {
if (getitem_func !== $B.NULL) {
var it = {
ob_type: $B.iterator,
it_seq: obj
Expand Down
23 changes: 17 additions & 6 deletions www/src/py_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -956,13 +956,24 @@ function reset_factory(cls) {
}

$B.make_iter = function(cls) {
// resolve the tp_iter slot by walking the full MRO, not only tp_base:
// __iter__ can be inherited from any ancestor, eg a dict subclass whose
// first base is a plain class. Only raw class attributes are used: a
// descriptor must not have its __get__ invoked at slot-resolution time
// (it would have side effects, cf unittest.mock.MagicProxy); descriptors
// are handled at call time by $B.$iter
cls.tp_iter = $B.NULL
var iter = $B.get_from_dict(cls, '__iter__', $B.NULL)
if (iter !== $B.NULL) {
cls.tp_iter = iter
} else if (cls.tp_base) {
cls.tp_iter = cls.tp_base.tp_iter ??
(cls.tp_base.tp_iter = $B.make_iter(cls.tp_base))
for (var klass of $B.get_mro(cls)) {
var iter = $B.get_from_dict(klass, '__iter__', $B.NULL)
if (iter !== $B.NULL) {
cls.tp_iter = iter
break
}
if (klass !== cls && Object.hasOwn(klass, 'tp_iter') &&
klass.tp_iter !== $B.NULL && klass.tp_iter != null) {
cls.tp_iter = klass.tp_iter
break
}
}
return cls.tp_iter
}
Expand Down