diff --git a/README.md b/README.md index 89efb84..c1faec4 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ with open("path_to_file/conf.toml", "rb") as f: toml_dict = tomli.load(f) ``` -Opening the file in binary mode (with the `"rb"` flag) is highly encouraged. +The file must be opened in binary mode (with the `"rb"` flag). Binary mode will enforce decoding the file as UTF-8 with universal newlines disabled, both of which are required to correctly parse TOML. Support for text file objects is deprecated for removal in the next major release. diff --git a/tomli/_parser.py b/tomli/_parser.py index afc2121..7852eb8 100644 --- a/tomli/_parser.py +++ b/tomli/_parser.py @@ -63,11 +63,11 @@ class TOMLDecodeError(ValueError): def load(fp: IO, *, parse_float: ParseFloat = float) -> Dict[str, Any]: - """Parse TOML from a file object.""" + """Parse TOML from a binary file object.""" s = fp.read() - if isinstance(s, bytes): + try: s = s.decode() - else: + except AttributeError: warnings.warn( "Text file object support is deprecated in favor of binary file objects." ' Use `open("foo.toml", "rb")` to open the file in binary mode.',