🌐 AI搜索 & 代理 主页
Skip to content

Commit b871d35

Browse files
committed
Only init/shutdown Python once
1 parent 5276ac6 commit b871d35

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
public class CallableObject
1111
{
12+
IPythonBaseTypeProvider BaseTypeProvider;
13+
1214
[OneTimeSetUp]
1315
public void SetUp()
1416
{
15-
PythonEngine.Initialize();
1617
using var locals = new PyDict();
1718
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals);
18-
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
19-
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider());
19+
BaseTypeProvider = new CustomBaseTypeProvider(new PyType(locals[CallViaInheritance.BaseClassName]));
20+
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(BaseTypeProvider);
2021
}
2122

2223
[OneTimeTearDown]
2324
public void Dispose()
2425
{
25-
PythonEngine.Shutdown();
26+
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Remove(BaseTypeProvider);
2627
}
28+
2729
[Test]
2830
public void CallMethodMakesObjectCallable()
2931
{
3032
var doubler = new DerivedDoubler();
3133
dynamic applyObjectTo21 = 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
public void CallMethodCanBeInheritedFromPython()
3639
{
3740
var callViaInheritance = new CallViaInheritance();
3841
dynamic applyObjectTo14 = 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
int result = scope.Eval<int>("o.Call(5)");
51-
Assert.AreEqual(105, result);
54+
Assert.That(result, Is.EqualTo(105));
5255
}
5356

5457
class Doubler
@@ -71,16 +74,14 @@ class {BaseClassName}(MyCallableBase): pass
7174
public int Call(int arg) => 3 * arg;
7275
}
7376

74-
class CustomBaseTypeProvider : IPythonBaseTypeProvider
77+
class CustomBaseTypeProvider(PyType BaseClass) : IPythonBaseTypeProvider
7578
{
76-
internal static PyType BaseClass;
77-
7879
public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
7980
{
80-
Assert.Greater(BaseClass.Refcount, 0);
81+
Assert.That(BaseClass.Refcount, Is.GreaterThan(0));
8182
return type != 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
public class ClassManagerTests
88
{
9-
[OneTimeSetUp]
10-
public void SetUp()
11-
{
12-
PythonEngine.Initialize();
13-
}
14-
15-
[OneTimeTearDown]
16-
public void Dispose()
17-
{
18-
PythonEngine.Shutdown();
19-
}
20-
219
[Test]
2210
public void NestedClassDerivingFromParent()
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
var got = 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
new ObjectToEncoderInstanceEncoder<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
var uri = group.TryEncode(new Uri("data:"));
5256
var clrObject = (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
var tuple = 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
var decoder = 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
public void CanDecode()
@@ -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(new PyInt(10), out long longResult));
113-
Assert.AreEqual(42, longResult);
114-
Assert.IsTrue(group.TryDecode(new PyFloat(10), out string strResult));
115-
Assert.AreSame("atad:", strResult);
116-
117-
Assert.IsFalse(group.TryDecode(new PyInt(10), out int _));
118-
}
119-
120-
[SetUp]
121-
public void SetUp()
122-
{
123-
PythonEngine.Initialize();
124-
}
125-
126-
[TearDown]
127-
public void Dispose()
128-
{
129-
PythonEngine.Shutdown();
120+
Assert.Multiple(() =>
121+
{
122+
Assert.That(group.TryDecode(new PyInt(10), out long longResult));
123+
Assert.That(longResult, Is.EqualTo(42));
124+
Assert.That(group.TryDecode(new PyFloat(10), out string strResult));
125+
Assert.That(strResult, Is.SameAs("atad:"));
126+
Assert.That(group.TryDecode(new PyInt(10), out int _), 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
public class Codecs
1010
{
11-
[SetUp]
12-
public void SetUp()
13-
{
14-
PythonEngine.Initialize();
15-
}
16-
17-
[TearDown]
18-
public void Dispose()
19-
{
20-
PythonEngine.Shutdown();
21-
}
22-
2311
[Test]
2412
public void TupleConversionsGeneric()
2513
{
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ namespace Python.EmbeddingTest
88
{
99
public class DynamicTest
1010
{
11-
[OneTimeSetUp]
12-
public void SetUp()
13-
{
14-
PythonEngine.Initialize();
15-
}
16-
17-
[OneTimeTearDown]
18-
public void Dispose()
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
public class ExtensionTypes
1010
{
11-
[OneTimeSetUp]
12-
public void SetUp()
13-
{
14-
PythonEngine.Initialize();
15-
}
16-
17-
[OneTimeTearDown]
18-
public void Dispose()
19-
{
20-
PythonEngine.Shutdown();
21-
}
22-
2311
[Test]
2412
public void WeakrefIsNone_AfterBoundMethodIsGone()
2513
{
2614
using var makeref = Py.Import("weakref").GetAttr("ref");
2715
var boundMethod = new UriBuilder().ToPython().GetAttr(nameof(UriBuilder.GetHashCode));
2816
var weakref = 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
public void GlobalSetup()
1414
{
1515
Finalizer.Instance.ErrorHandler += FinalizerErrorHandler;
16+
PythonEngine.Initialize();
1617
}
1718

1819
private void FinalizerErrorHandler(object sender, Finalizer.ErrorArgs e)

0 commit comments

Comments
 (0)