Skip to content

Commit ae84818

Browse files
committed
Allow the engine to be initialized as a Disposable.
1 parent 098b625 commit ae84818

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/embed_tests/InitializeTest.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,26 @@ public class InitializeTest
1313
public static void LoadSpecificArgs()
1414
{
1515
var args = new[] { "test1", "test2" };
16-
PythonEngine.Initialize(args);
17-
try
16+
using (new PythonEngine(args))
17+
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
1818
{
19-
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
20-
{
21-
Assert.That(argv[0].ToString() == args[0]);
22-
Assert.That(argv[1].ToString() == args[1]);
23-
}
24-
}
25-
finally
26-
{
27-
PythonEngine.Shutdown();
19+
Assert.That(argv[0].ToString() == args[0]);
20+
Assert.That(argv[1].ToString() == args[1]);
2821
}
2922
}
3023

3124
[Test]
3225
public static void LoadDefaultArgs()
3326
{
34-
PythonEngine.Initialize();
35-
try
36-
{
37-
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
38-
{
39-
Assert.That(argv.Length() != 0);
40-
}
41-
}
42-
finally
27+
using (new PythonEngine())
28+
using (var argv = new PyList(Runtime.Runtime.PySys_GetObject("argv")))
4329
{
44-
PythonEngine.Shutdown();
30+
Assert.That(argv.Length() != 0);
4531
}
4632
}
4733

4834
[Test]
49-
public static void Test()
35+
public static void StartAndStopTwice()
5036
{
5137
PythonEngine.Initialize();
5238
PythonEngine.Shutdown();

src/runtime/pythonengine.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,31 @@ namespace Python.Runtime
1010
/// <summary>
1111
/// This class provides the public interface of the Python runtime.
1212
/// </summary>
13-
public class PythonEngine
13+
public class PythonEngine : IDisposable
1414
{
1515
private static DelegateManager delegateManager;
1616
private static bool initialized;
1717

18+
public PythonEngine()
19+
{
20+
Initialize();
21+
}
22+
23+
public PythonEngine(params string[] args)
24+
{
25+
Initialize(args);
26+
}
27+
28+
public PythonEngine(IEnumerable<string> args)
29+
{
30+
Initialize(args);
31+
}
32+
33+
public void Dispose()
34+
{
35+
Shutdown();
36+
}
37+
1838
#region Properties
1939

2040
public static bool IsInitialized
@@ -197,7 +217,8 @@ public static void Initialize(IEnumerable<string> args)
197217
// when it is imported by the CLR extension module.
198218
//====================================================================
199219
#if PYTHON3
200-
public static IntPtr InitExt() {
220+
public static IntPtr InitExt()
221+
{
201222
#elif PYTHON2
202223
public static void InitExt()
203224
{
@@ -551,6 +572,11 @@ public static void SetArgv()
551572
);
552573
}
553574

575+
public static void SetArgv(params string[] argv)
576+
{
577+
SetArgv(argv as IEnumerable<string>);
578+
}
579+
554580
public static void SetArgv(IEnumerable<string> argv)
555581
{
556582
using (GIL())

0 commit comments

Comments
 (0)