-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Set encodings when reading config files #2996
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
base: main
Are you sure you want to change the base?
Set encodings when reading config files #2996
Conversation
Fixes UnicodeDecodeError by trying utf-8, euc-kr, gbk, cp949, latin1 encodings sequentially until successful decode.
…simplify file reading logic
Hi @secrett2633 , Thanks for the sponsorship! :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem all that is needed.
I guess we need to pass encoding
from the Config
class, otherwise this can't ever be different.
Although, I'm inclined to suggest users to use PYTHONUTF8=1
on their environment.
Thanks! Happy to help. 😊 @Kludex You're right that without passing encoding from the I've adjusted the implementation based on that feedback. |
Summary
Fixes UnicodeDecodeError by allowing explicit encoding selection when reading .env files.
Resolves #2016
Description
Modified the _read_file method in starlette/config.py to accept an encoding parameter.
This ensures .env files can be correctly decoded regardless of the system's default locale encoding.
Key changes:
Motivation
The current implementation uses open(file_name) without specifying an encoding, which defaults to the system’s locale (e.g., GBK on Windows in Chinese locales).
This leads to UnicodeDecodeError when the .env file is encoded in UTF-8 (a common format).
Expected Outcome
After this fix:
Checklist