mirror of
https://github.com/python/cpython.git
synced 2025-07-16 07:45:20 +00:00

This adds a new standard library module, `tomllib`, for parsing TOML. The implementation is based on Tomli (https://github.com/hukkin/tomli). ## Steps taken (converting `tomli` to `tomllib`) - Move everything in `tomli:src/tomli` to `Lib/tomllib`. Exclude `py.typed`. - Remove `__version__ = ...` line from `Lib/tomllib/__init__.py` - Move everything in `tomli:tests` to `Lib/test/test_tomllib`. Exclude the following test data dirs recursively: - `tomli:tests/data/invalid/_external/` - `tomli:tests/data/valid/_external/` - Create `Lib/test/test_tomllib/__main__.py`: ```python import unittest from . import load_tests unittest.main() ``` - Add the following to `Lib/test/test_tomllib/__init__.py`: ```python import os from test.support import load_package_tests def load_tests(*args): return load_package_tests(os.path.dirname(__file__), *args) ``` Also change `import tomli as tomllib` to `import tomllib`. - In `cpython/Lib/tomllib/_parser.py` replace `__fp` with `fp` and `__s` with `s`. Add the `/` to `load` and `loads` function signatures. - Run `make regen-stdlib-module-names` - Create `Doc/library/tomllib.rst` and reference it in `Doc/library/fileformats.rst`
64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
# SPDX-License-Identifier: MIT
|
|
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
|
|
# Licensed to PSF under a Contributor Agreement.
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import unittest
|
|
|
|
from . import burntsushi, tomllib
|
|
|
|
|
|
class MissingFile:
|
|
def __init__(self, path: Path):
|
|
self.path = path
|
|
|
|
|
|
DATA_DIR = Path(__file__).parent / "data"
|
|
|
|
VALID_FILES = tuple((DATA_DIR / "valid").glob("**/*.toml"))
|
|
assert VALID_FILES, "Valid TOML test files not found"
|
|
|
|
_expected_files = []
|
|
for p in VALID_FILES:
|
|
json_path = p.with_suffix(".json")
|
|
try:
|
|
text = json.loads(json_path.read_bytes().decode())
|
|
except FileNotFoundError:
|
|
text = MissingFile(json_path)
|
|
_expected_files.append(text)
|
|
VALID_FILES_EXPECTED = tuple(_expected_files)
|
|
|
|
INVALID_FILES = tuple((DATA_DIR / "invalid").glob("**/*.toml"))
|
|
assert INVALID_FILES, "Invalid TOML test files not found"
|
|
|
|
|
|
class TestData(unittest.TestCase):
|
|
def test_invalid(self):
|
|
for invalid in INVALID_FILES:
|
|
with self.subTest(msg=invalid.stem):
|
|
toml_bytes = invalid.read_bytes()
|
|
try:
|
|
toml_str = toml_bytes.decode()
|
|
except UnicodeDecodeError:
|
|
# Some BurntSushi tests are not valid UTF-8. Skip those.
|
|
continue
|
|
with self.assertRaises(tomllib.TOMLDecodeError):
|
|
tomllib.loads(toml_str)
|
|
|
|
def test_valid(self):
|
|
for valid, expected in zip(VALID_FILES, VALID_FILES_EXPECTED):
|
|
with self.subTest(msg=valid.stem):
|
|
if isinstance(expected, MissingFile):
|
|
# For a poor man's xfail, assert that this is one of the
|
|
# test cases where expected data is known to be missing.
|
|
assert valid.stem in {
|
|
"qa-array-inline-nested-1000",
|
|
"qa-table-inline-nested-1000",
|
|
}
|
|
continue
|
|
toml_str = valid.read_bytes().decode()
|
|
actual = tomllib.loads(toml_str)
|
|
actual = burntsushi.convert(actual)
|
|
expected = burntsushi.normalize(expected)
|
|
self.assertEqual(actual, expected)
|