Skip to content

Commit

Permalink
Merge pull request #143 from hukkin/tomllib
Browse files Browse the repository at this point in the history
Use tomllib on Python 3.11+
  • Loading branch information
takluyver authored Jun 5, 2022
2 parents a942316 + 3629d60 commit c45334a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Usage—you are responsible for ensuring build requirements are available:
from pep517.wrappers import Pep517HookCaller
src = 'path/to/source' # Folder containing 'pyproject.toml'
with open(os.path.join(src, 'pyproject.toml')) as f:
with open(os.path.join(src, 'pyproject.toml'), 'rb') as f:
build_sys = tomli.load(f)['build-system']
print(build_sys['requires']) # List of static requirements
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pytest
pytest-flake8
flake8 < 4 # https://github.com/tholo/pytest-flake8/issues/81
flake8
testpath
tomli
setuptools>=30
Expand Down
8 changes: 8 additions & 0 deletions pep517/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__all__ = ("tomllib",)

import sys

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
5 changes: 2 additions & 3 deletions pep517/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import shutil
import tempfile

import tomli

from ._compat import tomllib
from .envbuild import BuildEnvironment
from .wrappers import Pep517HookCaller

Expand All @@ -32,7 +31,7 @@ def load_system(source_dir):
"""
pyproject = os.path.join(source_dir, 'pyproject.toml')
with open(pyproject, 'rb') as f:
pyproject_data = tomli.load(f)
pyproject_data = tomllib.load(f)
return pyproject_data['build-system']


Expand Down
7 changes: 3 additions & 4 deletions pep517/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from subprocess import CalledProcessError
from tempfile import mkdtemp

import tomli

from ._compat import tomllib
from .colorlog import enable_colourful_output
from .envbuild import BuildEnvironment
from .wrappers import Pep517HookCaller
Expand Down Expand Up @@ -144,14 +143,14 @@ def check(source_dir):

try:
with open(pyproject, 'rb') as f:
pyproject_data = tomli.load(f)
pyproject_data = tomllib.load(f)
# Ensure the mandatory data can be loaded
buildsys = pyproject_data['build-system']
requires = buildsys['requires']
backend = buildsys['build-backend']
backend_path = buildsys.get('backend-path')
log.info('Loaded pyproject.toml')
except (tomli.TOMLDecodeError, KeyError):
except (tomllib.TOMLDecodeError, KeyError):
log.error("Invalid pyproject.toml", exc_info=True)
return False

Expand Down
5 changes: 2 additions & 3 deletions pep517/envbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from sysconfig import get_paths
from tempfile import mkdtemp

import tomli

from ._compat import tomllib
from .wrappers import LoggerWrapper, Pep517HookCaller

log = logging.getLogger(__name__)
Expand All @@ -21,7 +20,7 @@ def _load_pyproject(source_dir):
os.path.join(source_dir, 'pyproject.toml'),
'rb',
) as f:
pyproject_data = tomli.load(f)
pyproject_data = tomllib.load(f)
buildsys = pyproject_data['build-system']
return (
buildsys['requires'],
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ author-email = "thomas@kluyver.me.uk"
home-page = "https://github.com/pypa/pep517"
description-file = "README.rst"
requires = [
"tomli >=1.1.0",
"tomli >=1.1.0;python_version<'3.11'",
"importlib_metadata;python_version<'3.8'",
"zipp;python_version<'3.8'",
]
Expand Down

0 comments on commit c45334a

Please sign in to comment.