Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1da055e

Browse files
committed
Makes test_underpth* tests more robust by copying the executable.
1 parent 0eacef3 commit 1da055e

2 files changed

Lines changed: 51 additions & 25 deletions

File tree

Lib/test/test_site.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import encodings
1515
import urllib.request
1616
import urllib.error
17+
import shutil
1718
import subprocess
1819
import sysconfig
1920
from copy import copy
@@ -488,57 +489,79 @@ def test_startup_interactivehook_isolated_explicit(self):
488489
'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait()
489490
self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()")
490491

491-
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
492-
def test_underpth_nosite_file(self):
493-
_pth_file = os.path.splitext(sys.executable)[0] + '._pth'
492+
@classmethod
493+
def _create_underpth_exe(self, lines):
494+
exe_file = os.path.join(os.getenv('TEMP'), os.path.split(sys.executable)[1])
495+
shutil.copy(sys.executable, exe_file)
496+
497+
_pth_file = os.path.splitext(exe_file)[0] + '._pth'
494498
try:
495-
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
496499
with open(_pth_file, 'w') as f:
497-
print('fake-path-name', file=f)
498-
# Ensure the generated path is very long so that buffer
499-
# resizing in getpathp.c is exercised
500-
for _ in range(200):
501-
print(libpath, file=f)
502-
print('# comment', file=f)
500+
for line in lines:
501+
print(line, file=f)
502+
return exe_file
503+
except:
504+
os.unlink(_pth_file)
505+
os.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+
os.unlink(_pth_file)
512+
os.unlink(exe_file)
513+
514+
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
515+
def test_underpth_nosite_file(self):
516+
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
517+
exe_prefix = os.path.dirname(sys.executable)
518+
exe_file = self._create_underpth_exe([
519+
'fake-path-name',
520+
*[libpath for _ in range(200)],
521+
'# comment',
522+
'import site'
523+
])
503524

525+
try:
504526
env = os.environ.copy()
505527
env['PYTHONPATH'] = 'from-env'
506-
rc = subprocess.call([sys.executable, '-c',
528+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
529+
rc = subprocess.call([exe_file, '-c',
507530
'import sys; sys.exit(sys.flags.no_site and '
508531
'len(sys.path) > 200 and '
509532
'%r in sys.path and %r in sys.path and %r not in sys.path)' % (
510533
os.path.join(sys.prefix, 'fake-path-name'),
511534
libpath,
512535
os.path.join(sys.prefix, 'from-env'),
513536
)], env=env)
514-
self.assertEqual(rc, 0)
515537
finally:
516-
os.unlink(_pth_file)
538+
self._cleanup_underpth_exe(exe_file)
539+
self.assertEqual(rc, 0)
517540

518541
@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")
519542
def test_underpth_file(self):
520-
_pth_file = os.path.splitext(sys.executable)[0] + '._pth'
543+
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
544+
exe_prefix = os.path.dirname(sys.executable)
545+
exe_file = self._create_underpth_exe([
546+
'fake-path-name',
547+
*[libpath for _ in range(200)],
548+
'# comment',
549+
'import site'
550+
])
521551
try:
522-
libpath = os.path.dirname(os.path.dirname(encodings.__file__))
523-
with open(_pth_file, 'w') as f:
524-
print('fake-path-name', file=f)
525-
for _ in range(200):
526-
print(libpath, file=f)
527-
print('# comment', file=f)
528-
print('import site', file=f)
529-
530552
env = os.environ.copy()
531553
env['PYTHONPATH'] = 'from-env'
532-
rc = subprocess.call([sys.executable, '-c',
554+
env['PATH'] = '{};{}'.format(exe_prefix, os.getenv('PATH'))
555+
rc = subprocess.call([exe_file, '-c',
533556
'import sys; sys.exit(not sys.flags.no_site and '
534557
'%r in sys.path and %r in sys.path and %r not in sys.path)' % (
535558
os.path.join(sys.prefix, 'fake-path-name'),
536559
libpath,
537560
os.path.join(sys.prefix, 'from-env'),
538561
)], env=env)
539-
self.assertEqual(rc, 0)
540562
finally:
541-
os.unlink(_pth_file)
563+
self._cleanup_underpth_exe(exe_file)
564+
self.assertEqual(rc, 0)
542565

543566

544567
if __name__ == "__main__":

PCbuild/rt.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ if defined qmode goto Qmode
4848
echo Deleting .pyc/.pyo files ...
4949
"%exe%" "%pcbuild%rmpyc.py"
5050

51+
echo Cleaning _pth files ...
52+
if exist %prefix%*._pth del %prefix%*._pth
53+
5154
echo on
5255
%cmd%
5356
@echo off

0 commit comments

Comments
 (0)