From 8f66882e3abe91feeaf95819f242327c25fe143e Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Wed, 30 Dec 2020 19:44:20 +0000 Subject: [PATCH 1/7] Initialize gives a helpful message when the TypeOffset interop class is not configured correctly --- src/runtime/native/ABI.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/runtime/native/ABI.cs b/src/runtime/native/ABI.cs index 76337c797..8f163dfdf 100644 --- a/src/runtime/native/ABI.cs +++ b/src/runtime/native/ABI.cs @@ -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 develop' 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); From f82d9633bbbf5e23f2196ac9886111bfea78e7d5 Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Thu, 31 Dec 2020 13:40:14 +0000 Subject: [PATCH 2/7] Changed advice to run 'python setup.py configure' Python.Runtime.csproj includes all interopXX.cs files --- src/runtime/Python.Runtime.csproj | 6 ------ src/runtime/native/ABI.cs | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index acb8efc4e..f18cf7a49 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -19,12 +19,6 @@ $(DefineConstants);$(ConfiguredConstants) - - - - - - diff --git a/src/runtime/native/ABI.cs b/src/runtime/native/ABI.cs index 8f163dfdf..e95b259c5 100644 --- a/src/runtime/native/ABI.cs +++ b/src/runtime/native/ABI.cs @@ -25,7 +25,7 @@ internal static void Initialize(Version version, BorrowedReference pyType) { 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 develop' to fill in configured.props"; + "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); From f59e5d0e392dbba4e319c5c1a44ca33cb08f43fa Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Fri, 1 Jan 2021 01:57:08 +0000 Subject: [PATCH 3/7] Added LoadNativeTypeOffsetClass --- src/embed_tests/Python.EmbeddingTest.csproj | 4 +++ src/embed_tests/TestNativeTypeOffset.cs | 27 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/embed_tests/TestNativeTypeOffset.cs diff --git a/src/embed_tests/Python.EmbeddingTest.csproj b/src/embed_tests/Python.EmbeddingTest.csproj index 8f2db8efe..1bb4fed11 100644 --- a/src/embed_tests/Python.EmbeddingTest.csproj +++ b/src/embed_tests/Python.EmbeddingTest.csproj @@ -12,6 +12,10 @@ + + $(DefineConstants);$(ConfiguredConstants) + + diff --git a/src/embed_tests/TestNativeTypeOffset.cs b/src/embed_tests/TestNativeTypeOffset.cs new file mode 100644 index 000000000..5676b2713 --- /dev/null +++ b/src/embed_tests/TestNativeTypeOffset.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +using NUnit.Framework; + +namespace Python.EmbeddingPythonTest +{ + public class TestNativeTypeOffset + { +#if WINDOWS + // The code for NativeTypeOffset is not generated under Windows because sys.abiflags does not exist (see setup.py) +#else + /// + /// Tests that installation has generated code for NativeTypeOffset and that it can be loaded. + /// + [Test] + public void LoadNativeTypeOffsetClass() + { + new Python.Runtime.NativeTypeOffset(); + } +#endif + } +} From 16042e8e35461086fcc5dd1053d520e999a572fc Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Sun, 3 Jan 2021 14:34:34 +0000 Subject: [PATCH 4/7] Python.Runtime.csproj includes PythonInteropFile --- src/runtime/Python.Runtime.csproj | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index f18cf7a49..12055398d 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -19,6 +19,14 @@ $(DefineConstants);$(ConfiguredConstants) + + + + + + + + From 48c63e779734a2b84fdd441dfba4e9b4c6dddbbe Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Sun, 3 Jan 2021 16:10:27 +0000 Subject: [PATCH 5/7] LoadNativeTypeOffsetClass checks for sys.abiflags --- src/embed_tests/TestNativeTypeOffset.cs | 28 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/embed_tests/TestNativeTypeOffset.cs b/src/embed_tests/TestNativeTypeOffset.cs index 5676b2713..ddfcbe931 100644 --- a/src/embed_tests/TestNativeTypeOffset.cs +++ b/src/embed_tests/TestNativeTypeOffset.cs @@ -7,21 +7,39 @@ using NUnit.Framework; +using Python.Runtime; + namespace Python.EmbeddingPythonTest { public class TestNativeTypeOffset { -#if WINDOWS - // The code for NativeTypeOffset is not generated under Windows because sys.abiflags does not exist (see setup.py) -#else + private Py.GILState _gs; + + [SetUp] + public void SetUp() + { + _gs = Py.GIL(); + } + + [TearDown] + public void Dispose() + { + _gs.Dispose(); + } + /// /// Tests that installation has generated code for NativeTypeOffset and that it can be loaded. /// [Test] public void LoadNativeTypeOffsetClass() { - new Python.Runtime.NativeTypeOffset(); + PyObject sys = Py.Import("sys"); + string attributeName = "abiflags"; + if (sys.HasAttr(attributeName) && !string.IsNullOrEmpty(sys.GetAttr(attributeName).ToString())) + { + string typeName = "Python.Runtime.NativeTypeOffset"; + Assert.NotNull(Type.GetType(typeName), $"{typeName} does not exist and sys.{attributeName} is not empty"); + } } -#endif } } From 3590d06b55d46dd9a609eb363203ecd74be1e292 Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Sun, 3 Jan 2021 16:25:58 +0000 Subject: [PATCH 6/7] Fix typeName in LoadNativeTypeOffsetClass --- src/embed_tests/TestNativeTypeOffset.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/embed_tests/TestNativeTypeOffset.cs b/src/embed_tests/TestNativeTypeOffset.cs index ddfcbe931..03812c6fe 100644 --- a/src/embed_tests/TestNativeTypeOffset.cs +++ b/src/embed_tests/TestNativeTypeOffset.cs @@ -37,7 +37,7 @@ public void LoadNativeTypeOffsetClass() string attributeName = "abiflags"; if (sys.HasAttr(attributeName) && !string.IsNullOrEmpty(sys.GetAttr(attributeName).ToString())) { - string typeName = "Python.Runtime.NativeTypeOffset"; + string typeName = "Python.Runtime.NativeTypeOffset, Python.Runtime"; Assert.NotNull(Type.GetType(typeName), $"{typeName} does not exist and sys.{attributeName} is not empty"); } } From 7701005fb0237c53b9fbcaea39977d438bdae565 Mon Sep 17 00:00:00 2001 From: Tom Minka <8955276+tminka@users.noreply.github.com> Date: Sun, 3 Jan 2021 16:32:26 +0000 Subject: [PATCH 7/7] Remove reference to PythonInteropFile --- src/runtime/Python.Runtime.csproj | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index 12055398d..f18cf7a49 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -19,14 +19,6 @@ $(DefineConstants);$(ConfiguredConstants) - - - - - - - -