From 9e1a0e482224f4de40547a47800f843c39c75e03 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Fri, 24 Feb 2017 15:18:18 +0100 Subject: [PATCH] Fix the issue of sys.argv being cleared on import clr. Closes #404. --- src/runtime/pythonengine.cs | 14 ++++++++++---- src/tests/test_sysargv.py | 3 --- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/runtime/pythonengine.cs b/src/runtime/pythonengine.cs index accd349c5..3a227479a 100644 --- a/src/runtime/pythonengine.cs +++ b/src/runtime/pythonengine.cs @@ -122,7 +122,12 @@ public static int RunSimpleString(string code) public static void Initialize() { - Initialize(Enumerable.Empty()); + Initialize(setSysArgv: true); + } + + public static void Initialize(bool setSysArgv = true) + { + Initialize(Enumerable.Empty(), setSysArgv: setSysArgv); } /// @@ -134,7 +139,7 @@ public static void Initialize() /// first call. It is *not* necessary to hold the Python global /// interpreter lock (GIL) to call this method. /// - public static void Initialize(IEnumerable args) + public static void Initialize(IEnumerable args, bool setSysArgv = true) { if (!initialized) { @@ -148,7 +153,8 @@ public static void Initialize(IEnumerable args) initialized = true; Exceptions.Clear(); - Py.SetArgv(args); + if (setSysArgv) + Py.SetArgv(args); // register the atexit callback (this doesn't use Py_AtExit as the C atexit // callbacks are called after python is fully finalized but the python ones @@ -213,7 +219,7 @@ public static void InitExt() { try { - Initialize(); + Initialize(setSysArgv: false); // Trickery - when the import hook is installed into an already // running Python, the standard import machinery is still in diff --git a/src/tests/test_sysargv.py b/src/tests/test_sysargv.py index 2c381070b..d86aa1c1d 100644 --- a/src/tests/test_sysargv.py +++ b/src/tests/test_sysargv.py @@ -4,12 +4,9 @@ import sys -import pytest - from ._compat import check_output -@pytest.mark.xfail(reason="argv being reset on import clr. See gh#404") def test_sys_argv_state(filepath): """Test sys.argv state doesn't change after clr import. To better control the arguments being passed, test on a fresh python