Skip to content

Commit 2cfb453

Browse files
committed
Minor fixes and unit tests
1 parent e0064ea commit 2cfb453

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/embed_tests/ClassManagerTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,33 @@ def is_enum_value_defined(value):
10561056
}
10571057
}
10581058
}
1059+
1060+
[Test]
1061+
public void EnumInterableOperationsNotSupportedForManagedNonEnumTypes()
1062+
{
1063+
using (Py.GIL())
1064+
{
1065+
var module = PyModule.FromString("EnumInterableOperationsNotSupportedForManagedNonEnumTypes", $@"
1066+
from clr import AddReference
1067+
AddReference(""Python.EmbeddingTest"")
1068+
1069+
from Python.EmbeddingTest import *
1070+
1071+
def get_enum_values():
1072+
return [x for x in ClassManagerTests]
1073+
1074+
def count_enum_values():
1075+
return len(ClassManagerTests)
1076+
1077+
def is_enum_value_defined():
1078+
return 1 in ClassManagerTests
1079+
");
1080+
1081+
Assert.Throws<PythonException>(() => module.InvokeMethod("get_enum_values"));
1082+
Assert.Throws<PythonException>(() => module.InvokeMethod("count_enum_values"));
1083+
Assert.Throws<PythonException>(() => module.InvokeMethod("is_enum_value_defined"));
1084+
}
1085+
}
10591086
}
10601087

10611088
public class NestedTestParent

src/runtime/Types/MetaType.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,10 @@ public static NewReference __subclasscheck__(BorrowedReference tp, BorrowedRefer
366366
/// </summary>
367367
public static NewReference tp_iter(BorrowedReference tp)
368368
{
369-
var type = CheckAndGetEnumType(tp);
369+
if (!TryGetEnumType(tp, out var type))
370+
{
371+
return default;
372+
}
370373
var values = Enum.GetValues(type);
371374
return new Iterator(values.GetEnumerator(), type).Alloc();
372375
}
@@ -376,7 +379,10 @@ public static NewReference tp_iter(BorrowedReference tp)
376379
/// </summary>
377380
public static int mp_length(BorrowedReference tp)
378381
{
379-
var type = CheckAndGetEnumType(tp);
382+
if (!TryGetEnumType(tp, out var type))
383+
{
384+
return -1;
385+
}
380386
return Enum.GetValues(type).Length;
381387
}
382388

@@ -385,38 +391,47 @@ public static int mp_length(BorrowedReference tp)
385391
/// </summary>
386392
public static int sq_contains(BorrowedReference tp, BorrowedReference v)
387393
{
388-
var type = CheckAndGetEnumType(tp);
394+
if (!TryGetEnumType(tp, out var type))
395+
{
396+
return -1;
397+
}
389398

390399
if (!Converter.ToManaged(v, type, out var enumValue, false) &&
391400
!Converter.ToManaged(v, typeof(int), out enumValue, false) &&
392401
!Converter.ToManaged(v, typeof(string), out enumValue, false))
393402
{
394403
Exceptions.SetError(Exceptions.TypeError,
395404
$"invalid parameter type for sq_contains: should be {Converter.GetTypeByAlias(v)}, found {type}");
405+
return -1;
396406
}
407+
397408
return Enum.IsDefined(type, enumValue) ? 1 : 0;
398409
}
399410

400-
private static Type CheckAndGetEnumType(BorrowedReference tp)
411+
private static bool TryGetEnumType(BorrowedReference tp, out Type type)
401412
{
413+
type = null;
402414
var cb = GetManagedObject(tp) as ClassBase;
403415
if (cb == null)
404416
{
405417
Exceptions.SetError(Exceptions.TypeError, "invalid object");
418+
return false;
406419
}
407420

408421
if (!cb.type.Valid)
409422
{
410423
Exceptions.SetError(Exceptions.TypeError, "invalid type");
424+
return false;
411425
}
412426

413-
var type = cb.type.Value;
414-
if (!type.IsEnum)
427+
if (!cb.type.Value.IsEnum)
415428
{
416-
Exceptions.SetError(Exceptions.TypeError, "uniterable object");
429+
Exceptions.SetError(Exceptions.TypeError, "uniterable type");
430+
return false;
417431
}
418432

419-
return type;
433+
type = cb.type.Value;
434+
return true;
420435
}
421436
}
422437
}

0 commit comments

Comments
 (0)