MCPcopy Index your code
hub / github.com/python/cpython / PythonSymlink

Class PythonSymlink

Lib/test/support/__init__.py:1682–1762  ·  view source on GitHub ↗

Creates a symlink for the current Python executable

Source from the content-addressed store, hash-verified

1680 return unittest.skip("buggy MSVC UCRT strptime/strftime")(test) if _buggy_ucrt else test
1681
1682class PythonSymlink:
1683 """Creates a symlink for the current Python executable"""
1684 def __init__(self, link=None):
1685 from .os_helper import TESTFN
1686
1687 self.link = link or os.path.abspath(TESTFN)
1688 self._linked = []
1689 self.real = os.path.realpath(sys.executable)
1690 self._also_link = []
1691
1692 self._env = None
1693
1694 self._platform_specific()
1695
1696 if sys.platform == "win32":
1697 def _platform_specific(self):
1698 import glob
1699 import _winapi
1700
1701 if os.path.lexists(self.real) and not os.path.exists(self.real):
1702 # App symlink appears to not exist, but we want the
1703 # real executable here anyway
1704 self.real = _winapi.GetModuleFileName(0)
1705
1706 dll = _winapi.GetModuleFileName(sys.dllhandle)
1707 src_dir = os.path.dirname(dll)
1708 dest_dir = os.path.dirname(self.link)
1709 self._also_link.append((
1710 dll,
1711 os.path.join(dest_dir, os.path.basename(dll))
1712 ))
1713 for runtime in glob.glob(os.path.join(glob.escape(src_dir), "vcruntime*.dll")):
1714 self._also_link.append((
1715 runtime,
1716 os.path.join(dest_dir, os.path.basename(runtime))
1717 ))
1718
1719 self._env = {k.upper(): os.getenv(k) for k in os.environ}
1720 home = os.path.dirname(self.real)
1721 if sysconfig.is_python_build():
1722 home = os.path.join(home, sysconfig.get_config_var('VPATH'))
1723 self._env["PYTHONHOME"] = home
1724 else:
1725 def _platform_specific(self):
1726 pass
1727
1728 def __enter__(self):
1729 os.symlink(self.real, self.link)
1730 self._linked.append(self.link)
1731 for real, link in self._also_link:
1732 os.symlink(real, link)
1733 self._linked.append(link)
1734 return self
1735
1736 def __exit__(self, exc_type, exc_value, exc_tb):
1737 for link in self._linked:
1738 try:
1739 os.remove(link)

Callers 1

test_symlinkMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_symlinkMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…