Skip to content

Commit c35d2ad

Browse files
author
denfromufa
authored
read .net system environment arguments
1 parent 93a3785 commit c35d2ad

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/runtime/runtime.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,29 @@ internal static void Initialize()
367367
IntPtr sysargv = Runtime.PyObject_GetAttrString(sysmodule, "argv");
368368
//if sys.argv is available, no need to mess with it
369369
if (sysargv == IntPtr.Zero) {
370-
// Need to add at least the list with empty string to sys.argv,
371-
// this is how python interpreter gets loaded in interactive mode
372-
//PythonEngine.RunString("import sys; setattr(sys,'argv',[''])");
373-
374-
IntPtr pystrEmpty = PyString_FromString("");
375-
IntPtr pylistargv = PyList_New(1);
376-
Runtime.XIncref(pystrEmpty);
377-
int r = Runtime.PyList_SetItem(pylistargv, 0, pystrEmpty);
378-
if (r < 0) {
379-
throw new PythonException();
370+
string[] clrargv;
371+
try {
372+
clrargv = System.Environment.GetCommandLineArgs();
380373
}
381-
PySys_SetArgvEx(0, pylistargv, 0);
374+
catch (NotSupportedException) {
375+
// some platforms do not support argv
376+
// Need to add at least the list with empty string to sys.argv,
377+
// this is how python interpreter gets loaded in interactive mode
378+
clrargv = new string[] { String.Empty };
379+
}
380+
int vi = 0;
381+
int clrargc = clrargv.Length;
382+
IntPtr pylistargv = PyList_New(clrargc);
383+
foreach (string clrargvi in clrargv) {
384+
IntPtr pystrargvi = PyString_FromString(clrargvi);
385+
Runtime.XIncref(pystrargvi);
386+
int r = Runtime.PyList_SetItem(pylistargv, vi, pystrargvi);
387+
vi += 1;
388+
if (r < 0) {
389+
throw new PythonException();
390+
}
391+
}
392+
PySys_SetArgvEx(clrargc, pylistargv, 0);
382393
}
383394
}
384395

0 commit comments

Comments
 (0)