Skip to content

Commit 6cdbd7b

Browse files
authored
gh-122941: Fix test_launcher sporadic failures via py.ini isolation (GH-145090)
Adds _PYLAUNCHER_INIDIR as a private variable since the launcher is deprecated and not getting new features.
1 parent a003923 commit 6cdbd7b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

Lib/test/test_launcher.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ def run_py(self, args, env=None, allow_fail=False, expect_returncode=0, argv=Non
227227
"PYLAUNCHER_LIMIT_TO_COMPANY": "",
228228
**{k.upper(): v for k, v in (env or {}).items()},
229229
}
230+
if ini_dir := getattr(self, '_ini_dir', None):
231+
env.setdefault("_PYLAUNCHER_INIDIR", ini_dir)
230232
if not argv:
231233
argv = [self.py_exe, *args]
232234
with subprocess.Popen(
@@ -262,11 +264,14 @@ def run_py(self, args, env=None, allow_fail=False, expect_returncode=0, argv=Non
262264
return data
263265

264266
def py_ini(self, content):
265-
local_appdata = os.environ.get("LOCALAPPDATA")
266-
if not local_appdata:
267-
raise unittest.SkipTest("LOCALAPPDATA environment variable is "
268-
"missing or empty")
269-
return PreservePyIni(Path(local_appdata) / "py.ini", content)
267+
ini_dir = getattr(self, '_ini_dir', None)
268+
if not ini_dir:
269+
local_appdata = os.environ.get("LOCALAPPDATA")
270+
if not local_appdata:
271+
raise unittest.SkipTest("LOCALAPPDATA environment variable is "
272+
"missing or empty")
273+
ini_dir = local_appdata
274+
return PreservePyIni(Path(ini_dir) / "py.ini", content)
270275

271276
@contextlib.contextmanager
272277
def script(self, content, encoding="utf-8"):
@@ -302,6 +307,8 @@ def setUpClass(cls):
302307
p = subprocess.check_output("reg query HKCU\\Software\\Python /s")
303308
#print(p.decode('mbcs'))
304309

310+
cls._ini_dir = tempfile.mkdtemp()
311+
cls.addClassCleanup(shutil.rmtree, cls._ini_dir, ignore_errors=True)
305312

306313
@classmethod
307314
def tearDownClass(cls):

PC/launcher2.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,20 @@ _readIni(const wchar_t *section, const wchar_t *settingName, wchar_t *buffer, in
922922
{
923923
wchar_t iniPath[MAXLEN];
924924
int n;
925+
// Check for _PYLAUNCHER_INIDIR override (used for test isolation)
926+
DWORD len = GetEnvironmentVariableW(L"_PYLAUNCHER_INIDIR", iniPath, MAXLEN);
927+
if (len && len < MAXLEN) {
928+
if (join(iniPath, MAXLEN, L"py.ini")) {
929+
debug(L"# Reading from %s for %s/%s\n", iniPath, section, settingName);
930+
n = GetPrivateProfileStringW(section, settingName, NULL, buffer, bufferLength, iniPath);
931+
if (n) {
932+
debug(L"# Found %s in %s\n", settingName, iniPath);
933+
return n;
934+
}
935+
}
936+
// When _PYLAUNCHER_INIDIR is set, skip the default locations
937+
return 0;
938+
}
925939
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, iniPath)) &&
926940
join(iniPath, MAXLEN, L"py.ini")) {
927941
debug(L"# Reading from %s for %s/%s\n", iniPath, section, settingName);

0 commit comments

Comments
 (0)