Repository navigation
Save a locustfile downloaded from a URL byte for byte - #3577
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
download_locustfile_from_url() wrote response.text to a text-mode file opened with the locale encoding. When the server sends no charset, requests decodes text/* bodies as ISO-8859-1, so non-ASCII characters were double-encoded. On Windows the locale encoding (cp1252, cp932) either cannot encode the text (UnicodeEncodeError) or writes bytes that Python then refuses to import as UTF-8 source. Write response.content in binary mode instead, and check it with ast.parse() on the same bytes, so Python decodes the file the same way when it imports it. test_locustfile_from_url now compares the files as text, because a Windows checkout of examples/basic.py has CRLF line endings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
|
nice |
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.
locust -f <url>saves the download withopen(path, "w").write(response.text). Non-ASCII text in the locustfile (a Japanese comment,print("café")) breaks in two ways:python -m http.serversendstext/x-python; many S3/nginx setups send baretext/plain), requests decodes the body as ISO-8859-1 and the file is saved double-encoded, soprint("こんにちは")printsããã«ã¡ã¯.charset=utf-8(raw.githubusercontent.com), a Japanese comment is saved as cp932 and the import fails withSyntaxError: (unicode error) 'utf-8' codec can't decode byte 0x82 in position 0: invalid start byte. A character the code page can't encode fails while saving (UnicodeEncodeError: 'cp932' codec can't encode character ...).The download is Python source, so this saves
response.contentunchanged and lets Python decode it on import (UTF-8, or the file's coding line).ast.parse()now checks the same bytes.Tests:
test_locustfile_from_url_is_saved_byte_for_byteserves a non-ASCII locustfile from a local gevent server astext/x-pythonand compares the saved bytes. It fails on master (double-encoded bytes in UTF-8 mode,UnicodeEncodeErrorwith cp932) and passes with the change.test_locustfile_from_urlcompared the GitHub download withexamples/basic.pyusingfilecmp. On Windows that file is checked out with CRLF (*.py text), and it only matched because the old text-mode write also turned LF into CRLF. It now compares the two as text.I used Claude Code to write this change.