diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2022-05-15 15:45:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-15 15:45:55 -0700 |
commit | 0d67ae1b7938ada3fe5df680b643d69290f0c099 (patch) | |
tree | 8fa5d3325837a27cf950dec47883c4cd6c3a6215 /coverage/tomlconfig.py | |
parent | e8973c5f50e9510c6ec7addf8fad70eceff6ab89 (diff) | |
download | python-coveragepy-git-0d67ae1b7938ada3fe5df680b643d69290f0c099.tar.gz |
Use tomllib on Python 3.11 (#1359)
Co-authored-by: hauntsaninja <>
Diffstat (limited to 'coverage/tomlconfig.py')
-rw-r--r-- | coverage/tomlconfig.py | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/coverage/tomlconfig.py b/coverage/tomlconfig.py index a06da65f..82f84ce9 100644 --- a/coverage/tomlconfig.py +++ b/coverage/tomlconfig.py @@ -6,16 +6,21 @@ import configparser import os import re +import sys from coverage.exceptions import ConfigError from coverage.misc import import_third_party, substitute_variables -# TOML support is an install-time extra option. (Import typing is here because -# import_third_party will unload any module that wasn't already imported. -# tomli imports typing, and if we unload it, later it's imported again, and on -# Python 3.6, this causes infinite recursion.) -import typing # pylint: disable=unused-import, wrong-import-order -tomli = import_third_party("tomli") +if sys.version_info >= (3, 11): + import tomllib +else: + # TOML support on Python 3.10 and below is an install-time extra option. + # (Import typing is here because import_third_party will unload any module + # that wasn't already imported. tomli imports typing, and if we unload it, + # later it's imported again, and on Python 3.6, this causes infinite + # recursion.) + import typing # pylint: disable=unused-import, wrong-import-order + tomllib = import_third_party("tomli") class TomlDecodeError(Exception): @@ -45,11 +50,11 @@ class TomlConfigParser: toml_text = fp.read() except OSError: return [] - if tomli is not None: + if tomllib is not None: toml_text = substitute_variables(toml_text, os.environ) try: - self.data = tomli.loads(toml_text) - except tomli.TOMLDecodeError as err: + self.data = tomllib.loads(toml_text) + except tomllib.TOMLDecodeError as err: raise TomlDecodeError(str(err)) from err return [filename] else: |