diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 068aa13ed70356..1fd77786176645 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1616,14 +1616,19 @@ def _apply_pax_info(self, pax_headers, encoding, errors): """Replace fields with supplemental information from a previous pax extended or global header. """ + real_size_updated = False for keyword, value in pax_headers.items(): if keyword == "GNU.sparse.name": setattr(self, "path", value) elif keyword == "GNU.sparse.size": setattr(self, "size", int(value)) + real_size_updated = True elif keyword == "GNU.sparse.realsize": setattr(self, "size", int(value)) + real_size_updated = True elif keyword in PAX_FIELDS: + if keyword == "size" and real_size_updated: + continue if keyword in PAX_NUMBER_FIELDS: try: value = PAX_NUMBER_FIELDS[keyword](value) diff --git a/Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst b/Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst new file mode 100644 index 00000000000000..23080e4c70053f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-13-12-37-49.gh-issue-136601.ZBiMIN.rst @@ -0,0 +1 @@ +Always set ``TarInfo.size`` to the real file size for sparse files.