Skip to content

Commit eca5ac9

Browse files
filmorvmuriart
authored andcommitted
Implement Py.SetArgv.
1 parent f7f2fc0 commit eca5ac9

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/runtime/pythonengine.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public class KeywordArguments : PyDict
500500
public static KeywordArguments kw(params object[] kv)
501501
{
502502
var dict = new KeywordArguments();
503-
if (kv.Length%2 != 0)
503+
if (kv.Length % 2 != 0)
504504
throw new ArgumentException("Must have an equal number of keys and values");
505505
for (int i = 0; i < kv.Length; i += 2)
506506
{
@@ -521,5 +521,33 @@ public static PyObject Import(string name)
521521
{
522522
return PythonEngine.ImportModule(name);
523523
}
524+
525+
public static void SetArgv()
526+
{
527+
IEnumerable<string> args;
528+
try
529+
{
530+
args = Environment.GetCommandLineArgs();
531+
}
532+
catch (NotSupportedException)
533+
{
534+
args = Enumerable.Empty<string>();
535+
}
536+
537+
SetArgv(
538+
new[] { "" }.Concat(
539+
Environment.GetCommandLineArgs().Skip(1)
540+
)
541+
);
542+
}
543+
544+
public static void SetArgv(IEnumerable<string> argv)
545+
{
546+
using (GIL())
547+
{
548+
var arr = argv.ToArray();
549+
Runtime.PySys_SetArgvEx(arr.Length, arr, 0);
550+
}
551+
}
524552
}
525553
}

src/runtime/runtime.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,11 +2027,26 @@ internal unsafe static extern IntPtr
20272027
internal unsafe static extern IntPtr
20282028
PyImport_GetModuleDict();
20292029

2030-
2030+
#if PYTHON3
20312031
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
20322032
ExactSpelling = true, CharSet = CharSet.Ansi)]
20332033
internal unsafe static extern void
2034-
PySys_SetArgv(int argc, IntPtr argv);
2034+
PySys_SetArgvEx(
2035+
int argc,
2036+
[MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)]
2037+
string[] argv,
2038+
int updatepath
2039+
);
2040+
#elif PYTHON2
2041+
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
2042+
ExactSpelling = true, CharSet = CharSet.Ansi)]
2043+
internal unsafe static extern void
2044+
PySys_SetArgvEx(
2045+
int argc,
2046+
string[] argv,
2047+
int updatepath
2048+
);
2049+
#endif
20352050

20362051
[DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
20372052
ExactSpelling = true, CharSet = CharSet.Ansi)]

0 commit comments

Comments
 (0)