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

Skip to content

Add __bool__ for MetaType #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/embed_tests/ClassManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>)),
Expand Down
4 changes: 2 additions & 2 deletions src/perf_tests/Python.PerformanceTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
<PackageReference Include="quantconnect.pythonnet" Version="2.0.43" GeneratePathProperty="true">
<PackageReference Include="quantconnect.pythonnet" Version="2.0.44" GeneratePathProperty="true">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand All @@ -25,7 +25,7 @@
</Target>

<Target Name="CopyBaseline" AfterTargets="Build">
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.43\lib\net9.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.44\lib\net9.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
Expand Down
1 change: 1 addition & 0 deletions src/runtime/Native/ITypeOffsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions src/runtime/Native/TypeOffset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
2 changes: 1 addition & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<PackageId>QuantConnect.pythonnet</PackageId>
<Version>2.0.43</Version>
<Version>2.0.44</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
10 changes: 10 additions & 0 deletions src/runtime/Types/MetaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ public static int mp_length(BorrowedReference tp)
return Enum.GetValues(type).Length;
}

/// <summary>
/// Implements __bool__ for types, so that Python uses this instead of __len__ as default.
/// For types, this is always "true"
/// </summary>
public static int nb_bool(BorrowedReference tp)
{
var cb = GetManagedObject(tp) as ClassBase;
return cb == null || !cb.type.Valid ? 0 : 1;
}

/// <summary>
/// Implements __contains__ for Enum types.
/// </summary>
Expand Down
Loading