From 033577061ca4d579d9c650ec7ea91d99d0ecf022 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Thu, 29 Jul 2021 21:16:06 +0300 Subject: [PATCH] Deprecate text file objects as input to `load` (#106) --- .github/workflows/tests.yaml | 11 +++++------ CHANGELOG.md | 6 ++++++ README.md | 2 +- fuzzer/requirements.txt | 2 +- tomli/_parser.py | 7 +++++++ 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 016774a..e7e488b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -3,8 +3,7 @@ name: Tests on: push: branches: [ master ] - tags: - - '[0-9]+.[0-9]+.[0-9]+*' + tags: [ '[0-9]+.[0-9]+.[0-9]+*' ] pull_request: branches: [ master ] @@ -17,7 +16,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 with: - python-version: 3.8 + python-version: '3.8' - name: Install pre-commit run: | @@ -33,7 +32,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - python-version: [pypy-3.6, 3.6, 3.7, pypy-3.7, 3.8, 3.9, 3.10-dev] + python-version: ['pypy-3.6', 'pypy-3.7', '3.6', '3.7', '3.8', '3.9', '3.10-dev'] os: [ubuntu-latest, macos-latest, windows-latest] continue-on-error: ${{ matrix.python-version == '3.10-dev' }} @@ -69,7 +68,7 @@ jobs: - uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: '3.7' - name: Install (deps and package) run: | @@ -97,7 +96,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: '3.7' - name: Install Flit run: | pip install "flit==3.2.0" diff --git a/CHANGELOG.md b/CHANGELOG.md index 21158e7..dc7de96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.2.0 + +- Deprecated + - Text file objects as input to `load`. + Binary file objects should be used instead to avoid opening a TOML file with universal newlines or with an encoding other than UTF-8. + ## 1.1.0 - Added diff --git a/README.md b/README.md index a2b3214..89efb84 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ with open("path_to_file/conf.toml", "rb") as f: Opening the file in binary mode (with the `"rb"` flag) is highly encouraged. 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 may be deprecated for removal in a future release. +Support for text file objects is deprecated for removal in the next major release. ### Handle invalid TOML diff --git a/fuzzer/requirements.txt b/fuzzer/requirements.txt index 7ac285b..b4d78cc 100644 --- a/fuzzer/requirements.txt +++ b/fuzzer/requirements.txt @@ -1,4 +1,4 @@ # sudo apt-get install clang wheel -atheris==2.0.0 +atheris==2.0.3 tomli_w>=0.2.2 diff --git a/tomli/_parser.py b/tomli/_parser.py index 3e46c04..410ed7b 100644 --- a/tomli/_parser.py +++ b/tomli/_parser.py @@ -11,6 +11,7 @@ Optional, Tuple, ) +import warnings from tomli._re import ( RE_DATETIME, @@ -66,6 +67,12 @@ def load(fp: IO, *, parse_float: ParseFloat = float) -> Dict[str, Any]: s = fp.read() if isinstance(s, bytes): s = s.decode() + else: + 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, + ) return loads(s, parse_float=parse_float)