From a5e9b55bb0aa6620c59e9f108d17e1a617c07b78 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 16 Jul 2022 12:00:05 -0700 Subject: [PATCH 01/12] Generate XML documentation on build and add to it NuGet package (#1878) also fixed issues with xml docs in the code implements https://github.com/pythonnet/pythonnet/issues/1876 --- src/runtime/AssemblyManager.cs | 2 +- src/runtime/Python.Runtime.csproj | 2 ++ src/runtime/Runtime.cs | 1 + src/runtime/Types/ArrayObject.cs | 2 +- src/runtime/Types/EventBinding.cs | 1 - src/runtime/Types/ReflectedClrType.cs | 1 - 6 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/runtime/AssemblyManager.cs b/src/runtime/AssemblyManager.cs index d09d2d76e..a8bbd1f6c 100644 --- a/src/runtime/AssemblyManager.cs +++ b/src/runtime/AssemblyManager.cs @@ -334,7 +334,7 @@ public static bool IsValidNamespace(string name) } /// - /// Returns an IEnumerable containing the namepsaces exported + /// Returns an enumerable collection containing the namepsaces exported /// by loaded assemblies in the current app domain. /// public static IEnumerable GetNamespaces () diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index fad5b9da8..5072f23cd 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -23,6 +23,8 @@ true snupkg + True + ..\pythonnet.snk true diff --git a/src/runtime/Runtime.cs b/src/runtime/Runtime.cs index 88a8ed173..4dc904f43 100644 --- a/src/runtime/Runtime.cs +++ b/src/runtime/Runtime.cs @@ -99,6 +99,7 @@ internal static int GetRun() internal static bool HostedInPython; internal static bool ProcessIsTerminating; + /// /// Initialize the runtime... /// /// Always call this method from the Main thread. After the diff --git a/src/runtime/Types/ArrayObject.cs b/src/runtime/Types/ArrayObject.cs index bda717e56..b95934baf 100644 --- a/src/runtime/Types/ArrayObject.cs +++ b/src/runtime/Types/ArrayObject.cs @@ -520,7 +520,7 @@ static IntPtr AllocateBufferProcs() #endregion /// - /// + /// /// public static void InitializeSlots(PyType type, ISet initialized, SlotsHolder slotsHolder) { diff --git a/src/runtime/Types/EventBinding.cs b/src/runtime/Types/EventBinding.cs index 9eb2382ec..5c47d4aab 100644 --- a/src/runtime/Types/EventBinding.cs +++ b/src/runtime/Types/EventBinding.cs @@ -70,7 +70,6 @@ public static NewReference nb_inplace_subtract(BorrowedReference ob, BorrowedRef return new NewReference(ob); } - /// public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, BorrowedReference val) => EventObject.tp_descr_set(ds, ob, val); diff --git a/src/runtime/Types/ReflectedClrType.cs b/src/runtime/Types/ReflectedClrType.cs index 2e8f95924..b787939be 100644 --- a/src/runtime/Types/ReflectedClrType.cs +++ b/src/runtime/Types/ReflectedClrType.cs @@ -22,7 +22,6 @@ internal ReflectedClrType(BorrowedReference original) : base(original) { } /// /// /// Returned might be partially initialized. - /// If you need fully initialized type, use /// public static ReflectedClrType GetOrCreate(Type type) { From 85d0ca637d83c7dee2af6fb623dd3d1a05d66374 Mon Sep 17 00:00:00 2001 From: Victor Nova Date: Thu, 4 Aug 2022 11:01:17 -0700 Subject: [PATCH 02/12] mention the need for PythonEngine.Initialize and BeginAllowThreads in the README --- README.rst | 2 ++ src/runtime/README.md | 3 +++ 2 files changed, 5 insertions(+) diff --git a/README.rst b/README.rst index c2c2d53e1..ba4a56fbf 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,8 @@ Embedding Python in .NET (internal, derived from ``MissingMethodException``) upon calling ``Initialize``. Typical values are ``python38.dll`` (Windows), ``libpython3.8.dylib`` (Mac), ``libpython3.8.so`` (most other Unix-like operating systems). +- Then call ``PythonEngine.Initialize()``. If you plan to use Python objects from + multiple threads, also call ``PythonEngine.BeginAllowThreads()``. - All calls to python should be inside a ``using (Py.GIL()) {/* Your code here */}`` block. - Import python modules using ``dynamic mod = Py.Import("mod")``, then diff --git a/src/runtime/README.md b/src/runtime/README.md index ab75280ed..6c8eb2a6c 100644 --- a/src/runtime/README.md +++ b/src/runtime/README.md @@ -8,6 +8,9 @@ integrate Python engine and use Python libraries. (internal, derived from `MissingMethodException`) upon calling `Initialize`. Typical values are `python38.dll` (Windows), `libpython3.8.dylib` (Mac), `libpython3.8.so` (most other *nix). Full path may be required. +- Then call `PythonEngine.Initialize()`. If you plan to [use Python objects from + multiple threads](https://github.com/pythonnet/pythonnet/wiki/Threading), + also call `PythonEngine.BeginAllowThreads()`. - All calls to Python should be inside a `using (Py.GIL()) {/* Your code here */}` block. - Import python modules using `dynamic mod = Py.Import("mod")`, then From 5f63c67af223bc7703e58daa813ffe603bc9bc4f Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 8 Aug 2022 14:02:19 -0700 Subject: [PATCH 03/12] BREAKING: disabled implicit conversion from Python objects implementing sequence protocol to (#1902) .NET arrays when the target .NET type is `System.Object`. The conversion is still attempted when the target type is a `System.Array` fixes https://github.com/pythonnet/pythonnet/issues/1900 --- CHANGELOG.md | 3 +++ src/runtime/Converter.cs | 5 ----- tests/test_conversion.py | 7 +++++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5dd1816..9f0f212e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,9 @@ or the DLL must be loaded in advance. This must be done before calling any other - BREAKING: disabled implicit conversion from C# enums to Python `int` and back. One must now either use enum members (e.g. `MyEnum.Option`), or use enum constructor (e.g. `MyEnum(42)` or `MyEnum(42, True)` when `MyEnum` does not have a member with value 42). +- BREAKING: disabled implicit conversion from Python objects implementing sequence protocol to +.NET arrays when the target .NET type is `System.Object`. The conversion is still attempted when the +target type is a `System.Array`. - Sign Runtime DLL with a strong name - Implement loading through `clr_loader` instead of the included `ClrModule`, enables support for .NET Core diff --git a/src/runtime/Converter.cs b/src/runtime/Converter.cs index e1820f05b..2e0edc3b5 100644 --- a/src/runtime/Converter.cs +++ b/src/runtime/Converter.cs @@ -389,11 +389,6 @@ internal static bool ToManagedValue(BorrowedReference value, Type obType, return true; } - if (Runtime.PySequence_Check(value)) - { - return ToArray(value, typeof(object[]), out result, setError); - } - result = new PyObject(value); return true; } diff --git a/tests/test_conversion.py b/tests/test_conversion.py index a5b4c6fd9..f8d8039c6 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -577,6 +577,13 @@ class Foo(object): ob.ObjectField = Foo assert ob.ObjectField == Foo + class PseudoSeq: + def __getitem__(self, idx): + return 0 + + ob.ObjectField = PseudoSeq() + assert ob.ObjectField.__class__.__name__ == "PseudoSeq" + def test_null_conversion(): """Test null conversion.""" From 090902112f3609d203f0c97a878e276336af336a Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Fri, 12 Aug 2022 06:07:40 +0200 Subject: [PATCH 04/12] Explicit float and int conversion for builtin types (#1904) --- src/runtime/Native/ITypeOffsets.cs | 1 + src/runtime/Native/TypeOffset.cs | 1 + src/runtime/Types/ClassBase.cs | 54 +++++++++++++++++++++++++++++ src/runtime/Types/OperatorMethod.cs | 1 + tests/test_conversion.py | 25 +++++++++++++ 5 files changed, 82 insertions(+) diff --git a/src/runtime/Native/ITypeOffsets.cs b/src/runtime/Native/ITypeOffsets.cs index 2c4fdf59a..0920a312a 100644 --- a/src/runtime/Native/ITypeOffsets.cs +++ b/src/runtime/Native/ITypeOffsets.cs @@ -22,6 +22,7 @@ interface ITypeOffsets int nb_true_divide { get; } int nb_and { get; } int nb_int { get; } + int nb_float { get; } int nb_or { get; } int nb_xor { get; } int nb_lshift { get; } diff --git a/src/runtime/Native/TypeOffset.cs b/src/runtime/Native/TypeOffset.cs index 59c5c3ee0..847f1766d 100644 --- a/src/runtime/Native/TypeOffset.cs +++ b/src/runtime/Native/TypeOffset.cs @@ -31,6 +31,7 @@ static partial class TypeOffset internal static int nb_or { get; private set; } internal static int nb_xor { get; private set; } internal static int nb_int { get; private set; } + internal static int nb_float { get; private set; } internal static int nb_lshift { get; private set; } internal static int nb_rshift { get; private set; } internal static int nb_remainder { get; private set; } diff --git a/src/runtime/Types/ClassBase.cs b/src/runtime/Types/ClassBase.cs index 1e3c325cc..943841e25 100644 --- a/src/runtime/Types/ClassBase.cs +++ b/src/runtime/Types/ClassBase.cs @@ -529,6 +529,37 @@ static NewReference tp_call_impl(BorrowedReference ob, BorrowedReference args, B return callBinder.Invoke(ob, args, kw); } + static NewReference DoConvert(BorrowedReference ob) + { + var self = (CLRObject)GetManagedObject(ob)!; + using var python = self.inst.ToPython(); + return python.NewReferenceOrNull(); + } + + static NewReference DoConvertInt(BorrowedReference ob) + { + var self = (CLRObject)GetManagedObject(ob)!; + return Runtime.PyLong_FromLongLong(Convert.ToInt64(self.inst)); + } + + static NewReference DoConvertUInt(BorrowedReference ob) + { + var self = (CLRObject)GetManagedObject(ob)!; + return Runtime.PyLong_FromUnsignedLongLong(Convert.ToUInt64(self.inst)); + } + + static NewReference DoConvertBooleanInt(BorrowedReference ob) + { + var self = (CLRObject)GetManagedObject(ob)!; + return Runtime.PyInt_FromInt32((bool)self.inst ? 1 : 0); + } + + static NewReference DoConvertFloat(BorrowedReference ob) + { + var self = (CLRObject)GetManagedObject(ob)!; + return Runtime.PyFloat_FromDouble(Convert.ToDouble(self.inst)); + } + static IEnumerable GetCallImplementations(Type type) => type.GetMethods(BindingFlags.Public | BindingFlags.Instance) .Where(m => m.Name == "__call__"); @@ -564,6 +595,29 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH { TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.mp_length, new Interop.B_P(MpLengthSlot.impl), slotsHolder); } + + switch (Type.GetTypeCode(type.Value)) + { + case TypeCode.Boolean: + case TypeCode.SByte: + case TypeCode.Int16: + case TypeCode.Int32: + case TypeCode.Int64: + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder); + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder); + break; + case TypeCode.Byte: + case TypeCode.UInt16: + case TypeCode.UInt32: + case TypeCode.UInt64: + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertUInt), slotsHolder); + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder); + break; + case TypeCode.Double: + case TypeCode.Single: + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder); + break; + } } public virtual bool HasCustomNew() => this.GetType().GetMethod("tp_new") is not null; diff --git a/src/runtime/Types/OperatorMethod.cs b/src/runtime/Types/OperatorMethod.cs index 0c6127f65..88939a51d 100644 --- a/src/runtime/Types/OperatorMethod.cs +++ b/src/runtime/Types/OperatorMethod.cs @@ -53,6 +53,7 @@ static OperatorMethod() ["op_UnaryPlus"] = new SlotDefinition("__pos__", TypeOffset.nb_positive), ["__int__"] = new SlotDefinition("__int__", TypeOffset.nb_int), + ["__float__"] = new SlotDefinition("__float__", TypeOffset.nb_float), }; ComparisonOpMap = new Dictionary { diff --git a/tests/test_conversion.py b/tests/test_conversion.py index f8d8039c6..69e7ec63f 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -720,3 +720,28 @@ def test_intptr_construction(): with pytest.raises(OverflowError): UIntPtr(v) +def test_explicit_conversion(): + from System import ( + Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean + ) + from System import Double, Single + + assert int(Boolean(False)) == 0 + assert int(Boolean(True)) == 1 + + for t in [UInt64, UInt32, UInt16, Byte]: + assert int(t(127)) == 127 + assert float(t(127)) == 127.0 + + for t in [Int64, Int32, Int16, SByte]: + assert int(t(127)) == 127 + assert int(t(-127)) == -127 + assert float(t(127)) == 127.0 + assert float(t(-127)) == -127.0 + + assert int(Int64.MaxValue) == 2**63 - 1 + assert int(Int64.MinValue) == -2**63 + assert int(UInt64.MaxValue) == 2**64 - 1 + + for t in [Single, Double]: + assert float(t(0.125)) == 0.125 From bd48dc118c62993307ad914f1776bd964a559563 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sat, 13 Aug 2022 23:43:38 +0200 Subject: [PATCH 05/12] Fill nb_index slot for integer types (#1907) --- src/runtime/Native/ITypeOffsets.cs | 1 + src/runtime/Native/TypeOffset.cs | 1 + src/runtime/Types/ClassBase.cs | 3 +++ src/runtime/Types/OperatorMethod.cs | 1 + src/runtime/Util/OpsHelper.cs | 6 +++++- tests/test_conversion.py | 13 ++++++++++--- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/runtime/Native/ITypeOffsets.cs b/src/runtime/Native/ITypeOffsets.cs index 0920a312a..7d71b4b91 100644 --- a/src/runtime/Native/ITypeOffsets.cs +++ b/src/runtime/Native/ITypeOffsets.cs @@ -31,6 +31,7 @@ interface ITypeOffsets int nb_invert { get; } int nb_inplace_add { get; } int nb_inplace_subtract { get; } + int nb_index { get; } int ob_size { get; } int ob_type { get; } int qualname { get; } diff --git a/src/runtime/Native/TypeOffset.cs b/src/runtime/Native/TypeOffset.cs index 847f1766d..803b86bfa 100644 --- a/src/runtime/Native/TypeOffset.cs +++ b/src/runtime/Native/TypeOffset.cs @@ -38,6 +38,7 @@ static partial class TypeOffset internal static int nb_invert { get; private set; } internal static int nb_inplace_add { get; private set; } internal static int nb_inplace_subtract { get; private set; } + internal static int nb_index { get; private set; } internal static int ob_size { get; private set; } internal static int ob_type { get; private set; } internal static int qualname { get; private set; } diff --git a/src/runtime/Types/ClassBase.cs b/src/runtime/Types/ClassBase.cs index 943841e25..7296a1900 100644 --- a/src/runtime/Types/ClassBase.cs +++ b/src/runtime/Types/ClassBase.cs @@ -604,6 +604,7 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH case TypeCode.Int32: case TypeCode.Int64: TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder); + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_index, new Interop.B_N(DoConvertInt), slotsHolder); TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder); break; case TypeCode.Byte: @@ -611,10 +612,12 @@ public virtual void InitializeSlots(BorrowedReference pyType, SlotsHolder slotsH case TypeCode.UInt32: case TypeCode.UInt64: TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertUInt), slotsHolder); + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_index, new Interop.B_N(DoConvertUInt), slotsHolder); TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder); break; case TypeCode.Double: case TypeCode.Single: + TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_int, new Interop.B_N(DoConvertInt), slotsHolder); TypeManager.InitializeSlotIfEmpty(pyType, TypeOffset.nb_float, new Interop.B_N(DoConvertFloat), slotsHolder); break; } diff --git a/src/runtime/Types/OperatorMethod.cs b/src/runtime/Types/OperatorMethod.cs index 88939a51d..e3cc23370 100644 --- a/src/runtime/Types/OperatorMethod.cs +++ b/src/runtime/Types/OperatorMethod.cs @@ -54,6 +54,7 @@ static OperatorMethod() ["__int__"] = new SlotDefinition("__int__", TypeOffset.nb_int), ["__float__"] = new SlotDefinition("__float__", TypeOffset.nb_float), + ["__index__"] = new SlotDefinition("__index__", TypeOffset.nb_index), }; ComparisonOpMap = new Dictionary { diff --git a/src/runtime/Util/OpsHelper.cs b/src/runtime/Util/OpsHelper.cs index 12d0c7aa6..7c35f27eb 100644 --- a/src/runtime/Util/OpsHelper.cs +++ b/src/runtime/Util/OpsHelper.cs @@ -82,10 +82,14 @@ internal static class EnumOps where T : Enum { [ForbidPythonThreads] #pragma warning disable IDE1006 // Naming Styles - must match Python - public static PyInt __int__(T value) + public static PyInt __index__(T value) #pragma warning restore IDE1006 // Naming Styles => typeof(T).GetEnumUnderlyingType() == typeof(UInt64) ? new PyInt(Convert.ToUInt64(value)) : new PyInt(Convert.ToInt64(value)); + [ForbidPythonThreads] +#pragma warning disable IDE1006 // Naming Styles - must match Python + public static PyInt __int__(T value) => __index__(value); +#pragma warning restore IDE1006 // Naming Styles } } diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 69e7ec63f..bb686dd52 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -721,6 +721,7 @@ def test_intptr_construction(): UIntPtr(v) def test_explicit_conversion(): + from operator import index from System import ( Int64, UInt64, Int32, UInt32, Int16, UInt16, Byte, SByte, Boolean ) @@ -730,18 +731,24 @@ def test_explicit_conversion(): assert int(Boolean(True)) == 1 for t in [UInt64, UInt32, UInt16, Byte]: + assert index(t(127)) == 127 assert int(t(127)) == 127 assert float(t(127)) == 127.0 for t in [Int64, Int32, Int16, SByte]: + assert index(t(127)) == 127 + assert index(t(-127)) == -127 assert int(t(127)) == 127 assert int(t(-127)) == -127 assert float(t(127)) == 127.0 assert float(t(-127)) == -127.0 - assert int(Int64.MaxValue) == 2**63 - 1 - assert int(Int64.MinValue) == -2**63 - assert int(UInt64.MaxValue) == 2**64 - 1 + assert int(Int64(Int64.MaxValue)) == 2**63 - 1 + assert int(Int64(Int64.MinValue)) == -2**63 + assert int(UInt64(UInt64.MaxValue)) == 2**64 - 1 for t in [Single, Double]: assert float(t(0.125)) == 0.125 + assert int(t(123.4)) == 123 + with pytest.raises(TypeError): + index(t(123.4)) From 4241493468e1e939b7ab5670118ead68bc7a9e07 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 15 Sep 2022 21:08:45 -0700 Subject: [PATCH 06/12] fixed use of the process handle after Process instance goes out of scope (#1940) fixes https://github.com/pythonnet/pythonnet/issues/1939 --- src/runtime/Native/LibraryLoader.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/Native/LibraryLoader.cs b/src/runtime/Native/LibraryLoader.cs index 6d67bea61..05f960481 100644 --- a/src/runtime/Native/LibraryLoader.cs +++ b/src/runtime/Native/LibraryLoader.cs @@ -137,17 +137,17 @@ public IntPtr GetFunction(IntPtr hModule, string procedureName) static IntPtr[] GetAllModules() { - var self = Process.GetCurrentProcess().Handle; + using var self = Process.GetCurrentProcess(); uint bytes = 0; var result = new IntPtr[0]; - if (!EnumProcessModules(self, result, bytes, out var needsBytes)) + if (!EnumProcessModules(self.Handle, result, bytes, out var needsBytes)) throw new Win32Exception(); while (bytes < needsBytes) { bytes = needsBytes; result = new IntPtr[bytes / IntPtr.Size]; - if (!EnumProcessModules(self, result, bytes, out needsBytes)) + if (!EnumProcessModules(self.Handle, result, bytes, out needsBytes)) throw new Win32Exception(); } return result.Take((int)(needsBytes / IntPtr.Size)).ToArray(); From bdf671eebaa994908d905ba10f298ffcca71785d Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 15 Sep 2022 21:09:50 -0700 Subject: [PATCH 07/12] fixed new line character at the end of informational version (#1941) --- Directory.Build.props | 2 +- pythonnet.sln | 1 + tests/test_module.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 8c5b53685..72a519f4f 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -6,7 +6,7 @@ Python.NET 10.0 false - $([System.IO.File]::ReadAllText("version.txt")) + $([System.IO.File]::ReadAllText("version.txt").Trim()) $(FullVersion.Split('-', 2)[0]) $(FullVersion.Split('-', 2)[1]) diff --git a/pythonnet.sln b/pythonnet.sln index eb97cfbd0..d1a47892e 100644 --- a/pythonnet.sln +++ b/pythonnet.sln @@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Repo", "Repo", "{441A0123-F CHANGELOG.md = CHANGELOG.md LICENSE = LICENSE README.rst = README.rst + version.txt = version.txt EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CI", "CI", "{D301657F-5EAF-4534-B280-B858D651B2E5}" diff --git a/tests/test_module.py b/tests/test_module.py index 4e1a1a1ef..ddfa7bb36 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -31,6 +31,7 @@ def test_import_clr(): def test_version_clr(): import clr assert clr.__version__ >= "3.0.0" + assert clr.__version__[-1] != "\n" def test_preload_var(): From 3f124699e4bdcf0b0fd5904c5b7f4cf77d601064 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Fri, 16 Sep 2022 21:21:18 +0200 Subject: [PATCH 08/12] Bump clr-loader dependency to 0.2.3 and adjust interface - Supports loading without explicitly specifying the runtime config now - Exposes information on the loaded runtime --- pyproject.toml | 2 +- pythonnet/__init__.py | 21 ++++++++++++++------- tests/conftest.py | 10 ++++++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5ee89d3b7..39c7c14fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ license = {text = "MIT"} readme = "README.rst" dependencies = [ - "clr_loader>=0.1.7" + "clr_loader>=0.2.2,<0.3.0" ] classifiers = [ diff --git a/pythonnet/__init__.py b/pythonnet/__init__.py index 9876a0bec..c847c4c74 100644 --- a/pythonnet/__init__.py +++ b/pythonnet/__init__.py @@ -1,12 +1,12 @@ import sys from pathlib import Path -from typing import Dict, Optional, Union +from typing import Dict, Optional, Union, Any import clr_loader __all__ = ["set_runtime", "set_runtime_from_env", "load"] _RUNTIME: Optional[clr_loader.Runtime] = None -_LOADER_ASSEMBLY: Optional[clr_loader.wrappers.Assembly] = None +_LOADER_ASSEMBLY: Optional[clr_loader.Assembly] = None _LOADED: bool = False @@ -27,6 +27,13 @@ def set_runtime(runtime: Union[clr_loader.Runtime, str], **params: str) -> None: _RUNTIME = runtime +def get_runtime_info() -> Optional[clr_loader.RuntimeInfo]: + if _RUNTIME is None: + return None + else: + return _RUNTIME.info() + + def _get_params_from_env(prefix: str) -> Dict[str, str]: from os import environ @@ -43,7 +50,7 @@ def _get_params_from_env(prefix: str) -> Dict[str, str]: def _create_runtime_from_spec( - spec: str, params: Optional[Dict[str, str]] = None + spec: str, params: Optional[Dict[str, Any]] = None ) -> clr_loader.Runtime: if spec == "default": if sys.platform == "win32": @@ -109,9 +116,9 @@ def load( dll_path = Path(__file__).parent / "runtime" / "Python.Runtime.dll" - _LOADER_ASSEMBLY = _RUNTIME.get_assembly(str(dll_path)) + _LOADER_ASSEMBLY = assembly = _RUNTIME.get_assembly(str(dll_path)) + func = assembly.get_function("Python.Runtime.Loader.Initialize") - func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"] if func(b"") != 0: raise RuntimeError("Failed to initialize Python.Runtime.dll") @@ -125,12 +132,12 @@ def unload() -> None: global _RUNTIME, _LOADER_ASSEMBLY if _LOADER_ASSEMBLY is not None: - func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Shutdown"] + func = _LOADER_ASSEMBLY.get_function("Python.Runtime.Loader.Shutdown") if func(b"full_shutdown") != 0: raise RuntimeError("Failed to call Python.NET shutdown") _LOADER_ASSEMBLY = None if _RUNTIME is not None: - # TODO: Add explicit `close` to clr_loader + _RUNTIME.shutdown() _RUNTIME = None diff --git a/tests/conftest.py b/tests/conftest.py index fcd1d224a..e61e3680e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -53,10 +53,12 @@ def pytest_configure(config): runtime_params = {} if runtime_opt == "coreclr": - fw = "net6.0" - runtime_params["runtime_config"] = str( - bin_path / "Python.Test.runtimeconfig.json" - ) + # This is optional now: + # + # fw = "net6.0" + # runtime_params["runtime_config"] = str( + # bin_path / "Python.Test.runtimeconfig.json" + # ) collect_ignore.append("domain_tests/test_domain_reload.py") else: domain_tests_dir = cwd / "domain_tests" From db52ddef247cdc008bae18b5ad0fee2f1c690b21 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Fri, 16 Sep 2022 21:34:45 +0200 Subject: [PATCH 09/12] Improve error message if the runtime fails to load --- pythonnet/__init__.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/pythonnet/__init__.py b/pythonnet/__init__.py index c847c4c74..2cde01233 100644 --- a/pythonnet/__init__.py +++ b/pythonnet/__init__.py @@ -28,6 +28,8 @@ def set_runtime(runtime: Union[clr_loader.Runtime, str], **params: str) -> None: def get_runtime_info() -> Optional[clr_loader.RuntimeInfo]: + """Retrieve information on the configured runtime""" + if _RUNTIME is None: return None else: @@ -52,7 +54,9 @@ def _get_params_from_env(prefix: str) -> Dict[str, str]: def _create_runtime_from_spec( spec: str, params: Optional[Dict[str, Any]] = None ) -> clr_loader.Runtime: + was_default = False if spec == "default": + was_default = True if sys.platform == "win32": spec = "netfx" else: @@ -60,14 +64,29 @@ def _create_runtime_from_spec( params = params or _get_params_from_env(spec) - if spec == "netfx": - return clr_loader.get_netfx(**params) - elif spec == "mono": - return clr_loader.get_mono(**params) - elif spec == "coreclr": - return clr_loader.get_coreclr(**params) - else: - raise RuntimeError(f"Invalid runtime name: '{spec}'") + try: + if spec == "netfx": + return clr_loader.get_netfx(**params) + elif spec == "mono": + return clr_loader.get_mono(**params) + elif spec == "coreclr": + return clr_loader.get_coreclr(**params) + else: + raise RuntimeError(f"Invalid runtime name: '{spec}'") + except Exception as exc: + if was_default: + raise RuntimeError( + f"""Failed to create a default .NET runtime, which would + have been "{spec}" on this system. Either install a + compatible runtime or configure it explicitly via + `set_runtime` or the `PYTHONNET_*` environment variables + (see set_runtime_from_env).""" + ) from exc + else: + raise RuntimeError( + f"""Failed to create a .NET runtime ({spec}) using the + parameters {params}.""" + ) from exc def set_runtime_from_env() -> None: @@ -92,9 +111,7 @@ def set_runtime_from_env() -> None: set_runtime(runtime) -def load( - runtime: Union[clr_loader.Runtime, str, None] = None, **params: str -) -> None: +def load(runtime: Union[clr_loader.Runtime, str, None] = None, **params: str) -> None: """Load Python.NET in the specified runtime The same parameters as for `set_runtime` can be used. By default, From fd8fd3b88edbc59237a9c00176006657a1a4dc63 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sat, 17 Sep 2022 08:55:28 +0200 Subject: [PATCH 10/12] Fix manifest so we can build with python -m build --- MANIFEST.in | 1 + 1 file changed, 1 insertion(+) diff --git a/MANIFEST.in b/MANIFEST.in index 6458d5778..71473c2c3 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,6 +1,7 @@ graft src/runtime prune src/runtime/obj prune src/runtime/bin +include src/pythonnet.snk include Directory.Build.* include pythonnet.sln include version.txt From 91d3e55f965ae7fcaf9845d48c3076149dd5a712 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 17 Sep 2022 01:48:28 -0700 Subject: [PATCH 11/12] reenabled test for https://github.com/pythonnet/pythonnet/issues/1256 : NoStackOverflowOnSystemType (#1943) --- src/embed_tests/Codecs.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/embed_tests/Codecs.cs b/src/embed_tests/Codecs.cs index c9e83f03a..be416bc15 100644 --- a/src/embed_tests/Codecs.cs +++ b/src/embed_tests/Codecs.cs @@ -285,7 +285,6 @@ public void IterableDecoderTest() } // regression for https://github.com/pythonnet/pythonnet/issues/1427 - [Ignore("https://github.com/pythonnet/pythonnet/issues/1256")] [Test] public void PythonRegisteredDecoder_NoStackOverflowOnSystemType() { From f1ef11de8e0e47004c51e4419b419b12976ff195 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Sun, 18 Sep 2022 13:06:06 +0200 Subject: [PATCH 12/12] Bump release candidate version --- version.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.txt b/version.txt index dc72b3783..916e2438f 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -3.0.0-rc4 +3.0.0-rc5