Skip to content

Commit

Permalink
Add compatibility shim for removesuffix.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 16, 2024
1 parent 827f8a2 commit b572934
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion backports/tarfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import re
import warnings

from .compat.py38 import removesuffix

try:
import pwd
except ImportError:
Expand Down Expand Up @@ -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

Expand Down
Empty file.
24 changes: 24 additions & 0 deletions backports/tarfile/compat/py38.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b572934

Please sign in to comment.