diff options
author | Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> | 2022-03-08 08:26:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-08 08:26:13 (GMT) |
commit | 591f6754b56cb7f6c31fce8c22528bdf0a99556c (patch) | |
tree | 8f3914aa35e86201d09e4abf48edbc784ac6176d /Lib/test/test_tomllib/test_error.py | |
parent | 4d95fa1ac5d31ff450fb2f31b55ce1eb99d6efcb (diff) | |
download | cpython-591f6754b56cb7f6c31fce8c22528bdf0a99556c.zip cpython-591f6754b56cb7f6c31fce8c22528bdf0a99556c.tar.gz cpython-591f6754b56cb7f6c31fce8c22528bdf0a99556c.tar.bz2 |
bpo-40059: Add tomllib (PEP-680) (GH-31498)
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`
Diffstat (limited to 'Lib/test/test_tomllib/test_error.py')
-rw-r--r-- | Lib/test/test_tomllib/test_error.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Lib/test/test_tomllib/test_error.py b/Lib/test/test_tomllib/test_error.py new file mode 100644 index 0000000..7244626 --- /dev/null +++ b/Lib/test/test_tomllib/test_error.py @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: 2021 Taneli Hukkinen +# Licensed to PSF under a Contributor Agreement. + +import unittest + +from . import tomllib + + +class TestError(unittest.TestCase): + def test_line_and_col(self): + with self.assertRaises(tomllib.TOMLDecodeError) as exc_info: + tomllib.loads("val=.") + self.assertEqual(str(exc_info.exception), "Invalid value (at line 1, column 5)") + + with self.assertRaises(tomllib.TOMLDecodeError) as exc_info: + tomllib.loads(".") + self.assertEqual( + str(exc_info.exception), "Invalid statement (at line 1, column 1)" + ) + + with self.assertRaises(tomllib.TOMLDecodeError) as exc_info: + tomllib.loads("\n\nval=.") + self.assertEqual(str(exc_info.exception), "Invalid value (at line 3, column 5)") + + with self.assertRaises(tomllib.TOMLDecodeError) as exc_info: + tomllib.loads("\n\n.") + self.assertEqual( + str(exc_info.exception), "Invalid statement (at line 3, column 1)" + ) + + def test_missing_value(self): + with self.assertRaises(tomllib.TOMLDecodeError) as exc_info: + tomllib.loads("\n\nfwfw=") + self.assertEqual(str(exc_info.exception), "Invalid value (at end of document)") + + def test_invalid_char_quotes(self): + with self.assertRaises(tomllib.TOMLDecodeError) as exc_info: + tomllib.loads("v = '\n'") + self.assertTrue(" '\\n' " in str(exc_info.exception)) + + def test_module_name(self): + self.assertEqual(tomllib.TOMLDecodeError().__module__, tomllib.__name__) + + def test_invalid_parse_float(self): + def dict_returner(s: str) -> dict: + return {} + + def list_returner(s: str) -> list: + return [] + + for invalid_parse_float in (dict_returner, list_returner): + with self.assertRaises(ValueError) as exc_info: + tomllib.loads("f=0.1", parse_float=invalid_parse_float) + self.assertEqual( + str(exc_info.exception), "parse_float must not return dicts or lists" + ) |