10000 bpo-37421: Fix test_distutils.test_build_ext() (GH-14564) · python/cpython@0aefba7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0aefba7

Browse files
bpo-37421: Fix test_distutils.test_build_ext() (GH-14564)
test_distutils.test_build_ext() is now able to remove the temporary directory on Windows: don't import the newly built C extension ("xx") in the current process, but test it in a separated process. (cherry picked from commit 74c9dd5) Co-authored-by: Victor Stinner <vstinner@redhat.com>
1 parent 79665c6 commit 0aefba7

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

Lib/distutils/tests/support.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import unittest
77
import sysconfig
88
from copy import deepcopy
9+
import test.support
910

1011
from distutils import log
1112
from distutils.log import DEBUG, INFO, WARN, ERROR, FATAL
@@ -64,8 +65,8 @@ def tearDown(self):
6465
os.chdir(self.old_cwd)
6566
super().tearDown()
6667
while self.tempdirs:
67-
d = self.tempdirs.pop()
68-
shutil.rmtree(d, os.name in ('nt', 'cygwin'))
68+
tmpdir = self.tempdirs.pop()
69+
test.support.rmtree(tmpdir)
6970

7071
def mkdtemp(self):
7172
"""Create a temporary directory that will be cleaned up.

Lib/distutils/tests/test_build_ext.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import unittest
1717
from test import support
18+
from test.support.script_helper import assert_python_ok
1819

1920
# http://bugs.python.org/issue4373
2021
# Don't load the xx module more than once.
@@ -26,11 +27,8 @@ class BuildExtTestCase(TempdirManager,
2627
unittest.TestCase):
2728
def setUp(self):
2829
# Create a simple test environment
29-
# Note that we're making changes to sys.path
3030
super(BuildExtTestCase, self).setUp()
3131
self.tmp_dir = self.mkdtemp()
32-
self.sys_path = sys.path, sys.path[:]
33-
sys.path.append(self.tmp_dir)
3432
import site
3533
self.old_user_base = site.USER_BASE
3634
site.USER_BASE = self.mkdtemp()
@@ -40,15 +38,11 @@ def setUp(self):
4038
# bpo-30132: On Windows, a .pdb file may be created in the current
4139
# working directory. Create a temporary working directory to cleanup
4240
# everything at the end of the test.
43-
self.temp_cwd = support.temp_cwd()
44-
self.temp_cwd.__enter__()
45-
self.addCleanup(self.temp_cwd.__exit__, None, None, None)
41+
change_cwd = support.change_cwd(self.tmp_dir)
42+
change_cwd.__enter__()
43+
self.addCleanup(change_cwd.__exit__, None, None, None)
4644

4745
def tearDown(self):
48-
# Get everything back to normal
49-
support.unload('xx')
50-
sys.path = self.sys_path[0]
51-
sys.path[:] = self.sys_path[1]
5246
import site
5347
site.USER_BASE = self.old_user_base
5448
from distutils.command import build_ext
@@ -88,19 +82,34 @@ def test_build_ext(self):
8882
else:
8983
ALREADY_TESTED = type(self).__name__
9084

91-
import xx
85+
code = textwrap.dedent(f"""
86+
tmp_dir = {self.tmp_dir!r}
9287
93-
for attr in ('error', 'foo', 'new', 'roj'):
94-
self.assertTrue(hasattr(xx, attr))
88+
import sys
89+
import unittest
90+
from test import support
9591
96-
self.assertEqual(xx.foo(2, 5), 7)
97-
self.assertEqual(xx.foo(13,15), 28)
98-
self.assertEqual(xx.new().demo(), None)
99-
if support.HAVE_DOCSTRINGS:
100-
doc = 'This is a template module just for instruction.'
101-
self.assertEqual(xx.__doc__, doc)
102-
self.assertIsInstance(xx.Null(), xx.Null)
103-
self.assertIsInstance(xx.Str(), xx.Str)
92+
sys.path.insert(0, tmp_dir)
93+
import xx
94+
95+
class Tests(unittest.TestCase):
96+
def test_xx(self):
97+
for attr in ('error', 'foo', 'new', 'roj'):
98+
self.assertTrue(hasattr(xx, attr))
99+
100+
self.assertEqual(xx.foo(2, 5), 7)
101+
self.assertEqual(xx.foo(13,15), 28)
102+
self.assertEqual(xx.new().demo(), None)
103+
if support.HAVE_DOCSTRINGS:
104+
doc = 'This is a template module just for instruction.'
105+
self.assertEqual(xx.__doc__, doc)
106+
self.assertIsInstance(xx.Null(), xx.Null)
107+
self.assertIsInstance(xx.Str(), xx.Str)
108+
109+
110+
unittest.main()
111+
""")
112+
assert_python_ok('-c', code)
104113

105114
def test_solaris_enable_shared(self):
106115
dist = Distribution({'name': 'xx'})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test_distutils.test_build_ext() is now able to remove the temporary
2+
directory on Windows: don't import the newly built C extension ("xx") in the
3+
current process, but test it in a separated process.

0 commit comments

Comments
 (0)
0