Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae84818

Browse files
committed
Allow the engine to be initialized as a Disposable.
1 parent098b625 commitae84818

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
publicstaticvoidLoadSpecificArgs()
1414
{
1515
varargs=new[]{"test1","test2"};
16-
PythonEngine.Initialize(args);
17-
try
16+
using(newPythonEngine(args))
17+
using(varargv=newPyList(Runtime.Runtime.PySys_GetObject("argv")))
1818
{
19-
using(varargv=newPyList(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
publicstaticvoidLoadDefaultArgs()
3326
{
34-
PythonEngine.Initialize();
35-
try
36-
{
37-
using(varargv=newPyList(Runtime.Runtime.PySys_GetObject("argv")))
38-
{
39-
Assert.That(argv.Length()!=0);
40-
}
41-
}
42-
finally
27+
using(newPythonEngine())
28+
using(varargv=newPyList(Runtime.Runtime.PySys_GetObject("argv")))
4329
{
44-
PythonEngine.Shutdown();
30+
Assert.That(argv.Length()!=0);
4531
}
4632
}
4733

4834
[Test]
49-
publicstaticvoidTest()
35+
publicstaticvoidStartAndStopTwice()
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-
publicclassPythonEngine
13+
publicclassPythonEngine:IDisposable
1414
{
1515
privatestaticDelegateManagerdelegateManager;
1616
privatestaticboolinitialized;
1717

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

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

575+
publicstaticvoidSetArgv(paramsstring[]argv)
576+
{
577+
SetArgv(argvasIEnumerable<string>);
578+
}
579+
554580
publicstaticvoidSetArgv(IEnumerable<string>argv)
555581
{
556582
using(GIL())

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp