From c284fa431aef8b20dc18acb5a390345aa3e554c9 Mon Sep 17 00:00:00 2001 From: Ayan Sinha Mahapatra Date: Thu, 8 Jan 2026 13:08:50 +0530 Subject: [PATCH] Support setting the tarfile extract filter This is needed for python 3.14 support. Reference: https://github.com/aboutcode-org/commoncode/issues/88 Signed-off-by: Ayan Sinha Mahapatra --- CHANGELOG.rst | 6 ++++++ src/commoncode/archive.py | 8 ++++++-- src/commoncode/testcase.py | 8 ++++---- tests/data/filetype/types.tar | Bin 112640 -> 109056 bytes tests/test_filetype.py | 5 ++++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 25495220..dc638663 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Release notes ============= +Version 32.4.2 - (2025-01-08) +----------------------------- + +- Support setting the tar archive filter in python3.14 + https://github.com/aboutcode-org/commoncode/issues/88 + Version 32.4.1 - (2025-01-07) ----------------------------- diff --git a/src/commoncode/archive.py b/src/commoncode/archive.py index 3c0c7583..1d3e42cc 100644 --- a/src/commoncode/archive.py +++ b/src/commoncode/archive.py @@ -14,6 +14,7 @@ from os import path from commoncode.system import on_windows +from commoncode.system import py314 """ Mimimal tar and zip file handling, primarily for testing. @@ -39,7 +40,7 @@ def _extract_tar_raw(test_path, target_dir, to_bytes, *args, **kwargs): extract_tar_uni = partial(_extract_tar_raw, to_bytes=False) -def extract_tar(location, target_dir, verbatim=False, *args, **kwargs): +def extract_tar(location, target_dir, verbatim=False, filter=None, *args, **kwargs): """ Extract a tar archive at location in the target_dir directory. If `verbatim` is True preserve the permissions. @@ -58,7 +59,10 @@ def extract_tar(location, target_dir, verbatim=False, *args, **kwargs): if not verbatim: tarinfo.mode = 0o755 to_extract.append(tarinfo) - tar.extractall(target_dir, members=to_extract) + if py314 and filter: + tar.extractall(target_dir, members=to_extract, filter=filter) + else: + tar.extractall(target_dir, members=to_extract) finally: if tar: tar.close() diff --git a/src/commoncode/testcase.py b/src/commoncode/testcase.py index 525bf666..2a7b37a9 100644 --- a/src/commoncode/testcase.py +++ b/src/commoncode/testcase.py @@ -194,7 +194,7 @@ def remove_vcs(self, test_dir): for tf in tilde_files: os.remove(tf) - def __extract(self, test_path, extract_func=None, verbatim=False): + def __extract(self, test_path, extract_func=None, verbatim=False, filter=None): """ Given an archive file identified by test_path relative to a test files directory, return a new temp directory where the @@ -206,7 +206,7 @@ def __extract(self, test_path, extract_func=None, verbatim=False): target_path = path.basename(test_path) target_dir = self.get_temp_dir(target_path) original_archive = self.get_test_loc(test_path) - extract_func(original_archive, target_dir, verbatim=verbatim) + extract_func(original_archive, target_dir, verbatim=verbatim, filter=filter) return target_dir def extract_test_zip(self, test_path, *args, **kwargs): @@ -215,8 +215,8 @@ def extract_test_zip(self, test_path, *args, **kwargs): def extract_test_zip_raw(self, test_path, *args, **kwargs): return self.__extract(test_path, extract_zip_raw) - def extract_test_tar(self, test_path, verbatim=False): - return self.__extract(test_path, extract_tar, verbatim) + def extract_test_tar(self, test_path, verbatim=False, filter=None): + return self.__extract(test_path, extract_tar, verbatim, filter) def extract_test_tar_raw(self, test_path, *args, **kwargs): return self.__extract(test_path, extract_tar_raw) diff --git a/tests/data/filetype/types.tar b/tests/data/filetype/types.tar index 24fc04c44da43ac26ebb53a04feca6c8d1a04342..458dc4476efa3cb3b3507135af450adc925a1bfa 100644 GIT binary patch delta 1169 zcmZqpz}9eujnhCk$kjb0GQf4>!f0^=ATTpAVE|Hw=B8lUz|h2Wa`hbAM@K!$(-P{)czX9W1*7U_n#MubdU7>3)n z57O)=hGyhjx9Jm;-(;p^E#f8&h6V=a<|byQCI*HK28M=4<|YgZJ|Iu9Lp?E3&=H%9 zH#431$H>IMFq!E@3T}sWbiIafemMbq#<)tQ!Iwd zu$h<`Gbosnmxj2^blp7N{2_jb!W}0^fYz8BFesRjXAMWNZg7BOP%tn*#Ne`IdbI6vn5dshGF`G?Hi+a{_sf&Yw#d&tOSobOQ!3LCT^r#5HYqZcJEs062jc zaHB_ny(civYeYb8QfD)1lli^xd*2tmInldQI%%Z7)k^McskLMPfLh|_ZGL5kp#-f^ zASjOkqV}4MOY=CW-t_u@mviRd`t9CT`_hlA_1*7}^ttk4#v}`xvaf{Lq|>xCT)2)z zU>pkI=Kk`r67$A%%%2Xe36Vz`S<7Gg({SWEP4tQjO)8oh8GKvB#n?QL4B5OR>Wx;S(llqQHGn_B}XdAu|n zxMiV`am>aRjJB4~s~zunSyhTEoE?