diff --git a/src/dotenv/parser.py b/src/dotenv/parser.py index eb100b47..66773604 100644 --- a/src/dotenv/parser.py +++ b/src/dotenv/parser.py @@ -68,7 +68,7 @@ class Error(Exception): class Reader: def __init__(self, stream: IO[str]) -> None: - self.string = stream.read() + self.string = stream.read().removeprefix("\ufeff") self.position = Position.start() self.mark = Position.start() diff --git a/tests/test_parser.py b/tests/test_parser.py index 43386e5a..4ec5a5af 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -545,6 +545,35 @@ ), ], ), + # UTF-8 BOM at the start of the file should be stripped + ( + "\ufeffa=b", + [ + Binding( + key="a", + value="b", + original=Original(string="a=b", line=1), + error=False, + ) + ], + ), + ( + "\ufeffa=b\nc=d", + [ + Binding( + key="a", + value="b", + original=Original(string="a=b\n", line=1), + error=False, + ), + Binding( + key="c", + value="d", + original=Original(string="c=d", line=2), + error=False, + ), + ], + ), ], ) def test_parse_stream(test_input, expected):