Skip to content

Commit 42cf8b0

Browse files
Merge pull request #73 from baagaard-usgs/fix-nemesis-argv
FIX: Fix nemesis to remove running in isolation and set argv appropriately
2 parents 4308f76 + 79c8d3a commit 42cf8b0

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

nemesis/nemesis.c

+43-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ struct _inittab inittab[] = {
7474
{ 0, 0 }
7575
};
7676

77+
78+
void
79+
freeWchar(wchar_t* strings[],
80+
const int nstrings) {
81+
int i;
82+
83+
for (i = 0; i < nstrings; ++i) {
84+
PyMem_RawFree(strings[i]);
85+
}
86+
PyMem_RawFree(strings);
87+
}
88+
89+
90+
wchar_t**
91+
wcharFromChar(char* strings[],
92+
const int nstrings) {
93+
int i;
94+
95+
wchar_t** wstrings = PyMem_RawMalloc(sizeof(wchar_t*)*nstrings);
96+
if (!wstrings) { return NULL; }
97+
98+
for (i = 0; i < nstrings; ++i) {
99+
wstrings[i] = Py_DecodeLocale(strings[i], NULL);
100+
if (!wstrings[i]) {
101+
freeWchar(wstrings, i);
102+
return NULL;
103+
}
104+
}
105+
106+
return wstrings;
107+
}
108+
109+
77110
int main(int argc, char* argv[])
78111
{
79112
int c_status;
@@ -106,19 +139,25 @@ int main(int argc, char* argv[])
106139

107140
return Py_RunMain();
108141
} else {
109-
PyConfig_InitIsolatedConfig(&config);
142+
PyConfig_InitPythonConfig(&config);
110143

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

114-
py_status = PyConfig_SetBytesArgv(&config, argc-1, argv+1);
115-
if (PyStatus_Exception(py_status)) { goto exception; }
116-
117147
py_status = Py_InitializeFromConfig(&config);
118148
if (PyStatus_Exception(py_status)) { goto exception; }
119149

120150
PyConfig_Clear(&config);
121151

152+
/* :KLUDGE: PySys_SetArgv() is deprecated in v3.11 and is scheduled
153+
* for removal in v3.13. Switching to Michael Aivazis' current Pyre
154+
* should make this code obsolete and allow us to circumvent this
155+
* deprecation.
156+
*/
157+
wchar_t** w_argv = wcharFromChar(argv, argc);
158+
PySys_SetArgv(argc - 1, w_argv + 1);
159+
freeWchar(w_argv, argc);
160+
122161
c_status = PyRun_SimpleString(COMMAND);
123162

124163
Py_FinalizeEx();

0 commit comments

Comments
 (0)