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

Commit098b625

Browse files
committed
Add overload for Initialize to pass specific args.
1 parent4ab02e5 commit098b625

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

‎src/embed_tests/InitializeTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,42 @@ namespace Python.EmbeddingTest
99
{
1010
publicclassInitializeTest
1111
{
12+
[Test]
13+
publicstaticvoidLoadSpecificArgs()
14+
{
15+
varargs=new[]{"test1","test2"};
16+
PythonEngine.Initialize(args);
17+
try
18+
{
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();
28+
}
29+
}
30+
31+
[Test]
32+
publicstaticvoidLoadDefaultArgs()
33+
{
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
43+
{
44+
PythonEngine.Shutdown();
45+
}
46+
}
47+
1248
[Test]
1349
publicstaticvoidTest()
1450
{

‎src/runtime/Python.Runtime.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<ProjectDefaultTargets="Build"xmlns="http://schemas.microsoft.com/developer/msbuild/2003"ToolsVersion="4.0">
33
<PropertyGroup>
44
<ConfigurationCondition=" '$(Configuration)' == ''">Debug</Configuration>
@@ -92,6 +92,7 @@
9292
<Optimize>false</Optimize>
9393
<DebugType>full</DebugType>
9494
<PlatformTarget>x64</PlatformTarget>
95+
<DefineConstants>TRACE;DEBUG;PYTHON3;PYTHON35;UCS2</DefineConstants>
9596
</PropertyGroup>
9697
<Choose>
9798
<WhenCondition=" '$(Configuration)'=='DebugMono'">
@@ -208,4 +209,4 @@
208209
<CopySourceFiles="$(TargetAssembly)"DestinationFolder="$(PythonBuildDir)" />
209210
<CopySourceFiles="$(TargetAssemblyPdb)"Condition="Exists('$(TargetAssemblyPdb)')"DestinationFolder="$(PythonBuildDir)" />
210211
</Target>
211-
</Project>
212+
</Project>

‎src/runtime/pythonengine.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
usingSystem;
1+
usingSystem;
22
usingSystem.IO;
33
usingSystem.Threading;
44
usingSystem.Reflection;
5+
usingSystem.Collections.Generic;
6+
usingSystem.Linq;
57

68
namespacePython.Runtime
79
{
@@ -102,6 +104,11 @@ public static int RunSimpleString(string code)
102104

103105
#endregion
104106

107+
publicstaticvoidInitialize()
108+
{
109+
Initialize(Enumerable.Empty<string>());
110+
}
111+
105112
/// <summary>
106113
/// Initialize Method
107114
/// </summary>
@@ -112,7 +119,7 @@ public static int RunSimpleString(string code)
112119
/// first call. It is *not* necessary to hold the Python global
113120
/// interpreter lock (GIL) to call this method.
114121
/// </remarks>
115-
publicstaticvoidInitialize()
122+
publicstaticvoidInitialize(IEnumerable<string>args)
116123
{
117124
if(!initialized)
118125
{
@@ -126,6 +133,9 @@ public static void Initialize()
126133
initialized=true;
127134
Exceptions.Clear();
128135

136+
Py.SetArgv(args);
137+
Py.Throw();
138+
129139
// register the atexit callback (this doesn't use Py_AtExit as the C atexit
130140
// callbacks are called after python is fully finalized but the python ones
131141
// are called while the python engine is still running).
@@ -552,9 +562,12 @@ public static void SetArgv(IEnumerable<string> argv)
552562

553563
internalstaticvoidThrow()
554564
{
555-
if(Runtime.PyErr_Occurred()!=0)
565+
using(GIL())
556566
{
557-
thrownewPythonException();
567+
if(Runtime.PyErr_Occurred()!=0)
568+
{
569+
thrownewPythonException();
570+
}
558571
}
559572
}
560573
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp