Skip to content

Iterate a list or tuple subclass as Python does (0441) - #827

Merged
rmorozov merged 5 commits into
masterfrom
claude/project-thread-rh0ykr
Nov 10, 2026
Merged

rmorozov merged 5 commits into
masterfrom
claude/project-thread-rh0ykr

Conversation

@rmorozov

@rmorozov rmorozov commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Requested by Ruslan · project thread

Problem

Since 0357 S3 the Python module goes through the public host-object contract. That contract iterates every IsSequence object that has a Len by index (HostSequenceList: Len + ItemAtTo). A list subclass that overrides __iter__ therefore looped over its stored items:

class L(list):
    def __iter__(self): return iter(["x", "y"])
Template("{% for i in l %}{{ i }}{% endfor %}").render(l=L([1, 2, 3]))  # Jinja2: xy, module: 123

The verifier found a second case. A subclass that overrides only __getitem__ looped through that override (GGG), but Python's list iterator ignores it and gives 123.

Change

  • Public API. New public virtual IHostObject::IteratesByIndex(), true by default. EngineBridge::Iterate uses HostSequenceList only when it returns true. Otherwise it goes through Iterate. This adds a vtable slot before the G1 freeze, as the backlog report recommended, and is recorded in docs/api-2.0.md section 7 item 10.
  • Python module. PyHost::IteratesByIndex is true only for an exact list or tuple. A subclass is iterated through iter(): its own __iter__ if it has one, otherwise the base type's iterator, which is also live. The exact-type fast path is unchanged.
  • Codex follow-up: list operators. +, *, comparisons and str % x keep reading the stored items, as Python's list operations do. host::AsSequence now reads a new private IEngineHostObject::SequenceItems(), which the bridge answers by index.
  • Codex follow-up: loop.length. A loop over any host object takes loop.length (and revindex) from the object's Len when the template first reads it, as Jinja2 takes len(iterable) (SetLengthOfValue, src/statements.cpp). It falls back to counting the items when the object has no length. loop.last still comes from peeking.

Validation

  • PublicHostObjectTest.SequenceThatIteratesByItsOwnIterate covers a C++ host with its own Iterate, Len 3 and 2 iterated items: the loop, loop.length, last, + and *, narrow and wide.
  • python/tests/test_values.py::test_list_and_tuple_subclasses_iterate_as_python_does has 8 rows. They cover __iter__ and __getitem__ overrides, a plain subclass, a non-sequence with __len__, the loop attributes and the operators.
    • The contexts are built fresh for each engine, because render_both's deepcopy rebuilds a list subclass from its __iter__ and would hide the case (verifier F1).
    • With IteratesByIndex forced true, 4 of the first 5 rows fail.
  • Debug build: ctest 8/8 passes. Python module (3.12, limited API): full python/tests gives 1629 passed, 17 skipped, 2 xfailed.
  • Verifier (round 1, before the Codex fixes): ASan+UBSan clang build, PublicHostObject* 47/47 with no reports. The module under ASan had no sanitizer reports across a 90-template edge matrix.
  • scripts/preflight.sh --perf passes. The instruction gate shows at most +0.06%, on mitsuhiko_table.

Remaining limits

Task 0453: l|pprint of such a subclass prints its stored items, while Jinja2's pprint iterates it. It printed the same way before this PR. The task was first numbered 0452, but another thread took that number at the same time.

Roles: verifier, 1 round (F1 and F3 fixed, F2 and F4 first filed, then fixed after Codex). Red CI pushes: none so far.
Codex: 2 findings (P2), 2 fixed, 0 declined. Security review: not needed. The diff touches the host-object bridge and the loop's length source, not the lexer, parsers, loaders, escaping or the sandbox.

🤖 Generated with Claude Code

https://claude.ai/code/session_01AWNL7mqnaXzqZnVND5kuSG

IHostObject::IteratesByIndex() (true by default) lets a sequence say that
iter(x) differs from its items by index. The bridge then iterates it
through Iterate instead of Len and ItemAtTo. PyHost answers false for a
list or tuple subclass that overrides __iter__, so a loop over it goes
through __iter__ as in Jinja2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mqnaXzqZnVND5kuSG
A subclass that overrides only __getitem__ looped through it, while
Python's list iterator reads the list's own items. The Python rows are
built afresh per engine: a deep copy of a list subclass is rebuilt from
its __iter__ and hid the case. Filed 0452 for loop.length and operators.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mqnaXzqZnVND5kuSG
@rmorozov rmorozov self-assigned this Nov 10, 2026

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T22:31:24.498763Z c455b69 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 330371990d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/public_host_object.cpp Resolved
Comment thread src/public_host_object.cpp Resolved
…441)

Codex on #827: a list subclass iterated through its __iter__ lost
loop.length (Jinja2 takes len(iterable)) and + * < and str % read the
iterated items. A loop over a host object now takes its length from Len
when first read, and host::AsSequence reads the new private
IEngineHostObject::SequenceItems, by index. 0453 narrows to pprint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mqnaXzqZnVND5kuSG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AWNL7mqnaXzqZnVND5kuSG

Copy link
Copy Markdown
Member Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: c455b69b2c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@rmorozov
rmorozov marked this pull request as ready for review November 10, 2026 22:31
@rmorozov
rmorozov merged commit 1cf228b into master Nov 10, 2026
48 checks passed
rmorozov pushed a commit that referenced this pull request Nov 10, 2026
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DTQTriEsFrrBgcYNKDUUF4
rmorozov added a commit that referenced this pull request Nov 10, 2026
Mark 0441 done after #827; sync index and G1 row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants