fix: accept start as keyword arg in enumerate builtin#395
fix: accept start as keyword arg in enumerate builtin#395MukundaKatta wants to merge 1 commit intopydantic:mainfrom
Conversation
Matches CPython's `enumerate(iterable, start=0)` signature so that `start` works as both a positional and a keyword argument. Closes pydantic#394.
Merging this PR will not alter performance
Comparing Footnotes
|
| let (iterable, start) = parse_enumerate_args(args, vm)?; | ||
| let iter = MontyIter::new(iterable, vm)?; | ||
| defer_drop_mut!(iter, vm); | ||
| defer_drop!(start, vm); |
There was a problem hiding this comment.
🔴 Reference count leak / panic: start not protected by defer_drop! before fallible MontyIter::new call
If MontyIter::new(iterable, vm)? fails (e.g. enumerate(42, [1, 2, 3]) where 42 is not iterable), the ? operator causes an early return before defer_drop!(start, vm) on line 27 is reached. When start holds a Value::Ref (heap-allocated), this means drop_with_heap is never called — with the memory-model-checks feature (used in CI), this panics; without it, it silently leaks the reference count.
The correct pattern — used by analogous builtins sum (crates/monty/src/builtins/sum.rs:23) and filter (crates/monty/src/builtins/filter.rs:35) — is to register the secondary value with defer_drop! before the fallible MontyIter::new call.
| let (iterable, start) = parse_enumerate_args(args, vm)?; | |
| let iter = MontyIter::new(iterable, vm)?; | |
| defer_drop_mut!(iter, vm); | |
| defer_drop!(start, vm); | |
| let (iterable, start) = parse_enumerate_args(args, vm)?; | |
| defer_drop!(start, vm); | |
| let iter = MontyIter::new(iterable, vm)?; | |
| defer_drop_mut!(iter, vm); |
Was this helpful? React with 👍 or 👎 to provide feedback.
Closes #394.
The
enumerate(iterable, start)shim only acceptedstartpositionally, but stdlibenumerateallowsstartas a keyword (enumerate(iter, start=0)). Switched the parser to walkkwargssostart=works as a keyword while preserving the existing positional and arity errors, mirroring the kwarg-handling pattern used inzipandsorted.Test added in
crates/monty/test_cases/builtin__more_iter_funcs.pycovering the issue's reproducer and an unknown-kwarg rejection.