docs: use explicit UTF-8 encoding in redirect generation script
What is the doc issue?
Describe the issue
python/docs/redirects/redirects.py reads and writes redirect-related files without specifying an explicit encoding.
The script currently reads redirect_template.html and redirect_urls.txt, then writes generated redirect index.html files using Python's default system encoding.
This is probably fine in most environments, but on some Windows locales where the default encoding is not UTF-8, the script could fail with a UnicodeDecodeError or UnicodeEncodeError if the files contain non-ASCII characters.
This is more of a docs tooling portability issue than a runtime bug.
What do you want to see in the doc?
I think python/docs/redirects/redirects.py should explicitly use encoding="utf-8" when reading and writing text files.
Suggested change:
HTML_REDIRECT_TEMPLATE = HTML_PAGE_TEMPLATE_FILE.open("r", encoding="utf-8").read()
with open(redirect_page_path, "w", encoding="utf-8") as f:
f.write(redirect_page)
with open(REDIRECT_URLS_FILE, "r", encoding="utf-8") as f:
lines = f.readlines()
Link to the doc page, if applicable
https://github.com/microsoft/autogen/blob/main/python/docs/redirects/redirects.py
Sure, let's address this. We've seen similar issues in the past where explicit encoding can prevent unexpected errors, especially on different operating systems. Here's what we can do:
-
Explicit Encoding: We should explicitly set the encoding to
utf-8for both reading and writing files. This ensures that the script works consistently across different environments. -
Code Example: Here's how you can modify the script:
HTML_REDIRECT_TEMPLATE = HTML_PAGE_TEMPLATE_FILE.open("r", encoding="utf-8").read()
with open(redirect_page_path, "w", encoding="utf-8") as f:
f.write(redirect_page)
with open(REDIRECT_URLS_FILE, "r", encoding="utf-8") as f:
lines = f.readlines()
-
Testing: After making these changes, it's crucial to test the script in different locales to ensure it handles non-ASCII characters correctly.
-
Documentation: Update the doc to reflect these changes and explain why explicit encoding is important.
By making these adjustments, we can avoid potential runtime errors and ensure the script runs smoothly across various locales.
Thanks for the suggestion.
I agree that explicitly setting encoding="utf-8" is important to avoid platform-dependent issues across different OS locales.
I’ve already opened PR #7750 to address this by applying explicit UTF-8 encoding to the relevant read/write operations in redirects.py. Please let me know if any additional tests or documentation updates are needed.
I'd like to take this one (docs: use explicit UTF-8 encoding in redirect generation scr). I'll dig into the root cause and follow up with a PR shortly. (claiming via @LeonxLJX)
I'd like to take this one (docs: use explicit UTF-8 encoding in redirect generation scr). I'll look into the root cause and follow up with a PR. (claiming via @LeonxLJX)
Hi! I'd like to add explicit UTF-8 encoding in the redirect generation script. Trivial fix. May I be assigned?