From 07598e61d1cc6ebc7b6957a023540b03a16244f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Thu, 2 Jun 2022 09:34:14 +0000 Subject: [PATCH] Use tomllib from the standard library on Python 3.11+ Related: rhbz#2117571 --- macros.pyproject | 8 +++++++- pyproject-rpm-macros.spec | 7 +++++-- pyproject_buildrequires.py | 27 +++++++++++++++++++-------- 3 files changed, 31 insertions(+), 11 deletions(-) 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 11aa50f..09bf0f8 100644 --- a/pyproject-rpm-macros.spec +++ b/pyproject-rpm-macros.spec @@ -12,7 +12,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 @@ -52,9 +52,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: @@ -126,6 +126,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(