Skip to content

Commit 4f689a9

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

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/runtime/Util/OpsHelper.cs

+14-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,20 @@ internal static class EnumOps<T> where T : Enum
8282
{
8383
[ForbidPythonThreads]
8484
#pragma warning disable IDE1006 // Naming Styles - must match Python
85-
public static PyInt __int__(T value)
85+
public static PyInt __int__(T value) {
8686
#pragma warning restore IDE1006 // Naming Styles
87-
=> typeof(T).GetEnumUnderlyingType() == typeof(UInt64)
88-
? new PyInt(Convert.ToUInt64(value))
89-
: new PyInt(Convert.ToInt64(value));
87+
if (typeof(T).GetEnumUnderlyingType() == typeof(UInt64))
88+
{
89+
var converted = Convert.ToUInt64(value);
90+
using var _ = Py.GIL();
91+
return new PyInt(converted);
92+
}
93+
else
94+
{
95+
var converted = Convert.ToInt64(value);
96+
using var _ = Py.GIL();
97+
return new PyInt(converted);
98+
}
99+
}
90100
}
91101
}

tests/test_enum.py

+6
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)