From 2c37d1b374719d67a6ccab351de575a76b8f70e6 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Fri, 6 Nov 2026 17:40:08 -0500 Subject: [PATCH 1/3] Do not import __future__ in the generated version file The default version file template of setuptools-scm 10, which isolated builds now get, starts with "from __future__ import annotations". __future__ is not a frozen module, so every "import isal" loaded it from disk unless another module had done so first. isal only uses __version__, and _version.pyi declares nothing else, so a one-line template now replaces the default. Under cachegrind, "import isal" runs 576K fewer instructions on Python 3.11 and 653K fewer on 3.14, about a third of what it costs on top of interpreter startup. Timed in the process on Linux, "import isal.isal_zlib" takes 0.13 ms less on 3.11 (776 to 646 us) and 0.21 ms less on 3.14 (944 to 736 us). Processes that have already imported __future__ see no change. isal._version no longer has version, version_tuple, commit_id and their dunder forms. Nothing in isal uses them. --- CHANGELOG.rst | 3 +++ pyproject.toml | 2 ++ tests/test_version.py | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/test_version.py diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 481b7c5f..c6cdd3d1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,9 @@ Changelog version 1.10.0-dev ------------------ ++ Speed up ``import isal`` by about 0.1 ms in programs that have not imported + ``__future__`` yet. The version module that is generated when isal is built + now only sets ``__version__``. + Fix the gzip header that ``igzip_threaded.open`` writes when ``threads`` is not 0. It had the XFL and OS bytes swapped, so the ``file`` command reported the output as coming from VM/CMS or a FAT file system. diff --git a/pyproject.toml b/pyproject.toml index d1bbfb46..801d988d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,8 @@ urls.documentation = "https://python-isal.readthedocs.io" [tool.setuptools_scm] version_file = "src/isal/_version.py" +# The default template imports __future__, which slows down "import isal". +version_file_template = "__version__ = {version!r}\n" [tool.setuptools.packages.find] where = ["src"] diff --git a/tests/test_version.py b/tests/test_version.py new file mode 100644 index 00000000..64c75b66 --- /dev/null +++ b/tests/test_version.py @@ -0,0 +1,21 @@ +"""Check the version module that setuptools-scm writes.""" +import subprocess +import sys +from importlib import metadata + +import isal + + +def test_version_matches_package_metadata(): + assert isal.__version__ == metadata.version("isal") + + +def test_version_does_not_import_future(): + code = ("import sys; before = set(sys.modules); " + "from isal import __version__; " + "print(*(set(sys.modules) - before))") + result = subprocess.run([sys.executable, "-c", code], check=True, + capture_output=True, text=True) + new_modules = set(result.stdout.split()) + assert "isal._version" in new_modules + assert "__future__" not in new_modules From fc244599be513a3b28724dd33534486f2eb93fb6 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Tue, 17 Nov 2026 20:47:13 -0500 Subject: [PATCH 2/3] Keep the variables of the default version file Downstream code can use version_tuple and commit_id from isal._version, for example to check for a feature. The template is now the default template of setuptools-scm 10 without "from __future__ import annotations", so the version file keeps version, version_tuple, commit_id and their dunder forms. Python 3.10 and later can evaluate its annotations without the import, and Python 3.14 does not evaluate them on import. The commit_id line needs scm_version.short_node, which setuptools-scm has had since 9.2.0. Older versions fail the build with an AttributeError, so the build now requires setuptools-scm 9.2 or later. _version.pyi declares all the variables, so type checkers accept them too. Timed in the process on Python 3.14, as the median of 300 interleaved runs, "import isal.isal_zlib" takes 267 us. It takes 327 us with the default template, and 262 us when the version file only sets __version__. --- CHANGELOG.rst | 3 ++- pyproject.toml | 32 +++++++++++++++++++++++++++++--- src/isal/_version.pyi | 5 +++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 216699fb..136392db 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,7 +11,8 @@ version 1.10.0-dev ------------------ + Speed up ``import isal`` by about 0.1 ms in programs that have not imported ``__future__`` yet. The version module that is generated when isal is built - now only sets ``__version__``. + no longer imports ``__future__``. Building isal now requires setuptools-scm + 9.2 or later. + Fix ``igzip_threaded`` files that stopped working when the thread that opened them ended, for example a ``ThreadPoolExecutor`` thread after ``shutdown()``. Reads returned truncated data without an error, and writes diff --git a/pyproject.toml b/pyproject.toml index 801d988d..8cb0e2c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["setuptools>=77", "setuptools-scm>=8"] +requires = ["setuptools>=77", "setuptools-scm>=9.2"] build-backend = "setuptools.build_meta" [project] @@ -36,8 +36,34 @@ urls.documentation = "https://python-isal.readthedocs.io" [tool.setuptools_scm] version_file = "src/isal/_version.py" -# The default template imports __future__, which slows down "import isal". -version_file_template = "__version__ = {version!r}\n" +# The default template of setuptools-scm 10 without its +# "from __future__ import annotations", which slows down "import isal". +# scm_version.short_node needs setuptools-scm 9.2 or later. +version_file_template = ''' +# file generated by setuptools-scm +# don't change, don't track in version control + +__all__ = [ + "__version__", + "__version_tuple__", + "version", + "version_tuple", + "__commit_id__", + "commit_id", +] + +version: str +__version__: str +__version_tuple__: tuple[int | str, ...] +version_tuple: tuple[int | str, ...] +commit_id: str | None +__commit_id__: str | None + +__version__ = version = {version!r} +__version_tuple__ = version_tuple = {version_tuple!r} + +__commit_id__ = commit_id = {scm_version.short_node!r} +''' [tool.setuptools.packages.find] where = ["src"] diff --git a/src/isal/_version.pyi b/src/isal/_version.pyi index e59e7032..31f7d606 100644 --- a/src/isal/_version.pyi +++ b/src/isal/_version.pyi @@ -5,4 +5,9 @@ # This file is part of python-isal which is distributed under the # PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2. +version: str __version__: str +__version_tuple__: tuple[int | str, ...] +version_tuple: tuple[int | str, ...] +commit_id: str | None +__commit_id__: str | None From 2f6552f1da788c8a801c6ad277588e92b75d728a Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Tue, 17 Nov 2026 20:47:13 -0500 Subject: [PATCH 3/3] Remove the test that __future__ is not imported Testing this detail of the version file template is not needed. --- tests/test_version.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/test_version.py b/tests/test_version.py index 64c75b66..a3af6383 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -1,6 +1,4 @@ """Check the version module that setuptools-scm writes.""" -import subprocess -import sys from importlib import metadata import isal @@ -8,14 +6,3 @@ def test_version_matches_package_metadata(): assert isal.__version__ == metadata.version("isal") - - -def test_version_does_not_import_future(): - code = ("import sys; before = set(sys.modules); " - "from isal import __version__; " - "print(*(set(sys.modules) - before))") - result = subprocess.run([sys.executable, "-c", code], check=True, - capture_output=True, text=True) - new_modules = set(result.stdout.split()) - assert "isal._version" in new_modules - assert "__future__" not in new_modules