Fix TypeError in bash parsing with unclosed backticks (#7392)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Xingyao Wang 2025-03-25 14:38:01 -07:00 committed by GitHub
parent 78b67bc9d9
commit 951cb1c880
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 2 deletions

View File

@ -25,7 +25,7 @@ def split_bash_commands(commands: str) -> list[str]:
return ['']
try:
parsed = bashlex.parse(commands)
except (bashlex.errors.ParsingError, NotImplementedError):
except (bashlex.errors.ParsingError, NotImplementedError, TypeError):
logger.debug(
f'Failed to parse bash commands\n'
f'[input]: {commands}\n'
@ -145,7 +145,7 @@ def escape_bash_special_chars(command: str) -> str:
remaining = command[last_pos:]
parts.append(remaining)
return ''.join(parts)
except (bashlex.errors.ParsingError, NotImplementedError):
except (bashlex.errors.ParsingError, NotImplementedError, TypeError):
logger.debug(
f'Failed to parse bash commands for special characters escape\n'
f'[input]: {command}\n'

View File

@ -155,6 +155,31 @@ def test_invalid_syntax():
assert split_bash_commands(input_command) == [input_command]
def test_unclosed_backtick():
# This test reproduces issue #7391
# The issue occurs when parsing a command with an unclosed backtick
# which causes a TypeError: ParsingError.__init__() missing 2 required positional arguments: 's' and 'position'
command = 'echo `unclosed backtick'
# Should not raise TypeError
try:
result = split_bash_commands(command)
# If we get here, the error was handled properly
assert result == [command]
except TypeError as e:
# This is the error we're trying to fix
raise e
# Also test with the original command from the issue (with placeholder org/repo)
curl_command = 'curl -X POST "https://api.github.com/repos/example-org/example-repo/pulls" \\ -H "Authorization: Bearer $GITHUB_TOKEN" \\ -H "Accept: application/vnd.github.v3+json" \\ -d \'{ "title": "XXX", "head": "XXX", "base": "main", "draft": false }\' `echo unclosed'
try:
result = split_bash_commands(curl_command)
assert result == [curl_command]
except TypeError as e:
raise e
@pytest.fixture
def sample_commands():
return [