Skip to content

Commit

Permalink
Remove text file object support (#133)
Browse files Browse the repository at this point in the history
* Remove text file object support

* rm one line

* Changelog
  • Loading branch information
hukkin authored Nov 15, 2021
1 parent 6a93a19 commit 486c156
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Removed
- Python 3.6 support
- Support for text file objects as `load` input. Use binary file objects instead.

## 1.2.2

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ with open("path_to_file/conf.toml", "rb") as f:
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.

### Handle invalid TOML<a name="handle-invalid-toml"></a>

Expand Down
9 changes: 0 additions & 9 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from decimal import Decimal as D
from pathlib import Path

import pytest

import tomli


Expand All @@ -14,13 +12,6 @@ def test_load(tmp_path):
file_path = tmp_path / "test.toml"
file_path.write_text(content)

# Test text mode
with open(file_path, encoding="utf-8", newline="") as f:
with pytest.warns(DeprecationWarning):
actual = tomli.load(f) # type: ignore[arg-type]
assert actual == expected

# Test binary mode
with open(file_path, "rb") as bin_f:
actual = tomli.load(bin_f)
assert actual == expected
Expand Down
13 changes: 1 addition & 12 deletions tomli/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import string
from types import MappingProxyType
from typing import Any, BinaryIO, NamedTuple
import warnings

from tomli._re import (
RE_DATETIME,
Expand Down Expand Up @@ -53,17 +52,7 @@ class TOMLDecodeError(ValueError):

def load(fp: BinaryIO, *, parse_float: ParseFloat = float) -> dict[str, Any]:
"""Parse TOML from a binary file object."""
s_bytes = fp.read()
try:
s = s_bytes.decode()
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.',
DeprecationWarning,
stacklevel=2,
)
s = s_bytes # type: ignore[assignment]
s = fp.read().decode()
return loads(s, parse_float=parse_float)


Expand Down

0 comments on commit 486c156

Please sign in to comment.