mirror of https://github.com/pallets/flask.git
Set default encoding to UTF-8 for load_dotenv
This commit is contained in:
parent
6d9d79c70d
commit
33145c3699
|
|
@ -619,6 +619,9 @@ def load_dotenv(path=None):
|
||||||
Returns ``False`` when python-dotenv is not installed, or when
|
Returns ``False`` when python-dotenv is not installed, or when
|
||||||
the given path isn't a file.
|
the given path isn't a file.
|
||||||
|
|
||||||
|
.. versionchanged:: 2.0
|
||||||
|
When loading the env files, set the default encoding to UTF-8.
|
||||||
|
|
||||||
.. versionadded:: 1.0
|
.. versionadded:: 1.0
|
||||||
"""
|
"""
|
||||||
if dotenv is None:
|
if dotenv is None:
|
||||||
|
|
@ -636,7 +639,7 @@ def load_dotenv(path=None):
|
||||||
# else False
|
# else False
|
||||||
if path is not None:
|
if path is not None:
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
return dotenv.load_dotenv(path)
|
return dotenv.load_dotenv(path, encoding="utf-8")
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
@ -651,7 +654,7 @@ def load_dotenv(path=None):
|
||||||
if new_dir is None:
|
if new_dir is None:
|
||||||
new_dir = os.path.dirname(path)
|
new_dir = os.path.dirname(path)
|
||||||
|
|
||||||
dotenv.load_dotenv(path)
|
dotenv.load_dotenv(path, encoding="utf-8")
|
||||||
|
|
||||||
return new_dir is not None # at least one file was located and loaded
|
return new_dir is not None # at least one file was located and loaded
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
FOO=env
|
FOO=env
|
||||||
SPAM=1
|
SPAM=1
|
||||||
EGGS=2
|
EGGS=2
|
||||||
|
HAM=火腿
|
||||||
|
|
|
||||||
|
|
@ -505,7 +505,7 @@ need_dotenv = pytest.mark.skipif(dotenv is None, reason="dotenv is not installed
|
||||||
@need_dotenv
|
@need_dotenv
|
||||||
def test_load_dotenv(monkeypatch):
|
def test_load_dotenv(monkeypatch):
|
||||||
# can't use monkeypatch.delitem since the keys don't exist yet
|
# can't use monkeypatch.delitem since the keys don't exist yet
|
||||||
for item in ("FOO", "BAR", "SPAM"):
|
for item in ("FOO", "BAR", "SPAM", "HAM"):
|
||||||
monkeypatch._setitem.append((os.environ, item, notset))
|
monkeypatch._setitem.append((os.environ, item, notset))
|
||||||
|
|
||||||
monkeypatch.setenv("EGGS", "3")
|
monkeypatch.setenv("EGGS", "3")
|
||||||
|
|
@ -520,7 +520,8 @@ def test_load_dotenv(monkeypatch):
|
||||||
assert os.environ["SPAM"] == "1"
|
assert os.environ["SPAM"] == "1"
|
||||||
# set manually, files don't overwrite
|
# set manually, files don't overwrite
|
||||||
assert os.environ["EGGS"] == "3"
|
assert os.environ["EGGS"] == "3"
|
||||||
|
# test env file encoding
|
||||||
|
assert os.environ["HAM"] == "火腿"
|
||||||
# Non existent file should not load
|
# Non existent file should not load
|
||||||
assert not load_dotenv("non-existent-file")
|
assert not load_dotenv("non-existent-file")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue