Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Fix nemesis to remove running in isolation and set argv appropriately #73

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions nemesis/nemesis.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ struct _inittab inittab[] = {
{ 0, 0 }
};


void
freeWchar(wchar_t* strings[],
const int nstrings) {
int i;

for (i = 0; i < nstrings; ++i) {
PyMem_RawFree(strings[i]);
}
PyMem_RawFree(strings);
}


wchar_t**
wcharFromChar(char* strings[],
const int nstrings) {
int i;

wchar_t** wstrings = PyMem_RawMalloc(sizeof(wchar_t*)*nstrings);
if (!wstrings) { return NULL; }

for (i = 0; i < nstrings; ++i) {
wstrings[i] = Py_DecodeLocale(strings[i], NULL);
if (!wstrings[i]) {
freeWchar(wstrings, i);
return NULL;
}
}

return wstrings;
}


int main(int argc, char* argv[])
{
int c_status;
Expand Down Expand Up @@ -106,19 +139,25 @@ int main(int argc, char* argv[])

return Py_RunMain();
} else {
PyConfig_InitIsolatedConfig(&config);
PyConfig_InitPythonConfig(&config);

py_status = PyConfig_SetBytesString(&config, &config.program_name, argv[0]);
if (PyStatus_Exception(py_status)) { goto exception; }

py_status = PyConfig_SetBytesArgv(&config, argc-1, argv+1);
if (PyStatus_Exception(py_status)) { goto exception; }

py_status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(py_status)) { goto exception; }

PyConfig_Clear(&config);

/* :KLUDGE: PySys_SetArgv() is deprecated in v3.11 and is scheduled
* for removal in v3.13. Switching to Michael Aivazis' current Pyre
* should make this code obsolete and allow us to circumvent this
* deprecation.
*/
wchar_t** w_argv = wcharFromChar(argv, argc);
PySys_SetArgv(argc - 1, w_argv + 1);
freeWchar(w_argv, argc);

c_status = PyRun_SimpleString(COMMAND);

Py_FinalizeEx();
Expand Down