Repository navigation
GH-39149: [Python] Validate max_replacements in ReplaceSubstringOptions - #53775
Closed
Asthenia0412 wants to merge 1 commit into
Closed
Asthenia0412 wants to merge 1 commit into
Asthenia0412 wants to merge 1 commit into
Conversation
…gOptions Add a ValueError when max_replacements is less than -1 (other than the special value -1 which means unlimited), preventing an indefinite hang in the underlying C++ replace_substring_regex kernel. Also update the docstring to clarify the valid value range and add a regression test.
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.
Background
Issue #39149 reports that
pc.replace_substring_regex()hangs indefinitely when passed a negativemax_replacementsvalue (other than -1 which means unlimited). The root cause is that the C++ kernel decrementsmax_replacementsuntil it reaches 0, but a value like -3 becomes -4, -5, ... forever.Problem
The Python layer's
ReplaceSubstringOptions.__init__does not validate thatmax_replacements >= -1, allowing negative values to reach the C++ kernel and cause hangs.Solution
Add input validation on the Python side to reject
max_replacements < -1with a clearValueError. This is a defensive fix that fails fast before the C++ kernel is invoked.Testing
Added
test_replace_substring_negative_max_replacementscovering:max_replacements=-2,-3-> raises ValueError (original bug scenario)max_replacements=-1,None-> accepted, treated as unlimitedmax_replacements=0,5-> accepted non-negative values