Skip to content

Commit aa471fc

Browse files
committed
Take the GIL when converting enum to int
1 parent a6e4353 commit aa471fc

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/runtime/Util/OpsHelper.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,21 @@ static Func<T, T> UnaryOp(Func<Expression, UnaryExpression> op)
8080
[Ops]
8181
internal static class EnumOps<T> where T : Enum
8282
{
83-
[ForbidPythonThreads]
8483
#pragma warning disable IDE1006 // Naming Styles - must match Python
85-
public static PyInt __int__(T value)
84+
public static PyInt __int__(T value) {
8685
#pragma warning restore IDE1006 // Naming Styles
87-
=> typeof(T).GetEnumUnderlyingType() == typeof(UInt64)
88-
? new PyInt(Convert.ToUInt64(value))
89-
: new PyInt(Convert.ToInt64(value));
86+
if (typeof(T).GetEnumUnderlyingType() == typeof(UInt64))
87+
{
88+
var converted = Convert.ToUInt64(value);
89+
using var _ = Py.GIL();
90+
return new PyInt(converted);
91+
}
92+
else
93+
{
94+
var converted = Convert.ToInt64(value);
95+
using var _ = Py.GIL();
96+
return new PyInt(converted);
97+
}
98+
}
9099
}
91100
}

tests/test_enum.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ def test_ulong_enum():
8787
assert Test.ULongEnum.Two == Test.ULongEnum(2)
8888

8989

90+
def test_simple_enum_to_int():
91+
from System import DayOfWeek
92+
assert int(DayOfWeek.Sunday) == 0
93+
94+
9095
def test_long_enum_to_int():
9196
assert int(Test.LongEnum.Max) == 9223372036854775807
9297
assert int(Test.LongEnum.Min) == -9223372036854775808
@@ -138,6 +143,7 @@ def test_enum_undefined_value():
138143
# explicitly permit undefined values
139144
Test.FieldTest().EnumField = Test.ShortEnum(20, True)
140145

146+
141147
def test_enum_conversion():
142148
"""Test enumeration conversion."""
143149
ob = Test.FieldTest()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy