Skip to content

itertools.compress(x) calls x.__iter__() before reporting the missing selectors argument #9506

Open

Description

@jseop-lim

Feature

A call that leaves out a required argument converts the arguments it did pass first, so an exception from that conversion replaces the TypeError for the missing one. CPython counts the arguments before converting any.

Reproduction

import itertools


class RaisingIter:
    def __iter__(self):
        raise RuntimeError("__iter__ called")


itertools.compress(RaisingIter())

RustPython:

RuntimeError: __iter__ called

CPython:

TypeError: compress() missing required argument 'selectors' (pos 2)

itertools.compress(data=RaisingIter()) behaves the same.

Root cause

CompressNewArgs declares data as PyIter, and the FromArgs derive converts each field as soon as it takes it and reports a missing one only when it reaches that field (from_args.rs), so data.__iter__() runs first. FuncArgs::bind_for checks only the upper bound before T::from_args, while CPython's _PyArg_UnpackKeywords checks both bounds before itertools_compress_impl calls PyObject_GetIter.

Fix

In bind_for, before T::from_args, find the first required positional parameter in T::PARAMS that no positional or keyword argument fills and raise its error, next to the surplus check from #9242.

Environment

  • RustPython 713e14c (Python 3.15.0a), also reproduced on 0.4.0 and 0.6.1
  • CPython v3.15.2, also v3.14.4 and v3.12.13
  • OS: macOS arm64 (713e14c), Debian 12 (0.4.0, 0.6.1)

Python Documentation or reference to CPython source code

  • CPython itertools_compress: Modules/clinic/itertoolsmodule.c.h#L860-L867 settles the argument count before calling the implementation:

    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
    if (!fastargs) {
        goto exit;
    }
    seq1 = fastargs[0];
    seq2 = fastargs[1];
    return_value = itertools_compress_impl(type, seq1, seq2);
  • CPython itertools_compress_impl: Modules/itertoolsmodule.c#L3251-L3254 calls iter() on each argument only after that.

  • Documentation: itertools.compress

AI disclosure: this report was drafted with Claude Code (claude-opus-5-5) and reviewed by me before submission. The divergence comes from a differential-testing run against CPython 3.12.13, and every source reference above was checked against the linked revisions.

Activity

  1. added
    C-compatA discrepancy between RustPython and CPython
    on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions