Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(🐞) blackd: fix issue for mishandling single character input #3558

Merged
merged 3 commits into from Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -43,6 +43,8 @@

<!-- Changes to blackd -->

- fix a bug where a single character would cause a 500 (#3558)
hauntsaninja marked this conversation as resolved.
Show resolved Hide resolved

### Integrations

<!-- For example, Docker, GitHub Actions, pre-commit, editors -->
Expand Down
3 changes: 2 additions & 1 deletion src/blackd/__init__.py
Expand Up @@ -152,7 +152,8 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
)

# Preserve CRLF line endings
if req_str[req_str.find("\n") - 1] == "\r":
nl = req_str.find("\n")
if nl > 0 and req_str[nl - 1] == "\r":
formatted_str = formatted_str.replace("\n", "\r\n")
# If, after swapping line endings, nothing changed, then say so
if formatted_str == req_str:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_blackd.py
Expand Up @@ -240,3 +240,9 @@ async def test_normalizes_line_endings(self) -> None:
response = await self.client.post("/", data=data)
self.assertEqual(await response.text(), expected)
self.assertEqual(response.status, 200)

@unittest_run_loop
async def test_single_character(self) -> None:
response = await self.client.post("/", data="1")
self.assertEqual(await response.text(), "1\n")
self.assertEqual(response.status, 200)