bpo-29763: Use unittest cleanup in test_site (GH-841) · python/cpython@b94d7fd (original) (raw)

`@@ -17,6 +17,7 @@

`

17

17

`import shutil

`

18

18

`import subprocess

`

19

19

`import sysconfig

`

``

20

`+

import tempfile

`

20

21

`from copy import copy

`

21

22

``

22

23

`# These tests are not particularly useful if Python was invoked with -S.

`

`@@ -489,29 +490,25 @@ def test_startup_interactivehook_isolated_explicit(self):

`

489

490

`'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "interactivehook"))']).wait()

`

490

491

`self.assertTrue(r, "'interactivehook' not added by enablerlcompleter()")

`

491

492

``

492

``

`-

@classmethod

`

``

493

+

``

494

`+

@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")

`

``

495

`+

class _pthFileTests(unittest.TestCase):

`

``

496

+

493

497

`def _create_underpth_exe(self, lines):

`

494

``

`-

exe_file = os.path.join(os.getenv('TEMP'), os.path.split(sys.executable)[1])

`

``

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

`+

)

`

495

504

`shutil.copy(sys.executable, exe_file)

`

496

505

``

497

506

`_pth_file = os.path.splitext(exe_file)[0] + '._pth'

`

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)

`

``

507

`+

with open(_pth_file, 'w') as f:

`

``

508

`+

for line in lines:

`

``

509

`+

print(line, file=f)

`

``

510

`+

return exe_file

`

513

511

``

514

``

`-

@classmethod

`

515

512

`def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):

`

516

513

`sys_path = []

`

517

514

`for line in lines:

`

`@@ -521,7 +518,6 @@ def _calc_sys_path_for_underpth_nosite(self, sys_prefix, lines):

`

521

518

`sys_path.append(abs_path)

`

522

519

`return sys_path

`

523

520

``

524

``

`-

@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")

`

525

521

`def test_underpth_nosite_file(self):

`

526

522

`libpath = os.path.dirname(os.path.dirname(encodings.file))

`

527

523

`exe_prefix = os.path.dirname(sys.executable)

`

`@@ -536,20 +532,16 @@ def test_underpth_nosite_file(self):

`

536

532

`os.path.dirname(exe_file),

`

537

533

`pth_lines)

`

538

534

``

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)

`

``

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)

`

550

543

`self.assertTrue(rc, "sys.path is incorrect")

`

551

544

``

552

``

`-

@unittest.skipUnless(sys.platform == 'win32', "only supported on Windows")

`

553

545

`def test_underpth_file(self):

`

554

546

`libpath = os.path.dirname(os.path.dirname(encodings.file))

`

555

547

`exe_prefix = os.path.dirname(sys.executable)

`

`@@ -561,20 +553,17 @@ def test_underpth_file(self):

`

561

553

`'import site'

`

562

554

` ])

`

563

555

`sys_prefix = os.path.dirname(exe_file)

`

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)

`

``

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)

`

578

567

`self.assertTrue(rc, "sys.path is incorrect")

`

579

568

``

580

569

``