mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	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`
This commit is contained in:
		
							parent
							
								
									4d95fa1ac5
								
							
						
					
					
						commit
						591f6754b5
					
				
					 90 changed files with 1479 additions and 1 deletions
				
			
		
							
								
								
									
										57
									
								
								Lib/test/test_tomllib/test_error.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								Lib/test/test_tomllib/test_error.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -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" | ||||
|             ) | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Taneli Hukkinen
						Taneli Hukkinen