From 07577de8ad2c6c7175a435b4881ff457d830c456 Mon Sep 17 00:00:00 2001 From: Miro Hrončok Date: Jun 02 2022 09:34:14 +0000 Subject: Use tomllib from the standard library on Python 3.11+ --- diff --git a/macros.pyproject b/macros.pyproject index a3549c5..bf412f7 100644 --- a/macros.pyproject +++ b/macros.pyproject @@ -143,7 +143,13 @@ echo 'python%{python3_pkgversion}-devel' echo 'python%{python3_pkgversion}dist(pip) >= 19' echo 'python%{python3_pkgversion}dist(packaging)' %{!-N:if [ -f pyproject.toml ]; then - echo 'python%{python3_pkgversion}dist(toml)' + %["%{python3_pkgversion}" == "3" + ? "echo '(python%{python3_pkgversion}dist(toml) if python%{python3_pkgversion}-devel < 3.11)'" + : "%[v"%{python3_pkgversion}" < v"3.11" + ? "echo 'python%{python3_pkgversion}dist(toml)'" + : "true # will use tomllib, echo nothing" + ]" + ] elif [ -f setup.py ]; then # Note: If the default requirements change, also change them in the script! echo 'python%{python3_pkgversion}dist(setuptools) >= 40.8' diff --git a/pyproject-rpm-macros.spec b/pyproject-rpm-macros.spec index d20e254..1a8364a 100644 --- a/pyproject-rpm-macros.spec +++ b/pyproject-rpm-macros.spec @@ -10,7 +10,7 @@ License: MIT # Increment Y and reset Z when new macros or features are added # Increment Z when this is a bugfix or a cosmetic change # Dropping support for EOL Fedoras is *not* considered a breaking change -Version: 1.2.0 +Version: 1.3.0 Release: 1%{?dist} # Macro files @@ -50,9 +50,9 @@ BuildRequires: python3dist(pyyaml) BuildRequires: python3dist(packaging) BuildRequires: python3dist(pip) BuildRequires: python3dist(setuptools) -BuildRequires: python3dist(toml) BuildRequires: python3dist(tox-current-env) >= 0.0.6 BuildRequires: python3dist(wheel) +BuildRequires: (python3dist(toml) if python3-devel < 3.11) %endif # We build on top of those: @@ -124,6 +124,9 @@ export HOSTNAME="rpmbuild" # to speedup tox in network-less mock, see rhbz#1856 %license LICENSE %changelog +* Thu May 12 2022 Miro Hrončok - 1.3.0-1 +- Use tomllib from the standard library on Python 3.11+ + * Wed Apr 27 2022 Miro Hrončok - 1.2.0-1 - %%pyproject_buildrequires: Add provisional -w flag for build backends without prepare_metadata_for_build_wheel hook diff --git a/pyproject_buildrequires.py b/pyproject_buildrequires.py index d1f8d81..e604de5 100644 --- a/pyproject_buildrequires.py +++ b/pyproject_buildrequires.py @@ -187,21 +187,32 @@ class Requirements: self.add(req_str, **kwargs) -def get_backend(requirements): +def toml_load(opened_binary_file): try: - f = open('pyproject.toml') - except FileNotFoundError: - pyproject_data = {} - else: + # tomllib is in the standard library since 3.11.0b1 + import tomllib as toml_module + load_from = opened_binary_file + except ImportError: try: - # lazy import toml here, not needed without pyproject.toml - import toml + # note: we could use tomli here, + # but for backwards compatibility with RHEL 9, we use toml instead + import toml as toml_module + load_from = io.TextIOWrapper(opened_binary_file, encoding='utf-8') except ImportError as e: print_err('Import error:', e) # already echoed by the %pyproject_buildrequires macro sys.exit(0) + return toml_module.load(load_from) + + +def get_backend(requirements): + try: + f = open('pyproject.toml', 'rb') + except FileNotFoundError: + pyproject_data = {} + else: with f: - pyproject_data = toml.load(f) + pyproject_data = toml_load(f) buildsystem_data = pyproject_data.get('build-system', {}) requirements.extend(