Skip to content

ABI.Initialize gives a helpful message when the TypeOffset interop class is not configured correctly #1340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 4, 2021
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
4 changes: 4 additions & 0 deletions src/embed_tests/Python.EmbeddingTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<None Include="fixtures/**/*.py" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.*" />
<PackageReference Include="NUnit3TestAdapter" Version="3.*">
Expand Down
45 changes: 45 additions & 0 deletions src/embed_tests/TestNativeTypeOffset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingPythonTest
{
public class TestNativeTypeOffset
{
private Py.GILState _gs;

[SetUp]
public void SetUp()
{
_gs = Py.GIL();
}

[TearDown]
public void Dispose()
{
_gs.Dispose();
}

/// <summary>
/// Tests that installation has generated code for NativeTypeOffset and that it can be loaded.
/// </summary>
[Test]
public void LoadNativeTypeOffsetClass()
{
PyObject sys = Py.Import("sys");
string attributeName = "abiflags";
if (sys.HasAttr(attributeName) && !string.IsNullOrEmpty(sys.GetAttr(attributeName).ToString()))
{
string typeName = "Python.Runtime.NativeTypeOffset, Python.Runtime";
Assert.NotNull(Type.GetType(typeName), $"{typeName} does not exist and sys.{attributeName} is not empty");
}
}
}
}
6 changes: 0 additions & 6 deletions src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
<Compile Remove="interop*.cs" />
<Compile Include="interop.cs" />
<Compile Include="$(PythonInteropFile)" />
</ItemGroup>

<ItemGroup>
<None Remove="resources\clr.py" />
<EmbeddedResource Include="resources\clr.py">
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/native/ABI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ internal static void Initialize(Version version, BorrowedReference pyType)
thisAssembly.GetType(nativeTypeOffsetClassName, throwOnError: false)
?? thisAssembly.GetType(className, throwOnError: false);
if (typeOffsetsClass is null)
throw new NotSupportedException($"Python ABI v{version} is not supported");
{
var types = thisAssembly.GetTypes().Select(type => type.Name).Where(name => name.StartsWith("TypeOffset"));
string message = $"Searching for {className}, found {string.Join(",", types)}. " +
"If you are building Python.NET from source, make sure you have run 'python setup.py configure' to fill in configured.props";
throw new NotSupportedException($"Python ABI v{version} is not supported: {message}");
}
var typeOffsets = (ITypeOffsets)Activator.CreateInstance(typeOffsetsClass);
TypeOffset.Use(typeOffsets);

Expand Down