🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ jobs:
with:
architecture: ${{ matrix.os.platform }}
python-version: ${{ matrix.python }}
cache-python: true
activate-environment: true
enable-cache: true

- name: Synchronize the virtual environment
run: uv sync
run: uv sync --managed-python

- name: Show pyvenv.cfg
run: cat .venv/pyvenv.cfg

- name: Embedding tests (Mono/.NET Framework)
run: dotnet test --runtime any-${{ matrix.os.platform }} --framework net472 --logger "console;verbosity=detailed" src/embed_tests/
Expand All @@ -88,4 +92,8 @@ jobs:
run: pytest --runtime netfx

- name: Python tests run from .NET
# For some reason, it won't find pytest on the Windows + 3.10
# combination, which hints that it does not handle the venv properly in
# this combination.
if: ${{ matrix.os.category != 'windows' || matrix.python != '3.10' }}
run: dotnet test --runtime any-${{ matrix.os.platform }} src/python_tests_runner/
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<AssemblyCopyright>Copyright (c) 2006-2025 The Contributors of the Python.NET Project</AssemblyCopyright>
<AssemblyCompany>pythonnet</AssemblyCompany>
<AssemblyProduct>Python.NET</AssemblyProduct>
<LangVersion>10.0</LangVersion>
<LangVersion>12.0</LangVersion>
<IsPackable>false</IsPackable>
<FullVersion>$([System.IO.File]::ReadAllText("$(MSBuildThisFileDirectory)version.txt").Trim())</FullVersion>
<VersionPrefix>$(FullVersion.Split('-', 2)[0])</VersionPrefix>
<VersionSuffix Condition="$(FullVersion.Contains('-'))">$(FullVersion.Split('-', 2)[1])</VersionSuffix>
<RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
Expand Down
25 changes: 13 additions & 12 deletions src/embed_tests/CallableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,37 @@ namespace Python.EmbeddingTest
{
public class CallableObject
{
IPythonBaseTypeProvider BaseTypeProvider;

[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
using var locals = new PyDict();
PythonEngine.Exec(CallViaInheritance.BaseClassSource, locals: locals);
CustomBaseTypeProvider.BaseClass = new PyType(locals[CallViaInheritance.BaseClassName]);
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(new CustomBaseTypeProvider());
BaseTypeProvider = new CustomBaseTypeProvider(new PyType(locals[CallViaInheritance.BaseClassName]));
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Add(BaseTypeProvider);
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
PythonEngine.InteropConfiguration.PythonBaseTypeProviders.Remove(BaseTypeProvider);
}

[Test]
public void CallMethodMakesObjectCallable()
{
var doubler = new DerivedDoubler();
dynamic applyObjectTo21 = PythonEngine.Eval("lambda o: o(21)");
Assert.AreEqual(doubler.__call__(21), (int)applyObjectTo21(doubler.ToPython()));
Assert.That((int)applyObjectTo21(doubler.ToPython()), Is.EqualTo(doubler.__call__(21)));
}

[Test]
public void CallMethodCanBeInheritedFromPython()
{
var callViaInheritance = new CallViaInheritance();
dynamic applyObjectTo14 = PythonEngine.Eval("lambda o: o(14)");
Assert.AreEqual(callViaInheritance.Call(14), (int)applyObjectTo14(callViaInheritance.ToPython()));
Assert.That((int)applyObjectTo14(callViaInheritance.ToPython()), Is.EqualTo(callViaInheritance.Call(14)));
}

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

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

class CustomBaseTypeProvider : IPythonBaseTypeProvider
class CustomBaseTypeProvider(PyType BaseClass) : IPythonBaseTypeProvider
{
internal static PyType BaseClass;

public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
{
Assert.Greater(BaseClass.Refcount, 0);
Assert.That(BaseClass.Refcount, Is.GreaterThan(0));
return type != typeof(CallViaInheritance)
? existingBases
: new[] { BaseClass };
: [BaseClass];
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/ClassManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace Python.EmbeddingTest
{
public class ClassManagerTests
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void NestedClassDerivingFromParent()
{
Expand Down
62 changes: 30 additions & 32 deletions src/embed_tests/CodecGroups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void GetEncodersByType()
};

var got = group.GetEncoders(typeof(Uri)).ToArray();
CollectionAssert.AreEqual(new[]{encoder1, encoder2}, got);
Assert.That(got, Is.EqualTo(new[] { encoder1, encoder2 }).AsCollection);
}

[Test]
Expand All @@ -31,9 +31,13 @@ public void CanEncode()
new ObjectToEncoderInstanceEncoder<Uri>(),
};

Assert.IsTrue(group.CanEncode(typeof(Tuple<int>)));
Assert.IsTrue(group.CanEncode(typeof(Uri)));
Assert.IsFalse(group.CanEncode(typeof(string)));
Assert.Multiple(() =>
{
Assert.That(group.CanEncode(typeof(Tuple<int>)), Is.True);
Assert.That(group.CanEncode(typeof(Uri)), Is.True);
Assert.That(group.CanEncode(typeof(string)), Is.False);
});

}

[Test]
Expand All @@ -50,12 +54,12 @@ public void Encodes()

var uri = group.TryEncode(new Uri("data:"));
var clrObject = (CLRObject)ManagedType.GetManagedObject(uri);
Assert.AreSame(encoder1, clrObject.inst);
Assert.AreNotSame(encoder2, clrObject.inst);
Assert.That(clrObject.inst, Is.SameAs(encoder1));
Assert.That(clrObject.inst, Is.Not.SameAs(encoder2));

var tuple = group.TryEncode(Tuple.Create(1));
clrObject = (CLRObject)ManagedType.GetManagedObject(tuple);
Assert.AreSame(encoder0, clrObject.inst);
Assert.That(clrObject.inst, Is.SameAs(encoder0));
}

[Test]
Expand All @@ -72,11 +76,11 @@ public void GetDecodersByTypes()
};

var decoder = group.GetDecoder(pyfloat, typeof(string));
Assert.AreSame(decoder2, decoder);
Assert.That(decoder, Is.SameAs(decoder2));
decoder = group.GetDecoder(pystr, typeof(string));
Assert.IsNull(decoder);
Assert.That(decoder, Is.Null);
decoder = group.GetDecoder(pyint, typeof(long));
Assert.AreSame(decoder1, decoder);
Assert.That(decoder, Is.SameAs(decoder1));
}
[Test]
public void CanDecode()
Expand All @@ -91,10 +95,14 @@ public void CanDecode()
decoder2,
};

Assert.IsTrue(group.CanDecode(pyint, typeof(long)));
Assert.IsFalse(group.CanDecode(pyint, typeof(int)));
Assert.IsTrue(group.CanDecode(pyfloat, typeof(string)));
Assert.IsFalse(group.CanDecode(pystr, typeof(string)));
Assert.Multiple(() =>
{
Assert.That(group.CanDecode(pyint, typeof(long)));
Assert.That(group.CanDecode(pyint, typeof(int)), Is.False);
Assert.That(group.CanDecode(pyfloat, typeof(string)));
Assert.That(group.CanDecode(pystr, typeof(string)), Is.False);
});

}

[Test]
Expand All @@ -109,24 +117,14 @@ public void Decodes()
decoder2,
};

Assert.IsTrue(group.TryDecode(new PyInt(10), out long longResult));
Assert.AreEqual(42, longResult);
Assert.IsTrue(group.TryDecode(new PyFloat(10), out string strResult));
Assert.AreSame("atad:", strResult);

Assert.IsFalse(group.TryDecode(new PyInt(10), out int _));
}

[SetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[TearDown]
public void Dispose()
{
PythonEngine.Shutdown();
Assert.Multiple(() =>
{
Assert.That(group.TryDecode(new PyInt(10), out long longResult));
Assert.That(longResult, Is.EqualTo(42));
Assert.That(group.TryDecode(new PyFloat(10), out string strResult));
Assert.That(strResult, Is.SameAs("atad:"));
Assert.That(group.TryDecode(new PyInt(10), out int _), Is.False);
});
}
}
}
10 changes: 2 additions & 8 deletions src/embed_tests/Codecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ namespace Python.EmbeddingTest {

public class Codecs
{
[SetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[TearDown]
public void Dispose()
public void TearDown()
{
PythonEngine.Shutdown();
PyObjectConversions.Reset();
}

[Test]
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/dynamic.cs → src/embed_tests/Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ namespace Python.EmbeddingTest
{
public class DynamicTest
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

/// <summary>
/// Set the attribute of a PyObject with a .NET object.
/// </summary>
Expand Down
12 changes: 0 additions & 12 deletions src/embed_tests/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@

public class Events
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void UsingDoesNotLeak()
{
Expand Down Expand Up @@ -51,7 +39,7 @@
{
internal static int alive;

public event EventHandler LeakEvent;

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, arm64, ubuntu-22.04-arm, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu, x64, ubuntu-22.04, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (windows, x64, windows-latest, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.13)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.10)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.12)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used

Check warning on line 42 in src/embed_tests/Events.cs

View workflow job for this annotation

GitHub Actions / Build and Test (macos, x64, macos-13, 3.11)

The event 'ClassWithEventHandler.LeakEvent' is never used
private Array arr; // dummy array to exacerbate memory leak

public ClassWithEventHandler()
Expand Down
14 changes: 1 addition & 13 deletions src/embed_tests/ExtensionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ namespace Python.EmbeddingTest;

public class ExtensionTypes
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void WeakrefIsNone_AfterBoundMethodIsGone()
{
using var makeref = Py.Import("weakref").GetAttr("ref");
var boundMethod = new UriBuilder().ToPython().GetAttr(nameof(UriBuilder.GetHashCode));
var weakref = makeref.Invoke(boundMethod);
boundMethod.Dispose();
Assert.IsTrue(weakref.Invoke().IsNone());
Assert.That(weakref.Invoke().IsNone(), Is.True);
}
}
1 change: 1 addition & 0 deletions src/embed_tests/GlobalTestsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class GlobalTestsSetup
public void GlobalSetup()
{
Finalizer.Instance.ErrorHandler += FinalizerErrorHandler;
PythonEngine.Initialize();
}

private void FinalizerErrorHandler(object sender, Finalizer.ErrorArgs e)
Expand Down
Loading
Loading