Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add overload for Initialize to pass specific args.
  • Loading branch information
filmor authored and vmuriart committed Jan 31, 2017
commit a0849a9d490bc199752e498946153911bf0e7761
36 changes: 36 additions & 0 deletions src/embed_tests/InitializeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ namespace Python.EmbeddingTest
{
public class InitializeTest
{
[Test]
public static void LoadSpecificArgs()
{
var args = new[] { "test1", "test2" };
PythonEngine.Initialize(args);
try
{
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
{
Assert.That(argv[0].ToString() == args[0]);
Assert.That(argv[1].ToString() == args[1]);
}
}
finally
{
PythonEngine.Shutdown();
}
}

[Test]
public static void LoadDefaultArgs()
{
PythonEngine.Initialize();
try
{
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
{
Assert.That(argv.Length() != 0);
}
}
finally
{
PythonEngine.Shutdown();
}
}

[Test]
public static void Test()
{
Expand Down
19 changes: 16 additions & 3 deletions src/runtime/pythonengine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.IO;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace Python.Runtime
{
Expand Down Expand Up @@ -102,6 +104,11 @@ public static int RunSimpleString(string code)

#endregion

public static void Initialize()
{
Initialize(Enumerable.Empty<string>());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we need to check if sys.argv is non-empty in the case of extending with import clr? @filmor @vmuriart anyway let's see if this passes my tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did not click submit review few weeks ago

/// <summary>
/// Initialize Method
/// </summary>
Expand All @@ -112,7 +119,7 @@ public static int RunSimpleString(string code)
/// first call. It is *not* necessary to hold the Python global
/// interpreter lock (GIL) to call this method.
/// </remarks>
public static void Initialize()
public static void Initialize(IEnumerable<string> args)
{
if (!initialized)
{
Expand All @@ -126,6 +133,9 @@ public static void Initialize()
initialized = true;
Exceptions.Clear();

Py.SetArgv(args);
Py.Throw();

// 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
// are called while the python engine is still running).
Expand Down Expand Up @@ -552,9 +562,12 @@ public static void SetArgv(IEnumerable<string> argv)

internal static void Throw()
{
if (Runtime.PyErr_Occurred() != 0)
using (GIL())
{
throw new PythonException();
if (Runtime.PyErr_Occurred() != 0)
{
throw new PythonException();
}
}
}
}
Expand Down