diff --git a/distutils/command/build_py.py b/distutils/command/build_py.py index cf0ca57c32..edc2171cd1 100644 --- a/distutils/command/build_py.py +++ b/distutils/command/build_py.py @@ -5,7 +5,7 @@ import os import importlib.util import sys -from glob import glob +import glob from distutils.core import Command from distutils.errors import * @@ -125,7 +125,7 @@ def find_data_files(self, package, src_dir): files = [] for pattern in globs: # Each pattern has to be converted to a platform-specific path - filelist = glob(os.path.join(src_dir, convert_path(pattern))) + filelist = glob.glob(os.path.join(glob.escape(src_dir), convert_path(pattern))) # Files that match more than one pattern are only added once files.extend([fn for fn in filelist if fn not in files and os.path.isfile(fn)]) @@ -216,7 +216,7 @@ def check_module(self, module, module_file): def find_package_modules(self, package, package_dir): self.check_package(package, package_dir) - module_files = glob(os.path.join(package_dir, "*.py")) + module_files = glob.glob(os.path.join(glob.escape(package_dir), "*.py")) modules = [] setup_script = os.path.abspath(self.distribution.script_name) diff --git a/distutils/tests/__init__.py b/distutils/tests/__init__.py index 5d2e69e3e6..c7dcc7ec3d 100644 --- a/distutils/tests/__init__.py +++ b/distutils/tests/__init__.py @@ -15,26 +15,26 @@ import os import sys import unittest -import warnings from test.support import run_unittest +from .py38compat import save_restore_warnings_filters + here = os.path.dirname(__file__) or os.curdir def test_suite(): - old_filters = warnings.filters[:] suite = unittest.TestSuite() for fn in os.listdir(here): if fn.startswith("test") and fn.endswith(".py"): modname = "distutils.tests." + fn[:-3] - __import__(modname) + # bpo-40055: Save/restore warnings filters to leave them unchanged. + # Importing tests imports docutils which imports pkg_resources + # which adds a warnings filter. + with save_restore_warnings_filters(): + __import__(modname) module = sys.modules[modname] suite.addTest(module.test_suite()) - # bpo-40055: Save/restore warnings filters to leave them unchanged. - # Importing tests imports docutils which imports pkg_resources which adds a - # warnings filter. - warnings.filters[:] = old_filters return suite diff --git a/distutils/tests/py38compat.py b/distutils/tests/py38compat.py new file mode 100644 index 0000000000..46ff575892 --- /dev/null +++ b/distutils/tests/py38compat.py @@ -0,0 +1,50 @@ +# flake8: noqa + +import contextlib + +try: + from test.support.warnings_helper import check_warnings +except (ModuleNotFoundError, ImportError): + from test.support import check_warnings + + +try: + from test.support.os_helper import ( + change_cwd, + rmtree, + EnvironmentVarGuard, + TESTFN, + unlink, + skip_unless_symlink, + temp_dir, + create_empty_file, + temp_cwd, + ) +except (ModuleNotFoundError, ImportError): + from test.support import ( + change_cwd, + rmtree, + EnvironmentVarGuard, + TESTFN, + unlink, + skip_unless_symlink, + temp_dir, + create_empty_file, + temp_cwd, + ) + + +# From Python 3.9 +@contextlib.contextmanager +def _save_restore_warnings_filters(): + old_filters = warnings.filters[:] + try: + yield + finally: + warnings.filters[:] = old_filters + + +try: + from test.support.warnings_helper import save_restore_warnings_filters +except (ModuleNotFoundError, ImportError): + save_restore_warnings_filters = _save_restore_warnings_filters diff --git a/distutils/tests/support.py b/distutils/tests/support.py index 259af882ec..b4410fc9d9 100644 --- a/distutils/tests/support.py +++ b/distutils/tests/support.py @@ -6,7 +6,8 @@ import unittest import sysconfig from copy import deepcopy -import test.support + +from . import py38compat as os_helper from distutils import log from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL @@ -64,7 +65,7 @@ def tearDown(self): super().tearDown() while self.tempdirs: tmpdir = self.tempdirs.pop() - test.support.rmtree(tmpdir) + os_helper.rmtree(tmpdir) def mkdtemp(self): """Create a temporary directory that will be cleaned up. diff --git a/distutils/tests/test_archive_util.py b/distutils/tests/test_archive_util.py index e9aad0e40f..ce6456dc5d 100644 --- a/distutils/tests/test_archive_util.py +++ b/distutils/tests/test_archive_util.py @@ -13,7 +13,10 @@ ARCHIVE_FORMATS) from distutils.spawn import find_executable, spawn from distutils.tests import support -from test.support import check_warnings, run_unittest, patch, change_cwd +from test.support import run_unittest, patch + +from .py38compat import change_cwd +from .py38compat import check_warnings try: import grp diff --git a/distutils/tests/test_bdist_msi.py b/distutils/tests/test_bdist_msi.py index 418e60ec72..937266f8c0 100644 --- a/distutils/tests/test_bdist_msi.py +++ b/distutils/tests/test_bdist_msi.py @@ -1,9 +1,11 @@ """Tests for distutils.command.bdist_msi.""" import sys import unittest -from test.support import run_unittest, check_warnings +from test.support import run_unittest from distutils.tests import support +from .py38compat import check_warnings + @unittest.skipUnless(sys.platform == 'win32', 'these tests require Windows') class BDistMSITestCase(support.TempdirManager, diff --git a/distutils/tests/test_bdist_wininst.py b/distutils/tests/test_bdist_wininst.py index 5c3d025d33..31cf2628de 100644 --- a/distutils/tests/test_bdist_wininst.py +++ b/distutils/tests/test_bdist_wininst.py @@ -2,7 +2,9 @@ import sys import platform import unittest -from test.support import run_unittest, check_warnings +from test.support import run_unittest + +from .py38compat import check_warnings from distutils.command.bdist_wininst import bdist_wininst from distutils.tests import support diff --git a/distutils/tests/test_build_ext.py b/distutils/tests/test_build_ext.py index 1aec153783..5a72458c16 100644 --- a/distutils/tests/test_build_ext.py +++ b/distutils/tests/test_build_ext.py @@ -15,6 +15,7 @@ import unittest from test import support +from . import py38compat as os_helper from test.support.script_helper import assert_python_ok # http://bugs.python.org/issue4373 @@ -38,7 +39,7 @@ def setUp(self): # bpo-30132: On Windows, a .pdb file may be created in the current # working directory. Create a temporary working directory to cleanup # everything at the end of the test. - change_cwd = support.change_cwd(self.tmp_dir) + change_cwd = os_helper.change_cwd(self.tmp_dir) change_cwd.__enter__() self.addCleanup(change_cwd.__exit__, None, None, None) diff --git a/distutils/tests/test_core.py b/distutils/tests/test_core.py index 27ce7324af..666ff4a3cd 100644 --- a/distutils/tests/test_core.py +++ b/distutils/tests/test_core.py @@ -5,8 +5,8 @@ import os import shutil import sys -import test.support from test.support import captured_stdout, run_unittest +from . import py38compat as os_helper import unittest from distutils.tests import support from distutils import log @@ -62,13 +62,13 @@ def tearDown(self): super(CoreTestCase, self).tearDown() def cleanup_testfn(self): - path = test.support.TESTFN + path = os_helper.TESTFN if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) - def write_setup(self, text, path=test.support.TESTFN): + def write_setup(self, text, path=os_helper.TESTFN): f = open(path, "w") try: f.write(text) @@ -105,8 +105,8 @@ def test_run_setup_uses_current_dir(self): cwd = os.getcwd() # Create a directory and write the setup.py file there: - os.mkdir(test.support.TESTFN) - setup_py = os.path.join(test.support.TESTFN, "setup.py") + os.mkdir(os_helper.TESTFN) + setup_py = os.path.join(os_helper.TESTFN, "setup.py") distutils.core.run_setup( self.write_setup(setup_prints_cwd, path=setup_py)) diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index d431085bbd..45eadee87f 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -12,8 +12,9 @@ from distutils.cmd import Command from test.support import ( - TESTFN, captured_stdout, captured_stderr, run_unittest + captured_stdout, captured_stderr, run_unittest ) +from .py38compat import TESTFN from distutils.tests import support from distutils import log diff --git a/distutils/tests/test_extension.py b/distutils/tests/test_extension.py index e35f2738b6..2eb5b422df 100644 --- a/distutils/tests/test_extension.py +++ b/distutils/tests/test_extension.py @@ -3,9 +3,11 @@ import os import warnings -from test.support import check_warnings, run_unittest +from test.support import run_unittest from distutils.extension import read_setup_file, Extension +from .py38compat import check_warnings + class ExtensionTestCase(unittest.TestCase): def test_read_setup_file(self): diff --git a/distutils/tests/test_file_util.py b/distutils/tests/test_file_util.py index a4e2d025f9..d253607526 100644 --- a/distutils/tests/test_file_util.py +++ b/distutils/tests/test_file_util.py @@ -8,7 +8,9 @@ from distutils import log from distutils.tests import support from distutils.errors import DistutilsFileError -from test.support import run_unittest, unlink +from test.support import run_unittest +from .py38compat import unlink + class FileUtilTestCase(support.TempdirManager, unittest.TestCase): diff --git a/distutils/tests/test_filelist.py b/distutils/tests/test_filelist.py index 71fde2b718..d8e4b39fd2 100644 --- a/distutils/tests/test_filelist.py +++ b/distutils/tests/test_filelist.py @@ -8,11 +8,11 @@ from distutils.filelist import glob_to_re, translate_pattern, FileList from distutils import filelist -import test.support from test.support import captured_stdout, run_unittest from distutils.tests import support from .py35compat import adapt_glob +from . import py38compat as os_helper MANIFEST_IN = """\ @@ -298,9 +298,9 @@ def test_process_template(self): class FindAllTestCase(unittest.TestCase): - @test.support.skip_unless_symlink + @os_helper.skip_unless_symlink def test_missing_symlink(self): - with test.support.temp_cwd(): + with os_helper.temp_cwd(): os.symlink('foo', 'bar') self.assertEqual(filelist.findall(), []) @@ -310,13 +310,13 @@ def test_basic_discovery(self): '.' as the parameter, the dot should be omitted from the results. """ - with test.support.temp_cwd(): + with os_helper.temp_cwd(): os.mkdir('foo') file1 = os.path.join('foo', 'file1.txt') - test.support.create_empty_file(file1) + os_helper.create_empty_file(file1) os.mkdir('bar') file2 = os.path.join('bar', 'file2.txt') - test.support.create_empty_file(file2) + os_helper.create_empty_file(file2) expected = [file2, file1] self.assertEqual(sorted(filelist.findall()), expected) @@ -325,9 +325,9 @@ def test_non_local_discovery(self): When findall is called with another path, the full path name should be returned. """ - with test.support.temp_dir() as temp_dir: + with os_helper.temp_dir() as temp_dir: file1 = os.path.join(temp_dir, 'file1.txt') - test.support.create_empty_file(file1) + os_helper.create_empty_file(file1) expected = [file1] self.assertEqual(filelist.findall(temp_dir), expected) diff --git a/distutils/tests/test_register.py b/distutils/tests/test_register.py index e68b0af3ce..84607f996f 100644 --- a/distutils/tests/test_register.py +++ b/distutils/tests/test_register.py @@ -5,7 +5,9 @@ import urllib import warnings -from test.support import check_warnings, run_unittest +from test.support import run_unittest + +from .py38compat import check_warnings from distutils.command import register as register_module from distutils.command.register import register diff --git a/distutils/tests/test_sdist.py b/distutils/tests/test_sdist.py index 23db126959..b087a81787 100644 --- a/distutils/tests/test_sdist.py +++ b/distutils/tests/test_sdist.py @@ -6,7 +6,9 @@ import zipfile from os.path import join from textwrap import dedent -from test.support import captured_stdout, check_warnings, run_unittest +from test.support import captured_stdout, run_unittest + +from .py38compat import check_warnings try: import zlib diff --git a/distutils/tests/test_spawn.py b/distutils/tests/test_spawn.py index adaa48ef2d..f620da7843 100644 --- a/distutils/tests/test_spawn.py +++ b/distutils/tests/test_spawn.py @@ -4,9 +4,9 @@ import sys import unittest.mock from test.support import run_unittest -from test import support as test_support from .py35compat import unix_shell +from . import py38compat as os_helper from distutils.spawn import find_executable from distutils.spawn import spawn @@ -46,9 +46,9 @@ def test_spawn(self): spawn([exe]) # should work without any error def test_find_executable(self): - with test_support.temp_dir() as tmp_dir: + with os_helper.temp_dir() as tmp_dir: # use TESTFN to get a pseudo-unique filename - program_noeext = test_support.TESTFN + program_noeext = os_helper.TESTFN # Give the temporary program an ".exe" suffix for all. # It's needed on Windows and not harmful on other platforms. program = program_noeext + ".exe" @@ -68,7 +68,7 @@ def test_find_executable(self): self.assertEqual(rv, filename) # test find in the current directory - with test_support.change_cwd(tmp_dir): + with os_helper.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) @@ -78,7 +78,7 @@ def test_find_executable(self): self.assertIsNone(rv) # PATH='': no match, except in the current directory - with test_support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env['PATH'] = '' with unittest.mock.patch('distutils.spawn.os.confstr', return_value=tmp_dir, create=True), \ @@ -88,12 +88,12 @@ def test_find_executable(self): self.assertIsNone(rv) # look in current directory - with test_support.change_cwd(tmp_dir): + with os_helper.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # PATH=':': explicitly looks in the current directory - with test_support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env['PATH'] = os.pathsep with unittest.mock.patch('distutils.spawn.os.confstr', return_value='', create=True), \ @@ -102,12 +102,12 @@ def test_find_executable(self): self.assertIsNone(rv) # look in current directory - with test_support.change_cwd(tmp_dir): + with os_helper.change_cwd(tmp_dir): rv = find_executable(program) self.assertEqual(rv, program) # missing PATH: test os.confstr("CS_PATH") and os.defpath - with test_support.EnvironmentVarGuard() as env: + with os_helper.EnvironmentVarGuard() as env: env.pop('PATH', None) # without confstr diff --git a/distutils/tests/test_sysconfig.py b/distutils/tests/test_sysconfig.py index d5076391fc..c757194288 100644 --- a/distutils/tests/test_sysconfig.py +++ b/distutils/tests/test_sysconfig.py @@ -10,7 +10,11 @@ from distutils import sysconfig from distutils.ccompiler import get_default_compiler from distutils.tests import support -from test.support import TESTFN, run_unittest, check_warnings, swap_item +from test.support import run_unittest, swap_item + +from .py38compat import TESTFN +from .py38compat import check_warnings + class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): def setUp(self): diff --git a/distutils/tests/test_unixccompiler.py b/distutils/tests/test_unixccompiler.py index f2159662fd..1828ba1ae0 100644 --- a/distutils/tests/test_unixccompiler.py +++ b/distutils/tests/test_unixccompiler.py @@ -1,7 +1,9 @@ """Tests for distutils.unixccompiler.""" import sys import unittest -from test.support import EnvironmentVarGuard, run_unittest +from test.support import run_unittest + +from .py38compat import EnvironmentVarGuard from distutils import sysconfig from distutils.unixccompiler import UnixCCompiler