Skip to content

Commit

Permalink
Use standard library tomllib in Python >=3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Oct 16, 2022
1 parent 6e583bb commit 67a7134
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ classifiers = [
"Operating System :: Microsoft :: Windows",
"Operating System :: MacOS",
]
dependencies = ["distlib~=0.3.5", "flit~=3.7"]
dependencies = [
"distlib~=0.3.5",
"flit~=3.7",
"tomli~=1.1.0; python_version < '3.11'",
]
dynamic = ["version", "description"]

[project.urls]
Expand All @@ -34,4 +38,4 @@ Tracker = "https://github.com/tttapa/py-build-cmake/issues"
name = "py_build_cmake"

[tool.pytest.ini_options]
testpaths = ["test"]
testpaths = ["test"]
14 changes: 9 additions & 5 deletions src/py_build_cmake/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass, field
import re
import warnings
import tomli
from typing import Any, Dict, List, Optional, Set
from pathlib import Path
from flit_core.config import ConfigError, read_pep621_metadata
Expand All @@ -11,6 +10,11 @@
from .config_options import ConfigNode, OverrideConfigOption
from .pyproject_options import get_options, get_cross_path, get_tool_pbc_path

try:
import tomllib as toml_
except ImportError:
import tomli as toml_


@dataclass
class Config:
Expand All @@ -31,7 +35,7 @@ def read_metadata(pyproject_path, flag_overrides: Dict[str,
# Load the pyproject.toml file
pyproject_path = Path(pyproject_path)
pyproject_folder = pyproject_path.parent
pyproject = tomli.loads(pyproject_path.read_text('utf-8'))
pyproject = toml_.loads(pyproject_path.read_text('utf-8'))
if 'project' not in pyproject:
raise ConfigError('Missing [project] table')

Expand All @@ -40,7 +44,7 @@ def read_metadata(pyproject_path, flag_overrides: Dict[str,
localconfig_path = pyproject_folder / localconfig_fname
localconfig = None
if localconfig_path.exists():
localconfig = tomli.loads(localconfig_path.read_text('utf-8'))
localconfig = toml_.loads(localconfig_path.read_text('utf-8'))
# treat empty local override as no local override
localconfig = localconfig or None

Expand All @@ -49,7 +53,7 @@ def read_metadata(pyproject_path, flag_overrides: Dict[str,
crossconfig_path = pyproject_folder / crossconfig_fname
crossconfig = None
if crossconfig_path.exists():
crossconfig = tomli.loads(crossconfig_path.read_text('utf-8'))
crossconfig = toml_.loads(crossconfig_path.read_text('utf-8'))
crossconfig = crossconfig or None

# File names mapping to the actual dict with the config
Expand All @@ -64,7 +68,7 @@ def read_metadata(pyproject_path, flag_overrides: Dict[str,
def try_load_local(path: Path):
if not path.exists():
raise FileNotFoundError(path.absolute())
return tomli.loads(path.read_text('utf-8'))
return toml_.loads(path.read_text('utf-8'))

extra_flag_paths = {
'--local': get_tool_pbc_path(),
Expand Down

0 comments on commit 67a7134

Please sign in to comment.