Return at once when repeating an empty list or deque

Merged
GR
24d ago
+28-3

[] * n runs the clone loop in py_mul_impl n times over an empty slice, since the size preflight passes when
0 * n is 0.
[] * 2**40 does not return; CPython returns [] at once.
deque() * n without a maxlen has the same loop in repeat_deque.

The tuple path already returns early for count == 0 || len == 0.

This PR:

  • adds the same guard to py_mul_impl in list.rs;
  • clamps count to 0 for an empty source in repeat_deque;
  • adds [] * 2**40 and deque() * 2**40 to list__ops.py and collections__deque.py (both time out without the fix),
    plus [] * 2**63 still raising OverflowError and deque(maxlen=3) * 2**40.

No limitations/ change: the behaviour now matches CPython.


Summary by cubic

Fixes [] * n and deque() * n with empty operands so they return immediately, matching CPython's behavior.

Previously, [] * 2**40 and deque() * 2**40 spun an empty loop over count iterations and never returned. The tuple path already handled this; now the list path short-circuits directly and repeat_deque clamps the count to 0 for an empty source, while oversized counts still raise OverflowError as CPython does. Tests cover the immediate return for huge counts and the still-raised OverflowError for non-index-sized counts.