Fix StopIteration crash in create_array with empty generator#99
Merged
Conversation
When no nrows is provided and the iterable has no __len__, create_array called get_ncols() which consumed the first element via peek(). An empty generator raised an unhandled StopIteration instead of returning a zero-row array, unlike create_structured_array which handled this case. Catches StopIteration in the else-branch and returns np.zeros((0, 0)) to match the documented empty-iterable contract. Closes #96.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
create_arraycrashed withStopIterationwhen passed an empty generator and nonrows, becauseget_ncols()→peek()→next()consumed the first element of an empty iterator with no guardcreate_structured_arrayalready handled this correctly by delegating directly tocreate_chunked_structured_array, which has an explicit empty-iterable fallbacktry/except StopIterationin theelse-branch ofcreate_arrayto returnnp.zeros((0, 0), dtype=dtype)when the iterator is emptyCloses #96.
Test plan
test_create_array_empty_generator_returns_empty_array— empty generator returns shape(0, 0)with default dtypetest_create_array_empty_generator_respects_dtype— dtype is preserved on the empty resulttest_create_array_nonempty_generator— non-empty generator still produces correct shape and values (regression)