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

Commitb871d35

Browse files
committed
Only init/shutdown Python once
1 parent5276ac6 commitb871d35

29 files changed

+193
-357
lines changed

‎Directory.Build.props‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AssemblyCopyright>Copyright (c) 2006-2025 The Contributors of the Python.NET Project</AssemblyCopyright>
55
<AssemblyCompany>pythonnet</AssemblyCompany>
66
<AssemblyProduct>Python.NET</AssemblyProduct>
7-
<LangVersion>10.0</LangVersion>
7+
<LangVersion>12.0</LangVersion>
88
<IsPackable>false</IsPackable>
99
<FullVersion>$([System.IO.File]::ReadAllText("$(MSBuildThisFileDirectory)version.txt").Trim())</FullVersion>
1010
<VersionPrefix>$(FullVersion.Split('-', 2)[0])</VersionPrefix>

‎src/embed_tests/CallableObject.cs‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,37 @@ namespace Python.EmbeddingTest
99
{
1010
publicclassCallableObject
1111
{
12+
IPythonBaseTypeProviderBaseTypeProvider;
13+
1214
[OneTimeSetUp]
1315
publicvoidSetUp()
1416
{
15-
PythonEngine.Initialize();
1617
usingvarlocals=newPyDict();
1718
PythonEngine.Exec(CallViaInheritance.BaseClassSource,locals:locals);
18-
CustomBaseTypeProvider.BaseClass=newPyType(locals[CallViaInheritance.BaseClassName]);
19-
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(newCustomBaseTypeProvider());
19+
BaseTypeProvider=newCustomBaseTypeProvider(newPyType(locals[CallViaInheritance.BaseClassName]));
20+
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(BaseTypeProvider);
2021
}
2122

2223
[OneTimeTearDown]
2324
publicvoidDispose()
2425
{
25-
PythonEngine.Shutdown();
26+
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Remove(BaseTypeProvider);
2627
}
28+
2729
[Test]
2830
publicvoidCallMethodMakesObjectCallable()
2931
{
3032
vardoubler=newDerivedDoubler();
3133
dynamicapplyObjectTo21=PythonEngine.Eval("lambda o: o(21)");
32-
Assert.AreEqual(doubler.__call__(21),(int)applyObjectTo21(doubler.ToPython()));
34+
Assert.That((int)applyObjectTo21(doubler.ToPython()),Is.EqualTo(doubler.__call__(21)));
3335
}
36+
3437
[Test]
3538
publicvoidCallMethodCanBeInheritedFromPython()
3639
{
3740
varcallViaInheritance=newCallViaInheritance();
3841
dynamicapplyObjectTo14=PythonEngine.Eval("lambda o: o(14)");
39-
Assert.AreEqual(callViaInheritance.Call(14),(int)applyObjectTo14(callViaInheritance.ToPython()));
42+
Assert.That((int)applyObjectTo14(callViaInheritance.ToPython()),Is.EqualTo(callViaInheritance.Call(14)));
4043
}
4144

4245
[Test]
@@ -48,7 +51,7 @@ public void CanOverwriteCall()
4851
scope.Exec("orig_call = o.Call");
4952
scope.Exec("o.Call = lambda a: orig_call(a*7)");
5053
intresult=scope.Eval<int>("o.Call(5)");
51-
Assert.AreEqual(105,result);
54+
Assert.That(result,Is.EqualTo(105));
5255
}
5356

5457
classDoubler
@@ -71,16 +74,14 @@ class {BaseClassName}(MyCallableBase): pass
7174
publicintCall(intarg)=>3*arg;
7275
}
7376

74-
classCustomBaseTypeProvider:IPythonBaseTypeProvider
77+
classCustomBaseTypeProvider(PyTypeBaseClass):IPythonBaseTypeProvider
7578
{
76-
internalstaticPyTypeBaseClass;
77-
7879
publicIEnumerable<PyType>GetBaseTypes(Typetype,IList<PyType>existingBases)
7980
{
80-
Assert.Greater(BaseClass.Refcount,0);
81+
Assert.That(BaseClass.Refcount,Is.GreaterThan(0));
8182
returntype!=typeof(CallViaInheritance)
8283
?existingBases
83-
:new[]{BaseClass};
84+
:[BaseClass];
8485
}
8586
}
8687
}

‎src/embed_tests/ClassManagerTests.cs‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
66
{
77
publicclassClassManagerTests
88
{
9-
[OneTimeSetUp]
10-
publicvoidSetUp()
11-
{
12-
PythonEngine.Initialize();
13-
}
14-
15-
[OneTimeTearDown]
16-
publicvoidDispose()
17-
{
18-
PythonEngine.Shutdown();
19-
}
20-
219
[Test]
2210
publicvoidNestedClassDerivingFromParent()
2311
{

‎src/embed_tests/CodecGroups.cs‎

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void GetEncodersByType()
2020
};
2121

2222
vargot=group.GetEncoders(typeof(Uri)).ToArray();
23-
CollectionAssert.AreEqual(new[]{encoder1,encoder2},got);
23+
Assert.That(got,Is.EqualTo(new[]{encoder1,encoder2}).AsCollection);
2424
}
2525

2626
[Test]
@@ -31,9 +31,13 @@ public void CanEncode()
3131
newObjectToEncoderInstanceEncoder<Uri>(),
3232
};
3333

34-
Assert.IsTrue(group.CanEncode(typeof(Tuple<int>)));
35-
Assert.IsTrue(group.CanEncode(typeof(Uri)));
36-
Assert.IsFalse(group.CanEncode(typeof(string)));
34+
Assert.Multiple(()=>
35+
{
36+
Assert.That(group.CanEncode(typeof(Tuple<int>)),Is.True);
37+
Assert.That(group.CanEncode(typeof(Uri)),Is.True);
38+
Assert.That(group.CanEncode(typeof(string)),Is.False);
39+
});
40+
3741
}
3842

3943
[Test]
@@ -50,12 +54,12 @@ public void Encodes()
5054

5155
varuri=group.TryEncode(newUri("data:"));
5256
varclrObject=(CLRObject)ManagedType.GetManagedObject(uri);
53-
Assert.AreSame(encoder1,clrObject.inst);
54-
Assert.AreNotSame(encoder2,clrObject.inst);
57+
Assert.That(clrObject.inst,Is.SameAs(encoder1));
58+
Assert.That(clrObject.inst,Is.Not.SameAs(encoder2));
5559

5660
vartuple=group.TryEncode(Tuple.Create(1));
5761
clrObject=(CLRObject)ManagedType.GetManagedObject(tuple);
58-
Assert.AreSame(encoder0,clrObject.inst);
62+
Assert.That(clrObject.inst,Is.SameAs(encoder0));
5963
}
6064

6165
[Test]
@@ -72,11 +76,11 @@ public void GetDecodersByTypes()
7276
};
7377

7478
vardecoder=group.GetDecoder(pyfloat,typeof(string));
75-
Assert.AreSame(decoder2,decoder);
79+
Assert.That(decoder,Is.SameAs(decoder2));
7680
decoder=group.GetDecoder(pystr,typeof(string));
77-
Assert.IsNull(decoder);
81+
Assert.That(decoder,Is.Null);
7882
decoder=group.GetDecoder(pyint,typeof(long));
79-
Assert.AreSame(decoder1,decoder);
83+
Assert.That(decoder,Is.SameAs(decoder1));
8084
}
8185
[Test]
8286
publicvoidCanDecode()
@@ -91,10 +95,14 @@ public void CanDecode()
9195
decoder2,
9296
};
9397

94-
Assert.IsTrue(group.CanDecode(pyint,typeof(long)));
95-
Assert.IsFalse(group.CanDecode(pyint,typeof(int)));
96-
Assert.IsTrue(group.CanDecode(pyfloat,typeof(string)));
97-
Assert.IsFalse(group.CanDecode(pystr,typeof(string)));
98+
Assert.Multiple(()=>
99+
{
100+
Assert.That(group.CanDecode(pyint,typeof(long)));
101+
Assert.That(group.CanDecode(pyint,typeof(int)),Is.False);
102+
Assert.That(group.CanDecode(pyfloat,typeof(string)));
103+
Assert.That(group.CanDecode(pystr,typeof(string)),Is.False);
104+
});
105+
98106
}
99107

100108
[Test]
@@ -109,24 +117,14 @@ public void Decodes()
109117
decoder2,
110118
};
111119

112-
Assert.IsTrue(group.TryDecode(newPyInt(10),outlonglongResult));
113-
Assert.AreEqual(42,longResult);
114-
Assert.IsTrue(group.TryDecode(newPyFloat(10),outstringstrResult));
115-
Assert.AreSame("atad:",strResult);
116-
117-
Assert.IsFalse(group.TryDecode(newPyInt(10),outint_));
118-
}
119-
120-
[SetUp]
121-
publicvoidSetUp()
122-
{
123-
PythonEngine.Initialize();
124-
}
125-
126-
[TearDown]
127-
publicvoidDispose()
128-
{
129-
PythonEngine.Shutdown();
120+
Assert.Multiple(()=>
121+
{
122+
Assert.That(group.TryDecode(newPyInt(10),outlonglongResult));
123+
Assert.That(longResult,Is.EqualTo(42));
124+
Assert.That(group.TryDecode(newPyFloat(10),outstringstrResult));
125+
Assert.That(strResult,Is.SameAs("atad:"));
126+
Assert.That(group.TryDecode(newPyInt(10),outint_),Is.False);
127+
});
130128
}
131129
}
132130
}

‎src/embed_tests/Codecs.cs‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ namespace Python.EmbeddingTest {
88

99
publicclassCodecs
1010
{
11-
[SetUp]
12-
publicvoidSetUp()
13-
{
14-
PythonEngine.Initialize();
15-
}
16-
17-
[TearDown]
18-
publicvoidDispose()
19-
{
20-
PythonEngine.Shutdown();
21-
}
22-
2311
[Test]
2412
publicvoidTupleConversionsGeneric()
2513
{
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ namespace Python.EmbeddingTest
88
{
99
publicclassDynamicTest
1010
{
11-
[OneTimeSetUp]
12-
publicvoidSetUp()
13-
{
14-
PythonEngine.Initialize();
15-
}
16-
17-
[OneTimeTearDown]
18-
publicvoidDispose()
19-
{
20-
PythonEngine.Shutdown();
21-
}
22-
2311
/// <summary>
2412
/// Set the attribute of a PyObject with a .NET object.
2513
/// </summary>

‎src/embed_tests/ExtensionTypes.cs‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,13 @@ namespace Python.EmbeddingTest;
88

99
publicclassExtensionTypes
1010
{
11-
[OneTimeSetUp]
12-
publicvoidSetUp()
13-
{
14-
PythonEngine.Initialize();
15-
}
16-
17-
[OneTimeTearDown]
18-
publicvoidDispose()
19-
{
20-
PythonEngine.Shutdown();
21-
}
22-
2311
[Test]
2412
publicvoidWeakrefIsNone_AfterBoundMethodIsGone()
2513
{
2614
usingvarmakeref=Py.Import("weakref").GetAttr("ref");
2715
varboundMethod=newUriBuilder().ToPython().GetAttr(nameof(UriBuilder.GetHashCode));
2816
varweakref=makeref.Invoke(boundMethod);
2917
boundMethod.Dispose();
30-
Assert.IsTrue(weakref.Invoke().IsNone());
18+
Assert.That(weakref.Invoke().IsNone(),Is.True);
3119
}
3220
}

‎src/embed_tests/GlobalTestsSetup.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public partial class GlobalTestsSetup
1313
publicvoidGlobalSetup()
1414
{
1515
Finalizer.Instance.ErrorHandler+=FinalizerErrorHandler;
16+
PythonEngine.Initialize();
1617
}
1718

1819
privatevoidFinalizerErrorHandler(objectsender,Finalizer.ErrorArgse)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp