Skip to content

Commit 68a2183

Browse files
authored
Add __bool__ for MetaType (#102)
* Add __bool__ for MetaType * Bump version to 2.0.44 * Minor fix
1 parent e303acf commit 68a2183

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

src/embed_tests/ClassManagerTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,33 @@ def is_enum_value_defined():
10851085
}
10861086
}
10871087

1088+
[Test]
1089+
public void TruthinessCanBeCheckedForTypes()
1090+
{
1091+
using (Py.GIL())
1092+
{
1093+
var module = PyModule.FromString("TruthinessCanBeCheckedForTypes", $@"
1094+
from clr import AddReference
1095+
AddReference(""Python.EmbeddingTest"")
1096+
1097+
from Python.EmbeddingTest import *
1098+
1099+
def throw_if_falsy():
1100+
if not ClassManagerTests:
1101+
raise Exception(""ClassManagerTests is falsy"")
1102+
1103+
def throw_if_not_truthy():
1104+
if ClassManagerTests:
1105+
return
1106+
raise Exception(""ClassManagerTests is not truthy"")
1107+
");
1108+
1109+
// Types are always truthy
1110+
Assert.DoesNotThrow(() => module.InvokeMethod("throw_if_falsy"));
1111+
Assert.DoesNotThrow(() => module.InvokeMethod("throw_if_not_truthy"));
1112+
}
1113+
}
1114+
10881115
private static TestCaseData[] IDictionaryContainsTestCases =>
10891116
[
10901117
new(typeof(TestDictionary<string, string>)),

src/perf_tests/Python.PerformanceTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
16-
<PackageReference Include="quantconnect.pythonnet" Version="2.0.43" GeneratePathProperty="true">
16+
<PackageReference Include="quantconnect.pythonnet" Version="2.0.44" GeneratePathProperty="true">
1717
<IncludeAssets>compile</IncludeAssets>
1818
</PackageReference>
1919
</ItemGroup>
@@ -25,7 +25,7 @@
2525
</Target>
2626

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

3131
<Target Name="CopyNewBuild" AfterTargets="Build">

src/runtime/Native/ITypeOffsets.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface ITypeOffsets
3030
int nb_invert { get; }
3131
int nb_inplace_add { get; }
3232
int nb_inplace_subtract { get; }
33+
int nb_bool { get; }
3334
int ob_size { get; }
3435
int ob_type { get; }
3536
int qualname { get; }

src/runtime/Native/TypeOffset.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static partial class TypeOffset
3737
internal static int nb_invert { get; private set; }
3838
internal static int nb_inplace_add { get; private set; }
3939
internal static int nb_inplace_subtract { get; private set; }
40+
internal static int nb_bool { get; private set; }
4041
internal static int ob_size { get; private set; }
4142
internal static int ob_type { get; private set; }
4243
internal static int qualname { get; private set; }

src/runtime/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
55
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
66

7-
[assembly: AssemblyVersion("2.0.43")]
8-
[assembly: AssemblyFileVersion("2.0.43")]
7+
[assembly: AssemblyVersion("2.0.44")]
8+
[assembly: AssemblyFileVersion("2.0.44")]

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<RootNamespace>Python.Runtime</RootNamespace>
66
<AssemblyName>Python.Runtime</AssemblyName>
77
<PackageId>QuantConnect.pythonnet</PackageId>
8-
<Version>2.0.43</Version>
8+
<Version>2.0.44</Version>
99
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1111
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>

src/runtime/Types/MetaType.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ public static int mp_length(BorrowedReference tp)
386386
return Enum.GetValues(type).Length;
387387
}
388388

389+
/// <summary>
390+
/// Implements __bool__ for types, so that Python uses this instead of __len__ as default.
391+
/// For types, this is always "true"
392+
/// </summary>
393+
public static int nb_bool(BorrowedReference tp)
394+
{
395+
var cb = GetManagedObject(tp) as ClassBase;
396+
return cb == null || !cb.type.Valid ? 0 : 1;
397+
}
398+
389399
/// <summary>
390400
/// Implements __contains__ for Enum types.
391401
/// </summary>

0 commit comments

Comments
 (0)