Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cac82a6

Browse files
authored
ABI.Initialize gives a helpful message when the TypeOffset interop class is not configured correctly (#1340)
+ Added LoadNativeTypeOffsetClass test * Remove reference to PythonInteropFile
1 parent 1f40564 commit cac82a6

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/embed_tests/Python.EmbeddingTest.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<None Include="fixtures/**/*.py" CopyToOutputDirectory="PreserveNewest" />
1313
</ItemGroup>
1414

15+
<PropertyGroup>
16+
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
17+
</PropertyGroup>
18+
1519
<ItemGroup>
1620
<PackageReference Include="NUnit" Version="3.*" />
1721
<PackageReference Include="NUnit3TestAdapter" Version="3.*">
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
using NUnit.Framework;
9+
10+
using Python.Runtime;
11+
12+
namespace Python.EmbeddingPythonTest
13+
{
14+
public class TestNativeTypeOffset
15+
{
16+
private Py.GILState _gs;
17+
18+
[SetUp]
19+
public void SetUp()
20+
{
21+
_gs = Py.GIL();
22+
}
23+
24+
[TearDown]
25+
public void Dispose()
26+
{
27+
_gs.Dispose();
28+
}
29+
30+
/// <summary>
31+
/// Tests that installation has generated code for NativeTypeOffset and that it can be loaded.
32+
/// </summary>
33+
[Test]
34+
public void LoadNativeTypeOffsetClass()
35+
{
36+
PyObject sys = Py.Import("sys");
37+
string attributeName = "abiflags";
38+
if (sys.HasAttr(attributeName) && !string.IsNullOrEmpty(sys.GetAttr(attributeName).ToString()))
39+
{
40+
string typeName = "Python.Runtime.NativeTypeOffset, Python.Runtime";
41+
Assert.NotNull(Type.GetType(typeName), $"{typeName} does not exist and sys.{attributeName} is not empty");
42+
}
43+
}
44+
}
45+
}

src/runtime/Python.Runtime.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
<DefineConstants>$(DefineConstants);$(ConfiguredConstants)</DefineConstants>
2020
</PropertyGroup>
2121

22-
<ItemGroup Condition=" '$(PythonInteropFile)' != '' ">
23-
<Compile Remove="interop*.cs" />
24-
<Compile Include="interop.cs" />
25-
<Compile Include="$(PythonInteropFile)" />
26-
</ItemGroup>
27-
2822
<ItemGroup>
2923
<None Remove="resources\clr.py" />
3024
<EmbeddedResource Include="resources\clr.py">

src/runtime/native/ABI.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ internal static void Initialize(Version version, BorrowedReference pyType)
2222
thisAssembly.GetType(nativeTypeOffsetClassName, throwOnError: false)
2323
?? thisAssembly.GetType(className, throwOnError: false);
2424
if (typeOffsetsClass is null)
25-
throw new NotSupportedException($"Python ABI v{version} is not supported");
25+
{
26+
var types = thisAssembly.GetTypes().Select(type => type.Name).Where(name => name.StartsWith("TypeOffset"));
27+
string message = $"Searching for {className}, found {string.Join(",", types)}. " +
28+
"If you are building Python.NET from source, make sure you have run 'python setup.py configure' to fill in configured.props";
29+
throw new NotSupportedException($"Python ABI v{version} is not supported: {message}");
30+
}
2631
var typeOffsets = (ITypeOffsets)Activator.CreateInstance(typeOffsetsClass);
2732
TypeOffset.Use(typeOffsets);
2833

0 commit comments

Comments
 (0)