Skip to content

Commit c575172

Browse files
authored
gh-149879: Fix sys.orig_argv[0] on Cygwin: add ".exe" suffix (#149885)
1 parent 50aff5f commit c575172

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Python/initconfig.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,6 +3688,44 @@ PyConfig_SetWideStringList(PyConfig *config, PyWideStringList *list,
36883688
}
36893689

36903690

3691+
#ifdef __CYGWIN__
3692+
// Cygwin strips ".exe" suffix from argv[0].
3693+
// Add again the ".exe" suffix.
3694+
static PyStatus
3695+
config_argv0_add_exe(PyConfig *config)
3696+
{
3697+
if (config->argv.length < 1) {
3698+
return _PyStatus_OK();
3699+
}
3700+
const wchar_t *argv0 = config->argv.items[0];
3701+
size_t len = wcslen(argv0);
3702+
if (len >= 5 && wcscmp(argv0 + len - 4, L".exe") == 0) {
3703+
return _PyStatus_OK();
3704+
}
3705+
3706+
wchar_t *exe = PyMem_RawMalloc((len + 4 + 1) * sizeof(wchar_t));
3707+
if (exe == NULL) {
3708+
return _PyStatus_NO_MEMORY();
3709+
}
3710+
wcscpy(exe, argv0);
3711+
wcscat(exe, L".exe");
3712+
3713+
FILE *fp = _Py_wfopen(exe, L"rb");
3714+
if (fp != NULL) {
3715+
fclose(fp);
3716+
3717+
PyMem_RawFree(config->argv.items[0]);
3718+
config->argv.items[0] = exe;
3719+
}
3720+
else {
3721+
PyMem_RawFree(exe);
3722+
}
3723+
3724+
return _PyStatus_OK();
3725+
}
3726+
#endif
3727+
3728+
36913729
/* Read the configuration into PyConfig from:
36923730
36933731
* Command line arguments
@@ -3707,6 +3745,13 @@ _PyConfig_Read(PyConfig *config, int compute_path_config)
37073745

37083746
config_get_global_vars(config);
37093747

3748+
#ifdef __CYGWIN__
3749+
status = config_argv0_add_exe(config);
3750+
if (_PyStatus_EXCEPTION(status)) {
3751+
return status;
3752+
}
3753+
#endif
3754+
37103755
if (config->orig_argv.length == 0
37113756
&& !(config->argv.length == 1
37123757
&& wcscmp(config->argv.items[0], L"") == 0))

0 commit comments

Comments
 (0)