From 1492d7d7c682257833b9f05c217186277a1567d1 Mon Sep 17 00:00:00 2001 From: Victor Nova Date: Wed, 25 May 2022 11:37:48 -0700 Subject: [PATCH 1/3] expose Min/MaxSupportedVersion and IsSupportedVersion on PythonEngine fixes https://github.com/pythonnet/pythonnet/discussions/1724 --- CHANGELOG.md | 1 + src/runtime/PythonEngine.cs | 4 ++++ tests/test_engine.py | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31deb70ac..069629021 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ See [Mixins/collections.py](src/runtime/Mixins/collections.py). and other `PyObject` derived types when called from Python. - .NET classes, that have `__call__` method are callable from Python - `PyIterable` type, that wraps any iterable object in Python +- `PythonEngine` properties for supported Python versions: `MinSupportedVersion`, `MaxSupportedVersion`, and `IsSupportedVersion` ### Changed diff --git a/src/runtime/PythonEngine.cs b/src/runtime/PythonEngine.cs index c4c9fad61..f184ebe88 100644 --- a/src/runtime/PythonEngine.cs +++ b/src/runtime/PythonEngine.cs @@ -127,6 +127,10 @@ public static string PythonPath } } + public static Version MinSupportedVersion => new(3, 7); + public static Version MaxSupportedVersion => new(3, 10, int.MaxValue, int.MaxValue); + public static bool IsSupportedVersion(Version version) => version >= MinSupportedVersion && version <= MaxSupportedVersion; + public static string Version { get { return Marshal.PtrToStringAnsi(Runtime.Py_GetVersion()); } diff --git a/tests/test_engine.py b/tests/test_engine.py index 06a44d561..65aaa3ce8 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -19,6 +19,14 @@ def test_multiple_calls_to_initialize(): assert False # Initialize() raise an exception. +def test_supported_version(): + major, minor, build, *_ = sys.version_info + ver = System.Version(major, minor, build) + assert PythonEngine.IsSupportedVersion(ver) + assert ver >= PythonEngine.MinSupportedVersion + assert ver <= PythonEngine.MaxSupportedVersion + + @pytest.mark.skip(reason="FIXME: test crashes") def test_import_module(): """Test module import.""" From 26d1039c9702746c8deb50db1b2aee34a0c27dab Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Thu, 26 May 2022 20:58:28 +0200 Subject: [PATCH 2/3] Ensure that codecs are definitely clean in tests --- tests/test_codec.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_codec.py b/tests/test_codec.py index 6c74bd8eb..0c1fb44f4 100644 --- a/tests/test_codec.py +++ b/tests/test_codec.py @@ -10,6 +10,7 @@ @pytest.fixture(autouse=True) def reset(): + CodecResetter.Reset() yield CodecResetter.Reset() From 2e1652b8f8878d9db3f1676e3f3a97674fcc3ecb Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Thu, 26 May 2022 21:05:34 +0200 Subject: [PATCH 3/3] Ensure that the RawProxyEncoder is removed again --- tests/test_conversion.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 4de286b14..6693d8000 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -4,7 +4,7 @@ import pytest import System -from Python.Test import ConversionTest, MethodResolutionInt, UnicodeString +from Python.Test import ConversionTest, MethodResolutionInt, UnicodeString, CodecResetter from Python.Runtime import PyObjectConversions from Python.Runtime.Codecs import RawProxyEncoder @@ -659,6 +659,8 @@ def CanEncode(self, clr_type): l.Add(42) assert ob.ListField.Count == 1 + CodecResetter.Reset() + def test_int_param_resolution_required(): """Test resolution of `int` parameters when resolution is needed"""