Skip to content

Commit faf5faf

Browse files
filmorvmuriart
authored andcommitted
Fix the issue of sys.argv being cleared on import clr.
Closes #404.
1 parent 012b488 commit faf5faf

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/runtime/pythonengine.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ public static int RunSimpleString(string code)
122122

123123
public static void Initialize()
124124
{
125-
Initialize(Enumerable.Empty<string>());
125+
Initialize(setSysArgv: true);
126+
}
127+
128+
public static void Initialize(bool setSysArgv = true)
129+
{
130+
Initialize(Enumerable.Empty<string>(), setSysArgv: setSysArgv);
126131
}
127132

128133
/// <summary>
@@ -134,7 +139,7 @@ public static void Initialize()
134139
/// first call. It is *not* necessary to hold the Python global
135140
/// interpreter lock (GIL) to call this method.
136141
/// </remarks>
137-
public static void Initialize(IEnumerable<string> args)
142+
public static void Initialize(IEnumerable<string> args, bool setSysArgv = true)
138143
{
139144
if (!initialized)
140145
{
@@ -148,7 +153,8 @@ public static void Initialize(IEnumerable<string> args)
148153
initialized = true;
149154
Exceptions.Clear();
150155

151-
Py.SetArgv(args);
156+
if (setSysArgv)
157+
Py.SetArgv(args);
152158

153159
// register the atexit callback (this doesn't use Py_AtExit as the C atexit
154160
// callbacks are called after python is fully finalized but the python ones
@@ -213,7 +219,7 @@ public static void InitExt()
213219
{
214220
try
215221
{
216-
Initialize();
222+
Initialize(setSysArgv: false);
217223

218224
// Trickery - when the import hook is installed into an already
219225
// running Python, the standard import machinery is still in

src/tests/test_sysargv.py

-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
import sys
66

7-
import pytest
8-
97
from ._compat import check_output
108

119

12-
@pytest.mark.xfail(reason="argv being reset on import clr. See gh#404")
1310
def test_sys_argv_state(filepath):
1411
"""Test sys.argv state doesn't change after clr import.
1512
To better control the arguments being passed, test on a fresh python

0 commit comments

Comments
 (0)