Skip to content

Commit

Permalink
Fix SyntaxError on Python 3.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed May 28, 2024
1 parent 3e5775f commit 5759c07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
18 changes: 18 additions & 0 deletions tests/compat/py38.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import random
import sys
import test.support
Expand Down Expand Up @@ -75,3 +76,20 @@ def removesuffix(self, suffix):
def removeprefix(self, prefix):
return self.removeprefix(prefix)


@contextlib.contextmanager
def temp_tarfile_open(DIR, tarname):
"""
Syntax compatibility for Python 3.8 for:
```
with (
os_helper.temp_dir(DIR),
tarfile.open(tarname, encoding="iso8859-1") as tar
):
```
"""
from .py310 import os_helper
import tarfile
with os_helper.temp_dir(DIR), tarfile.open(tarname, encoding="iso8859-1") as tar:
yield tar
11 changes: 3 additions & 8 deletions tests/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from test.support import script_helper
from .compat.py38 import warnings_helper
from .compat.py38 import removesuffix
from .compat.py38 import temp_tarfile_open

# Check for our compression modules.
try:
Expand Down Expand Up @@ -741,10 +742,7 @@ def test_extract_directory(self):

def test_deprecation_if_no_filter_passed_to_extractall(self):
DIR = pathlib.Path(TEMPDIR) / "extractall"
with (
os_helper.temp_dir(DIR),
tarfile.open(tarname, encoding="iso8859-1") as tar
):
with temp_tarfile_open(DIR, tarname) as tar:
directories = [t for t in tar if t.isdir()]
with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm:
tar.extractall(DIR, directories)
Expand All @@ -754,10 +752,7 @@ def test_deprecation_if_no_filter_passed_to_extractall(self):
def test_deprecation_if_no_filter_passed_to_extract(self):
dirtype = "ustar/dirtype"
DIR = pathlib.Path(TEMPDIR) / "extractall"
with (
os_helper.temp_dir(DIR),
tarfile.open(tarname, encoding="iso8859-1") as tar
):
with temp_tarfile_open(DIR, tarname) as tar:
tarinfo = tar.getmember(dirtype)
with self.assertWarnsRegex(DeprecationWarning, "Use the filter argument") as cm:
tar.extract(tarinfo, path=DIR)
Expand Down

0 comments on commit 5759c07

Please sign in to comment.