Skip to content

GH-39149: [Python] Validate max_replacements in ReplaceSubstringOptions - #53775

Closed
Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix/replace-substring-negative-max-replacements
Closed

Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix/replace-substring-negative-max-replacements

Conversation

@Asthenia0412

@Asthenia0412 Asthenia0412 commented Oct 14, 2026 •

Copy link
Copy Markdown

Background

Issue #39149 reports that pc.replace_substring_regex() hangs indefinitely when passed a negative max_replacements value (other than -1 which means unlimited). The root cause is that the C++ kernel decrements max_replacements until it reaches 0, but a value like -3 becomes -4, -5, ... forever.

Problem

The Python layer's ReplaceSubstringOptions.__init__ does not validate that max_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 < -1 with a clear ValueError. This is a defensive fix that fails fast before the C++ kernel is invoked.

Testing

Added test_replace_substring_negative_max_replacements covering:

  • max_replacements=-2, -3 -> raises ValueError (original bug scenario)
  • max_replacements=-1, None -> accepted, treated as unlimited
  • max_replacements=0, 5 -> accepted non-negative values

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant