From b572934bdc9bfc84bbb3ec5b8d16a62603b39fb3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Tue, 16 Apr 2024 07:07:28 -0400 Subject: [PATCH] Add compatibility shim for removesuffix. --- backports/tarfile/__init__.py | 4 +++- backports/tarfile/compat/__init__.py | 0 backports/tarfile/compat/py38.py | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 backports/tarfile/compat/__init__.py create mode 100644 backports/tarfile/compat/py38.py diff --git a/backports/tarfile/__init__.py b/backports/tarfile/__init__.py index a7a9a6e..6dd498d 100755 --- a/backports/tarfile/__init__.py +++ b/backports/tarfile/__init__.py @@ -48,6 +48,8 @@ import re import warnings +from .compat.py38 import removesuffix + try: import pwd except ImportError: @@ -1365,7 +1367,7 @@ def _proc_gnulong(self, tarfile): # Remove redundant slashes from directories. This is to be consistent # with frombuf(). if next.isdir(): - next.name = next.name.removesuffix("/") + next.name = removesuffix(next.name, "/") return next diff --git a/backports/tarfile/compat/__init__.py b/backports/tarfile/compat/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backports/tarfile/compat/py38.py b/backports/tarfile/compat/py38.py new file mode 100644 index 0000000..20fbbfc --- /dev/null +++ b/backports/tarfile/compat/py38.py @@ -0,0 +1,24 @@ +import sys + + +if sys.version_info < (3, 9): + + def removesuffix(self, suffix): + # suffix='' should not call self[:-0]. + if suffix and self.endswith(suffix): + return self[: -len(suffix)] + else: + return self[:] + + def removeprefix(self, prefix): + if self.startswith(prefix): + return self[len(prefix) :] + else: + return self[:] +else: + + def removesuffix(self, suffix): + return self.removesuffix(suffix) + + def removeprefix(self, prefix): + return self.removeprefix(prefix)