|
14 | 14 | import encodings |
15 | 15 | import urllib.request |
16 | 16 | import urllib.error |
| 17 | +import shutil |
17 | 18 | import subprocess |
18 | 19 | import sysconfig |
19 | 20 | from copy import copy |
@@ -488,57 +489,79 @@ def test_startup_interactivehook_isolated_explicit(self): |
488 | 489 | 'import site, sys; site.enablerlcompleter(); sys.exit(hasattr(sys, "__interactivehook__"))']).wait() |
489 | 490 | self.assertTrue(r, "'__interactivehook__' not added by enablerlcompleter()") |
490 | 491 |
|
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' |
494 | 498 | try: |
495 | | - libpath = os.path.dirname(os.path.dirname(encodings.__file__)) |
496 | 499 | 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 | + ]) |
503 | 524 |
|
| 525 | + try: |
504 | 526 | env = os.environ.copy() |
505 | 527 | 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', |
507 | 530 | 'import sys; sys.exit(sys.flags.no_site and ' |
508 | 531 | 'len(sys.path) > 200 and ' |
509 | 532 | '%r in sys.path and %r in sys.path and %r not in sys.path)' % ( |
510 | 533 | os.path.join(sys.prefix, 'fake-path-name'), |
511 | 534 | libpath, |
512 | 535 | os.path.join(sys.prefix, 'from-env'), |
513 | 536 | )], env=env) |
514 | | - self.assertEqual(rc, 0) |
515 | 537 | finally: |
516 | | - os.unlink(_pth_file) |
| 538 | + self._cleanup_underpth_exe(exe_file) |
| 539 | + self.assertEqual(rc, 0) |
517 | 540 |
|
518 | 541 | @unittest.skipUnless(sys.platform == 'win32', "only supported on Windows") |
519 | 542 | 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 | + ]) |
521 | 551 | 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 | | - |
530 | 552 | env = os.environ.copy() |
531 | 553 | 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', |
533 | 556 | 'import sys; sys.exit(not sys.flags.no_site and ' |
534 | 557 | '%r in sys.path and %r in sys.path and %r not in sys.path)' % ( |
535 | 558 | os.path.join(sys.prefix, 'fake-path-name'), |
536 | 559 | libpath, |
537 | 560 | os.path.join(sys.prefix, 'from-env'), |
538 | 561 | )], env=env) |
539 | | - self.assertEqual(rc, 0) |
540 | 562 | finally: |
541 | | - os.unlink(_pth_file) |
| 563 | + self._cleanup_underpth_exe(exe_file) |
| 564 | + self.assertEqual(rc, 0) |
542 | 565 |
|
543 | 566 |
|
544 | 567 | if __name__ == "__main__": |
|
0 commit comments