Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(docs): use explicit UTF-8 encoding in redirect generation script#7811

Open
aadhar-build wants to merge 1 commit into
microsoft:mainfrom
aadhar-build:fix/issue-7749-utf8-encoding-redirects
Open

fix(docs): use explicit UTF-8 encoding in redirect generation script#7811
aadhar-build wants to merge 1 commit into
microsoft:mainfrom
aadhar-build:fix/issue-7749-utf8-encoding-redirects

Conversation

@aadhar-build

Copy link
Copy Markdown

Summary

python/docs/redirects/redirects.py calls open() three times without specifying encoding. On Windows systems where the default locale is not UTF-8 (e.g. cp1252), this causes UnicodeDecodeError when processing files that contain UTF-8 characters.

Changed: Added encoding="utf-8" to all three open() calls in the file.

# Before
HTML_REDIRECT_TEMPLATE = HTML_PAGE_TEMPLATE_FILE.open("r").read()
with open(redirect_page_path, "w") as f:
with open(REDIRECT_URLS_FILE, "r") as f:

# After
HTML_REDIRECT_TEMPLATE = HTML_PAGE_TEMPLATE_FILE.open("r", encoding="utf-8").read()
with open(redirect_page_path, "w", encoding="utf-8") as f:
with open(REDIRECT_URLS_FILE, "r", encoding="utf-8") as f:

Related issue

Closes #7749

Testing

No logic changes — this is an encoding portability fix. Works on Linux/macOS (where UTF-8 is the default) and prevents UnicodeDecodeError on Windows with non-UTF-8 locales.

The redirects.py script read and wrote files without specifying an
encoding, which could cause UnicodeDecodeError or UnicodeEncodeError
on Windows systems where the default locale is not UTF-8.

Added encoding=\"utf-8\" to all three open() calls.

Fixes microsoft#7749

@avinashkamat48 avinashkamat48 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explicit encoding updates look good, but HTML_REDIRECT_TEMPLATE is still read at import time. If this script is imported from tooling before sys.argv/working paths are ready, any file-read error still happens during import rather than during main(). Could you move the template read into main() or generate_redirect() so importing the module stays side-effect free?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: use explicit UTF-8 encoding in redirect generation script

2 participants