Skip to content

Commit cd815ed

Browse files
authored
Revert "bpo-29763: Use unittest cleanup in test_site (GH-841)" (GH-942)
This reverts commit b94d7fd.
1 parent 7bd8d3e commit cd815ed

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

Lib/test/test_site.py

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import shutil
1818
import subprocess
1919
import sysconfig
20-
import tempfile
2120
from copy import copy
2221

2322
# These tests are not particularly useful if Python was invoked with -S.
@@ -490,25 +489,29 @@ def test_startup_interactivehook_isolated_explicit(self):
490489
'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
491490
self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
492491

493-
494-
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
495-
class _pthFileTests(unittest.TestCase):
496-
492+
@classmethod
497493
def _create_underpth_exe(self, lines):
498-
self.temp_dir = tempfile.TemporaryDirectory()
499-
self.addCleanup(self.temp_dir.cleanup)
500-
exe_file = os.path.join(
501-
self.temp_dir.name,
502-
os.path.split(sys.executable)[1],
503-
)
494+
exe_file = os.path.join(os.getenv('TEMP'), os.path.split(sys.executable)[1])
504495
shutil.copy(sys.executable, exe_file)
505496

506497
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
507-
with open(_pth_file, 'w') as f:
508-
for line in lines:
509-
print(line, file=f)
510-
return exe_file
498+
try:
499+
with open(_pth_file, 'w') as f:
500+
for line in lines:
501+
print(line, file=f)
502+
return exe_file
503+
except:
504+
test.support.unlink(_pth_file)
505+
test.support.unlink(exe_file)
506+
raise
507+
508+
@classmethod
509+
def _cleanup_underpth_exe(self, exe_file):
510+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
511+
test.support.unlink(_pth_file)
512+
test.support.unlink(exe_file)
511513

514+
@classmethod
512515
def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
513516
sys_path = []
514517
for line in lines:
@@ -518,6 +521,7 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):
518521
sys_path.append(abs_path)
519522
return sys_path
520523

524+
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
521525
def test_underpth_nosite_file(self):
522526
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
523527
exe_prefix = os.path.dirname(sys.executable)
@@ -532,16 +536,20 @@ def test_underpth_nosite_file(self):
532536
os.path.dirname(exe_file),
533537
pth_lines)
534538

535-
env = os.environ.copy()
536-
env['PYTHONPATH'] = 'from-env'
537-
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
538-
rc = subprocess.call([exe_file, '-c',
539-
'import sys; sys.exit(sys.flags.no_site and '
540-
'len(sys.path) > 200 and '
541-
'sys.path == %r)' % sys_path,
542-
], env=env)
539+
try:
540+
env = os.environ.copy()
541+
env['PYTHONPATH'] = 'from-env'
542+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
543+
rc = subprocess.call([exe_file, '-c',
544+
'import sys; sys.exit(sys.flags.no_site and '
545+
'len(sys.path) > 200 and '
546+
'sys.path == %r)' % sys_path,
547+
], env=env)
548+
finally:
549+
self._cleanup_underpth_exe(exe_file)
543550
self.assertTrue(rc, "sys.path is incorrect")
544551

552+
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
545553
def test_underpth_file(self):
546554
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
547555
exe_prefix = os.path.dirname(sys.executable)
@@ -553,17 +561,20 @@ def test_underpth_file(self):
553561
'import site'
554562
])
555563
sys_prefix = os.path.dirname(exe_file)
556-
env = os.environ.copy()
557-
env['PYTHONPATH'] = 'from-env'
558-
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
559-
rc = subprocess.call([exe_file, '-c',
560-
'import sys; sys.exit(not sys.flags.no_site and '
561-
'%r in sys.path and %r in sys.path and %r not in sys.path and '
562-
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
563-
os.path.join(sys_prefix, 'fake-path-name'),
564-
libpath,
565-
os.path.join(sys_prefix, 'from-env'),
566-
)], env=env)
564+
try:
565+
env = os.environ.copy()
566+
env['PYTHONPATH'] = 'from-env'
567+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
568+
rc = subprocess.call([exe_file, '-c',
569+
'import sys; sys.exit(not sys.flags.no_site and '
570+
'%r in sys.path and %r in sys.path and %r not in sys.path and '
571+
'all("\\r" not in p and "\\n" not in p for p in sys.path))' % (
572+
os.path.join(sys_prefix, 'fake-path-name'),
573+
libpath,
574+
os.path.join(sys_prefix, 'from-env'),
575+
)], env=env)
576+
finally:
577+
self._cleanup_underpth_exe(exe_file)
567578
self.assertTrue(rc, "sys.path is incorrect")
568579

569580

0 commit comments

Comments
 (0)