blackd: fix mishandling of single character input (#3558)

This commit is contained in:
KotlinIsland 2023-09-07 17:11:50 +10:00 committed by GitHub
parent df50fee7fd
commit 8daa64a2e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -45,6 +45,8 @@
<!-- Changes to blackd --> <!-- Changes to blackd -->
- Fix an issue in `blackd` with single character input (#3558)
### Integrations ### Integrations
<!-- For example, Docker, GitHub Actions, pre-commit, editors --> <!-- For example, Docker, GitHub Actions, pre-commit, editors -->

View File

@ -152,7 +152,8 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
) )
# Preserve CRLF line endings # 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") formatted_str = formatted_str.replace("\n", "\r\n")
# If, after swapping line endings, nothing changed, then say so # If, after swapping line endings, nothing changed, then say so
if formatted_str == req_str: if formatted_str == req_str:

View File

@ -240,3 +240,9 @@ async def test_normalizes_line_endings(self) -> None:
response = await self.client.post("/", data=data) response = await self.client.post("/", data=data)
self.assertEqual(await response.text(), expected) self.assertEqual(await response.text(), expected)
self.assertEqual(response.status, 200) 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)