summaryrefslogtreecommitdiffstats
path: root/Doc/library/tomllib.rst
diff options
context:
space:
mode:
authorTaneli Hukkinen <3275109+hukkin@users.noreply.github.com>2022-03-08 08:26:13 (GMT)
committerGitHub <noreply@github.com>2022-03-08 08:26:13 (GMT)
commit591f6754b56cb7f6c31fce8c22528bdf0a99556c (patch)
tree8f3914aa35e86201d09e4abf48edbc784ac6176d /Doc/library/tomllib.rst
parent4d95fa1ac5d31ff450fb2f31b55ce1eb99d6efcb (diff)
downloadcpython-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 'Doc/library/tomllib.rst')
-rw-r--r--Doc/library/tomllib.rst117
1 files changed, 117 insertions, 0 deletions
diff --git a/Doc/library/tomllib.rst b/Doc/library/tomllib.rst
new file mode 100644
index 0000000..918576e
--- /dev/null
+++ b/Doc/library/tomllib.rst
@@ -0,0 +1,117 @@
+:mod:`tomllib` --- Parse TOML files
+===================================
+
+.. module:: tomllib
+ :synopsis: Parse TOML files.
+
+.. versionadded:: 3.11
+
+.. moduleauthor:: Taneli Hukkinen
+.. sectionauthor:: Taneli Hukkinen
+
+**Source code:** :source:`Lib/tomllib`
+
+--------------
+
+This module provides an interface for parsing TOML (Tom's Obvious Minimal
+Language, `https://toml.io <https://toml.io/en/>`_). This module does not
+support writing TOML.
+
+.. seealso::
+
+ The `Tomli-W package <https://pypi.org/project/tomli-w/>`__
+ is a TOML writer that can be used in conjunction with this module,
+ providing a write API familiar to users of the standard library
+ :mod:`marshal` and :mod:`pickle` modules.
+
+.. seealso::
+
+ The `TOML Kit package <https://pypi.org/project/tomlkit/>`__
+ is a style-preserving TOML library with both read and write capability.
+ It is a recommended replacement for this module for editing already
+ existing TOML files.
+
+
+This module defines the following functions:
+
+.. function:: load(fp, /, *, parse_float=float)
+
+ Read a TOML file. The first argument should be a readable and binary file object.
+ Return a :class:`dict`. Convert TOML types to Python using this
+ :ref:`conversion table <toml-to-py-table>`.
+
+ *parse_float* will be called with the string of every TOML
+ float to be decoded. By default, this is equivalent to ``float(num_str)``.
+ This can be used to use another datatype or parser for TOML floats
+ (e.g. :class:`decimal.Decimal`). The callable must not return a
+ :class:`dict` or a :class:`list`, else a :exc:`ValueError` is raised.
+
+ A :exc:`TOMLDecodeError` will be raised on an invalid TOML document.
+
+
+.. function:: loads(s, /, *, parse_float=float)
+
+ Load TOML from a :class:`str` object. Return a :class:`dict`. Convert TOML
+ types to Python using this :ref:`conversion table <toml-to-py-table>`. The
+ *parse_float* argument has the same meaning as in :func:`load`.
+
+ A :exc:`TOMLDecodeError` will be raised on an invalid TOML document.
+
+
+The following exceptions are available:
+
+.. exception:: TOMLDecodeError
+
+ Subclass of :exc:`ValueError`.
+
+
+Examples
+--------
+
+Parsing a TOML file::
+
+ import tomllib
+
+ with open("pyproject.toml", "rb") as f:
+ data = tomllib.load(f)
+
+Parsing a TOML string::
+
+ import tomllib
+
+ toml_str = """
+ python-version = "3.11.0"
+ python-implementation = "CPython"
+ """
+
+ data = tomllib.loads(toml_str)
+
+
+Conversion Table
+----------------
+
+.. _toml-to-py-table:
+
++------------------+--------------------------------------------------------------------------------------+
+| TOML | Python |
++==================+======================================================================================+
+| table | dict |
++------------------+--------------------------------------------------------------------------------------+
+| string | str |
++------------------+--------------------------------------------------------------------------------------+
+| integer | int |
++------------------+--------------------------------------------------------------------------------------+
+| float | float (configurable with *parse_float*) |
++------------------+--------------------------------------------------------------------------------------+
+| boolean | bool |
++------------------+--------------------------------------------------------------------------------------+
+| offset date-time | datetime.datetime (``tzinfo`` attribute set to an instance of ``datetime.timezone``) |
++------------------+--------------------------------------------------------------------------------------+
+| local date-time | datetime.datetime (``tzinfo`` attribute set to ``None``) |
++------------------+--------------------------------------------------------------------------------------+
+| local date | datetime.date |
++------------------+--------------------------------------------------------------------------------------+
+| local time | datetime.time |
++------------------+--------------------------------------------------------------------------------------+
+| array | list |
++------------------+--------------------------------------------------------------------------------------+