diff --git a/src/embed_tests/ClassManagerTests.cs b/src/embed_tests/ClassManagerTests.cs index dcdf66edb..2fd38f272 100644 --- a/src/embed_tests/ClassManagerTests.cs +++ b/src/embed_tests/ClassManagerTests.cs @@ -1085,6 +1085,33 @@ def is_enum_value_defined(): } } + [Test] + public void TruthinessCanBeCheckedForTypes() + { + using (Py.GIL()) + { + var module = PyModule.FromString("TruthinessCanBeCheckedForTypes", $@" +from clr import AddReference +AddReference(""Python.EmbeddingTest"") + +from Python.EmbeddingTest import * + +def throw_if_falsy(): + if not ClassManagerTests: + raise Exception(""ClassManagerTests is falsy"") + +def throw_if_not_truthy(): + if ClassManagerTests: + return + raise Exception(""ClassManagerTests is not truthy"") +"); + + // Types are always truthy + Assert.DoesNotThrow(() => module.InvokeMethod("throw_if_falsy")); + Assert.DoesNotThrow(() => module.InvokeMethod("throw_if_not_truthy")); + } + } + private static TestCaseData[] IDictionaryContainsTestCases => [ new(typeof(TestDictionary)), diff --git a/src/perf_tests/Python.PerformanceTests.csproj b/src/perf_tests/Python.PerformanceTests.csproj index 99f447f56..ee239ff12 100644 --- a/src/perf_tests/Python.PerformanceTests.csproj +++ b/src/perf_tests/Python.PerformanceTests.csproj @@ -13,7 +13,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + compile @@ -25,7 +25,7 @@ - + diff --git a/src/runtime/Native/ITypeOffsets.cs b/src/runtime/Native/ITypeOffsets.cs index 2c4fdf59a..fb65e76f8 100644 --- a/src/runtime/Native/ITypeOffsets.cs +++ b/src/runtime/Native/ITypeOffsets.cs @@ -30,6 +30,7 @@ interface ITypeOffsets int nb_invert { get; } int nb_inplace_add { get; } int nb_inplace_subtract { get; } + int nb_bool { 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 a1bae8253..0a85b05d2 100644 --- a/src/runtime/Native/TypeOffset.cs +++ b/src/runtime/Native/TypeOffset.cs @@ -37,6 +37,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_bool { 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/Properties/AssemblyInfo.cs b/src/runtime/Properties/AssemblyInfo.cs index c8a43c43a..c3e7c304f 100644 --- a/src/runtime/Properties/AssemblyInfo.cs +++ b/src/runtime/Properties/AssemblyInfo.cs @@ -4,5 +4,5 @@ [assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")] [assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")] -[assembly: AssemblyVersion("2.0.43")] -[assembly: AssemblyFileVersion("2.0.43")] +[assembly: AssemblyVersion("2.0.44")] +[assembly: AssemblyFileVersion("2.0.44")] diff --git a/src/runtime/Python.Runtime.csproj b/src/runtime/Python.Runtime.csproj index f1f77f9d7..9b870ed44 100644 --- a/src/runtime/Python.Runtime.csproj +++ b/src/runtime/Python.Runtime.csproj @@ -5,7 +5,7 @@ Python.Runtime Python.Runtime QuantConnect.pythonnet - 2.0.43 + 2.0.44 false LICENSE https://github.com/pythonnet/pythonnet diff --git a/src/runtime/Types/MetaType.cs b/src/runtime/Types/MetaType.cs index bfaced5f6..9a66240d3 100644 --- a/src/runtime/Types/MetaType.cs +++ b/src/runtime/Types/MetaType.cs @@ -386,6 +386,16 @@ public static int mp_length(BorrowedReference tp) return Enum.GetValues(type).Length; } + /// + /// Implements __bool__ for types, so that Python uses this instead of __len__ as default. + /// For types, this is always "true" + /// + public static int nb_bool(BorrowedReference tp) + { + var cb = GetManagedObject(tp) as ClassBase; + return cb == null || !cb.type.Valid ? 0 : 1; + } + /// /// Implements __contains__ for Enum types. ///