Use tomllib on Python 3.11+

Fallback to tomli for compatibility with older Python.
This commit is contained in:
Felix Yan 2023-05-08 10:52:27 +03:00
parent 8cd75fd101
commit 34e87db8f9
No known key found for this signature in database
GPG Key ID: 786C63F330D7CB92
6 changed files with 25 additions and 12 deletions

View File

@ -24,7 +24,7 @@ This is the version 2.0 branch. For the old version 1.x, please switch to the ``
Dependency Dependency
---------- ----------
- Python 3.7+ - Python 3.7+
- Python library: structlog, tomli, platformdirs - Python library: structlog, platformdirs, tomli (on Python < 3.11)
- One of these Python library combinations (ordered by preference): - One of these Python library combinations (ordered by preference):
* tornado + pycurl * tornado + pycurl

View File

@ -18,7 +18,7 @@ This is the version 2.0 branch. For the old version 1.x, please switch to the ``
Dependency Dependency
---------- ----------
- Python 3.7+ - Python 3.7+
- Python library: structlog, tomli, platformdirs - Python library: structlog, platformdirs, tomli (on Python < 3.11)
- One of these Python library combinations (ordered by preference): - One of these Python library combinations (ordered by preference):
* tornado + pycurl * tornado + pycurl

View File

@ -21,7 +21,12 @@ import contextvars
import json import json
import structlog import structlog
import tomli
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import platformdirs import platformdirs
from .lib import nicelogger from .lib import nicelogger
@ -157,8 +162,8 @@ def load_file(
) -> Tuple[Entries, Options]: ) -> Tuple[Entries, Options]:
try: try:
with open(file, 'rb') as f: with open(file, 'rb') as f:
config = tomli.load(f) config = tomllib.load(f)
except (OSError, tomli.TOMLDecodeError) as e: except (OSError, tomllib.TOMLDecodeError) as e:
raise FileLoadError('version configuration file', file, e) raise FileLoadError('version configuration file', file, e)
ver_files: Optional[Tuple[Path, Path]] = None ver_files: Optional[Tuple[Path, Path]] = None

View File

@ -14,7 +14,11 @@ from pathlib import Path
import contextvars import contextvars
import abc import abc
import tomli try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import structlog import structlog
from .httpclient import session from .httpclient import session
@ -56,8 +60,8 @@ class KeyManager:
if file is not None: if file is not None:
try: try:
with file.open('rb') as f: with file.open('rb') as f:
keys = tomli.load(f)['keys'] keys = tomllib.load(f)['keys']
except (OSError, tomli.TOMLDecodeError) as e: except (OSError, tomllib.TOMLDecodeError) as e:
raise FileLoadError('keyfile', str(file), e) raise FileLoadError('keyfile', str(file), e)
else: else:
keys = {} keys = {}

View File

@ -41,7 +41,7 @@ zip_safe = True
packages = find_namespace: packages = find_namespace:
install_requires = install_requires =
setuptools; python_version<"3.8" setuptools; python_version<"3.8"
tomli tomli; python_version<"3.11"
structlog structlog
platformdirs platformdirs
tornado>=6 tornado>=6

View File

@ -6,7 +6,11 @@ import structlog
import os import os
from pathlib import Path from pathlib import Path
import tomli try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import pytest import pytest
import pytest_asyncio import pytest_asyncio
@ -54,7 +58,7 @@ async def get_version():
@pytest_asyncio.fixture(scope="module") @pytest_asyncio.fixture(scope="module")
async def run_str(): async def run_str():
async def __call__(str): async def __call__(str):
entries = tomli.loads(str) entries = tomllib.loads(str)
newvers = await run(entries) newvers = await run(entries)
return newvers.popitem()[1] return newvers.popitem()[1]
@ -63,7 +67,7 @@ async def run_str():
@pytest_asyncio.fixture(scope="module") @pytest_asyncio.fixture(scope="module")
async def run_str_multi(): async def run_str_multi():
async def __call__(str): async def __call__(str):
entries = tomli.loads(str) entries = tomllib.loads(str)
newvers = await run(entries) newvers = await run(entries)
return newvers return newvers