From 88ba93b9d9e3bc79a983fec9d04c20a773e504ed Mon Sep 17 00:00:00 2001 From: "Lumberbot (aka Jack)" <39504233+meeseeksmachine@users.noreply.github.com> Date: Thu, 30 May 2024 20:24:04 +0200 Subject: [PATCH] feat(backport): Use Python 3.12+ tarfile data extraction filter (#2500) * Backport PR https://github.com/scikit-hep/pyhf/pull/2455 * In Python 3.12 extraction filters are added and will become default in Python 3.14. To start using them for when Python 3.12 support is added, and to guard against a Python 3.14 DeprecationWarning, use the data extraction filter for extracting tarfiles in pyhf.contrib.utils.download. - c.f. https://docs.python.org/3.12/library/tarfile.html#extraction-filters Co-authored-by: Matthew Feickert --- src/pyhf/contrib/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pyhf/contrib/utils.py b/src/pyhf/contrib/utils.py index 079bc55db9..6e1a0784e7 100644 --- a/src/pyhf/contrib/utils.py +++ b/src/pyhf/contrib/utils.py @@ -91,7 +91,12 @@ def download(archive_url, output_directory, force=False, compress=False): with tarfile.open( mode="r:*", fileobj=BytesIO(response.content) ) as archive: - archive.extractall(output_directory) + # TODO: Simplify after pyhf is Python 3.12+ only + # c.f. https://docs.python.org/3.12/library/tarfile.html#extraction-filters + if hasattr(tarfile, "data_filter"): + archive.extractall(output_directory, filter="data") + else: + archive.extractall(output_directory) except tarfile.ReadError: if not zipfile.is_zipfile(BytesIO(response.content)): raise exceptions.InvalidArchive(